db2介面呼叫儲存過程 DB2儲存過程示例

2021-10-13 10:55:24 字數 4623 閱讀 5617

下面是對於建立db2儲存過程的乙個示例,正在入手這項學習的朋友可以參考。

在db2命令列中進行操作:

1.開始-執行,輸入cmd,執行db2cw命令進入db2命令列模式

2.再輸入db2後,便可以輸入sql語句進行執行;如果不輸入,則需要db2 "sql語句"來執行

備註,命令回車提示成功後,對於insert則一定要進行commit,否則無效

--建立表結構

db2 "create table tablename as (select row_number(),zd from tablename2) definition only"

--建立新錶

db2 "create table tablename(a varchar(14))"

--刪除表

db2 "drop table table_name"

--向表中插入資料

db2 "insert into tablename(zd1,zd2,zd3,zd4,zd5) values ('aaaaa','110','1111','2222','2012-09-11')"

--查詢表中資料

db2 "select * from tablename"

--刪除表中前10000萬記錄

db2 "delete from (select * from tablename fetch first 10000 rows only) where 1=1"

--提交

db2 "commit"

--查詢系統當前時間

db2 "select current time from sysibm.sysdummy1"

--查詢表中總記錄數

db2 "select count(0) from tablename"

db2 "select current time from sysibm.sysdummy1"

db2 "call 儲存過程名()"--呼叫儲存過程

db2 "commit"--提交

db2 "select current time from sysibm.sysdummy1"

====編寫乙個儲存過程造資料,5個字段(1個日期型別[值相對固定],3個字段隨機從已存在的表中取,1個字段同另1欄位一起作主鍵)

分析與結果:

沒接觸過db2的話,看看當前不會的知識點

1.如何對現有表的字段值進行隨機取值

select int(rand()*100) from sysibm.sysdummy1;

2.如何找到db2類似於oracle的rownum唯一物理行

select zd,  row_number() over(order by zd desc) as rownum  from ora_table1;

select zd from (select zd,  row_number() over(order by zd desc) as rownum  from ora_table1 ) where rownum=100;

3.db2中的迴圈語法,儲存過程的結構是什麼

while i<10 do

--sql語句

set i=i+1;

end while;

可以在db2的samples資料夾中找到whiles.db2例項進行參考

4.如何執行儲存過程

命令列模式中輸入

db2 -td@ -vf whiles.db2

db2 "call dept_median(51, ?)--若無引數,則db2 "call dept_median()"

====結果(優化中)

create procedure test_pro()

language sql

begin

declare i int default 1001;

declare aa int default 1;

declare t int;

declare total_605 int;

declare total_606 int;

declare total_need_code int;

declare a605 char(3);

declare a606 char(4);

declare abankcode char(14);

select count(need_zd)+1 into total_605 from ora_dictionary where type_a='605';

select count(need_zd)+1 into total_606 from ora_dictionary where type_a='606';

select count(need_code)+1 into total_need_code from table_info;

while i<=10000 do

select int(rand()*(total_605+1)) into t from sysibm.sysdummy1;

select need_zd into a605 from (select need_zd,  row_number() over(order by need_zd desc) as rownum  from ora_dictionary where type_a='605' ) where rownum=t;

select int(rand()*(total_606+1)) into t from sysibm.sysdummy1;

select need_zd into a606 from (select need_zd,  row_number() over(order by need_zd desc) as rownum  from ora_dictionary where type_a='606' ) where rownum=t;

select int(rand()*(total_need_code+1)) into t from sysibm.sysdummy1;

select need_code into abankcode from (select need_code,  row_number() over(order by need_code desc) as rownum  from table_info ) where rownum=t;

insert into czystru(zd1,zd2,zd3,zd4,zd5) values(char(aa),a605,a606,abankcode,'2010-08-03');

if (mod(i,1000)=0)

then

commit;

end if;

set aa=aa+1;

set i=i+1;

end while;

end @

====db2自帶的whiles.db2例子

create procedure dept_median

(in deptnumber smallint, out mediansalary double)

language sql

begin

declare sqlcode integer;

declare sqlstate char(5);

declare v_numrecords int default 1;

declare v_counter int default 0;

declare v_mod int default 0;

declare v_salary1 double default 0;

declare v_salary2 double default 0;

declare c1 cursor for

select cast(salary as double) from staff

where dept = deptnumber

order by salary;

declare exit handler for not found

set mediansalary = 6666;

-- initialize out parameter

set mediansalary = 0;

select count(*) into v_numrecords from staff

where dept = deptnumber;

open c1;

set v_mod = mod(v_numrecords, 2);

case v_mod

when 0 then

while v_counter < (v_numrecords / 2 + 1) do

set v_salary1 = v_salary2;

fetch c1 into v_salary2;

set v_counter = v_counter + 1;

end while;

set mediansalary = (v_salary1 + v_salary2)/2;

when 1 then

while v_counter < (v_numrecords / 2 + 1) do

fetch c1 into mediansalary;

set v_counter = v_counter + 1;

end while;

end case;

end @

db2儲存過程批量插入資料 db2儲存過程批量更新

db2儲存過程批量更新 雲伺服器 elastic compute service,簡稱ecs 是阿里雲提供的效能卓越 穩定可靠 彈性擴充套件的iaas infrastructure as a service 級別雲計算服務。雲伺服器ecs免去了您採購it硬體的前期準備,讓您像使用水 電 天然氣等公共...

db2儲存過程批量插入資料 db2儲存過程批量提交

db2儲存過程批量提交 雲伺服器 elastic compute service,簡稱ecs 是阿里雲提供的效能卓越 穩定可靠 彈性擴充套件的iaas infrastructure as a service 級別雲計算服務。雲伺服器ecs免去了您採購it硬體的前期準備,讓您像使用水 電 天然氣等公共...

DB2儲存過程雜談

db2常用的異常處理方式分為三種 undo 回滾 exit 退出 continue 繼續執行 declare undo exit continue handler for 異常處理的物件可以為針對指定的sqlstate declare undo exit continue handler for s...