在Oracle過程中使用游標

2021-04-17 16:26:48 字數 1474 閱讀 3650

create or replace procedure ss_c002_tm14to13(p_errcode out number,

p_errtext out varchar2) is

/*isbn轉換條碼,14位錯誤的轉換成13位正確的

yc2008-04-18

*/cursor c_gckc is

select *

from t_gckc

where length(isbn)=14;

v_isbn     varchar2(20);

v_tm       varchar2(30); --條碼

v_rowcount number(12, 0) := 0;

v_row      number(12, 0) := 0;

v_sysdate  date;

v_isbn2    varchar2(20); --原書號

v_rows     number(12, 0) := 0;

begin

v_sysdate := sysdate;

--迴圈讀取資料

for vc_gckc in c_gckc loop

v_rows  := v_rows + 1;

v_isbn2 := vc_gckc.isbn; --原書號

v_tm:=substr(v_isbn2,1,12)||'0';

--計算庫存是否有記錄

select count(*)

into v_row

from t_gckc

where dm = vc_gckc.dm

and xydm = vc_gckc.xydm

and isbn = v_tm;

--沒有,進行修改

if v_row = 0 then

update t_gckc

set isbn = v_tm, gxrq = v_sysdate

where dm = vc_gckc.dm

and xydm = vc_gckc.xydm

and isbn = vc_gckc.isbn;

end if;

--記錄數

v_rowcount := v_rowcount + 1;

if v_rows = 1000 then

commit;

v_rows := 0;

end if;

end loop;

--提交資料

commit;

p_errcode := 0;

p_errtext := '共' || v_rowcount || '條記錄轉換成功';

exception

when others then

rollback;

p_errcode := -1;

p_errtext := '[' || v_isbn2 || ']錯誤資訊:' || sqlerrm;

end;

儲存過程中使用游標

create proc cursortest id int 0,name varchar 50 as 建立游標 declare cursor cursor 設定游標欲操作的資料集 set cursor cursor for select id,name from users 開啟游標 open cu...

Oracle中使用游標

游標 目的 為了處理select語句返回多行資料 使用步驟 1 定義游標 cursor cursor name is select statement 2 開啟游標 open cursor name 3 提取資料 fetch cursor name into variable1,提取一行資料 或fe...

Oracle儲存過程中如何使用游標

本儲存過程的功能 把test tbl2中與test tbl1中id相同但salary不同的記錄中的salary的值更新為test tbl1中的salary的值 建立儲存過程 create or replace procedure p update test tbl2 is 定義游標 cursor c...