oracle 如何创建游标,oracle存储过程的创建和游标的使用

第一个例子:

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值