declare cur_col cursor using(动态sql)或者 for(select语句);
--打开游标
open cur_col with hold;
CustomerLoop1:
loop
fetch next cur_col into v_col;
if (sqlcode <> 0) then
leave CustomerLoop1 ;
end if ;
--设置游标需要执行的功能
end loop CustomerLoop1;
--关闭游标
close cur_col;
如果游标是可更新的就可以更新或者删除游标所返回的行,如果游标是只读的就只能读取数据不能更新或者删除数据。
可以通过在declare中使用关键字read only或者update来显示的指定一个游标是否可更新。
所有可滚动游标和所有insensitive游标都是只读的。
举例:
begin
DECLARE v_Ssql VARCHAR(30000); --执行SQL
declare CUR_MSISDN CHAR(64);
declare CUR_IMSI CHAR(64);
declare V_MSISDN CHAR(64);
declare V_IMSI CHAR(64);
--定义游标
declare cur_col cursor for select MSISDN ,IMSI from #SUBSCRIBER_CFG;
--打开游标
open cur_col with hold;
CustomerLoop1:
loop
fetch next cur_col into V_MSISDN,V_IMSI;
if (sqlcode <> 0) then
leave CustomerLoop1 ;
end if ;
set v_Ssql = 'update UNITE_UFDR_SUBSCRIBER_CFG a '
||' set a.MSISDN = '''||V_MSISDN
||' and a.IMSI = '||V_IMSI
||''' where a.MSISDN ='''||V_MSISDN
||''' or a.IMSI ='''||V_IMSI
||'''';
EXECUTE IMMEDIATE v_Ssql;
commit;
--设置游标需要执行的功能
end loop CustomerLoop1;
--关闭游标
close cur_col;
end;
--打开游标
open cur_col with hold;
CustomerLoop1:
loop
fetch next cur_col into v_col;
if (sqlcode <> 0) then
leave CustomerLoop1 ;
end if ;
--设置游标需要执行的功能
end loop CustomerLoop1;
--关闭游标
close cur_col;
如果游标是可更新的就可以更新或者删除游标所返回的行,如果游标是只读的就只能读取数据不能更新或者删除数据。
可以通过在declare中使用关键字read only或者update来显示的指定一个游标是否可更新。
所有可滚动游标和所有insensitive游标都是只读的。
举例:
begin
DECLARE v_Ssql VARCHAR(30000); --执行SQL
declare CUR_MSISDN CHAR(64);
declare CUR_IMSI CHAR(64);
declare V_MSISDN CHAR(64);
declare V_IMSI CHAR(64);
--定义游标
declare cur_col cursor for select MSISDN ,IMSI from #SUBSCRIBER_CFG;
--打开游标
open cur_col with hold;
CustomerLoop1:
loop
fetch next cur_col into V_MSISDN,V_IMSI;
if (sqlcode <> 0) then
leave CustomerLoop1 ;
end if ;
set v_Ssql = 'update UNITE_UFDR_SUBSCRIBER_CFG a '
||' set a.MSISDN = '''||V_MSISDN
||' and a.IMSI = '||V_IMSI
||''' where a.MSISDN ='''||V_MSISDN
||''' or a.IMSI ='''||V_IMSI
||'''';
EXECUTE IMMEDIATE v_Ssql;
commit;
--设置游标需要执行的功能
end loop CustomerLoop1;
--关闭游标
close cur_col;
end;