当前位置:数据库 > Oracle >>

oracle循环和过程

oracle循环和过程
 
例:
--创建表
create table t_test 
(
       test_id varchar2(100),
test_name varchar2(100)
);
--查询
select * from t_test;
truncate table t_test;
--声明过程--配合循环插入测试数据
declare 
v_index number := 1;
begin
        loop
               if v_index < 100 then
                  insert into t_test values ('id'||v_index,'name'||v_index);
                  v_index := v_index + 1;
               else exit;
               end if;
        end loop;       
        commit;
end;
-----------------------------------
loop循环:
create or replace procedure pro_test_loop is
i number;
begin
i:=0;
loop
  i:=i+1;
  dbms_output.put_line(i);
  if i>5 then
    exit;
  end if;
end loop;
end pro_test_loop;

while循环:
create or replace procedure pro_test_while is
i number;
begin
i:=0;
while i<5 loop
  i:=i+1;
  dbms_output.put_line(i);
end loop;
end pro_test_while;

for循环1:
create or replace procedure pro_test_for is
i number;
begin
i:=0;
for i in 1..5 loop
  dbms_output.put_line(i);
end loop;
end pro_test_for;

for循环2:
create or replace procedure pro_test_cursor is
userRow t_user%rowtype;
cursor userRows is
select * from t_user;
begin
for userRow in userRows loop
    dbms_output.put_line(userRow.Id||','||userRow.Name||','||userRows%rowcount);
end loop;
end pro_test_cursor;

 


CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,