db2儲存過程總結
create or replace procedure P_test (in v_date date,out v_return int,out v_message varchar(500)) --宣告儲存,in輸入引數,out輸出引數call的時候輸入?, call p_zongjie('2021-1-1',?)
begin
declare v_name varchar(10);
declare v_age varchar(3);
declare v_money int;
declare v_cout default 10;
declare v_cursor cursor for --如果遊標迴圈有commit ,rollback,需要加with hold
select name ,age from cust;
open v_cursor
testloop:
loop
fetch v_cursor into v_name, v_age;
set v_cout=v_cout-1;
if name is null then
iterate v_cursor; --相當於 continue,
end if;
P:begin atomic --undo 異常處理只能在atomic裡執行 這裡P相當於區域性異常處理
declare undo handler for sqlexception
begin
values('存過發生異常:' || char(sqlcode)) into v_message;
set v_return=-1;
end;
update cust set age=100 where name=v_name;
end P;
if v_cout=0 then
leave v_cursor; --相當於break
end if;
end loop;
close v_cursor;
end