第一个例子:
call insertTable();
(在创建的时候insertTable不能写成insertTable())
create or replace procedure insertTable is
begin
--新建游标suts
Declare cursor suts is select * from sys_user_temp;
begin
--开始循环
for sut in suts loop
begin
insert into user (ID, LOGIN_NAME, PASSWORD, ENABLED)
values (seq_user.nextval,sut.login_name, '123456', 1)
insert into sys_user_role (USER_ID, ROLE_ID, INS_ID)
values ((select id from user where login_name=sut.login_name), (select ur.id from user_role ur where ur.name=sut.role), 0);
end;
end loop;
commit;
end;
end;
第二个例子:
create or replace procedure test_proc is
v_date date; --变量定义
begin
select sysdate into v_date from dual;
end test_proc;
第三个例子:(定义游标)
create or replace procedure test_proc is
v_date date; --定义变量
cursor cur is select * from ldcode; --定义游标
begin
select sysdate into v_date from dual;
end test_proc;
第四个例子:(编写for循环)
create or replace procedure test_proc is
v_date date; --定义变量
cursor cur is select * from ldcode where rownum<10; --定义游标
begin
select sysdate into v_date from dual;
--游标for循环开始
for temp in cur loop --temp为临时变量名,自己任意起
Dbms_Output.put_line(temp.Code); --输出某个字段,使用"变量名.列名"即可。
end loop;
--游标for循环结束
end test_proc;
第五个例子:
--存储过程中for循环调用存储过程
create or replace procedure testdate2 is
Cursor datekeey is select to_number(to_char(datekeey,'yyyyMMdd')) datekeey from (
select date'2017-01-01' + (rownum - 1) datekeey
from dual connect by rownum <= (date'2018-09-18' - date'2017-01-01' + 1)
); --遍历日期的起始
i number;
begin
for i in datekeey LOOP
begin
--调用存储过程
testdate(i.datekeey);
--dbms_output.put_line(i.datekeey);
end;
end LOOP;
commit;
end testdate2;