文章詳情頁(yè)
DB2編程序技巧(1)
瀏覽:142日期:2023-03-24 09:22:34
正在看的db2教程是:DB2編程序技巧(1)。
1 DB2編程
1.1 建存儲(chǔ)過(guò)程時(shí)Create 后一定不要用TAB鍵
create procedure
的create后只能用空格,而不可用tab健,否則編譯會(huì)通不過(guò)。
切記,切記。
1.2 使用臨時(shí)表
要注意,臨時(shí)表只能建在user tempory tables space 上,如果database只有system tempory table space是不能建臨時(shí)表的。
另外,DB2的臨時(shí)表和sybase及oracle的臨時(shí)表不太一樣,DB2的臨時(shí)表是在一個(gè)session內(nèi)有效的。所以,如果程序有多線程,最好不要用臨時(shí)表,很難控制。
建臨時(shí)表時(shí)最好加上 with replace選項(xiàng),這樣就可以不顯示的drop 臨時(shí)表,建臨時(shí)表時(shí)如果不加該選項(xiàng)而該臨時(shí)表在該session內(nèi)已創(chuàng)建且沒(méi)有drop,這時(shí)會(huì)發(fā)生錯(cuò)誤。
1.3 從數(shù)據(jù)表中取指定前幾條記錄
select * from tb_market_code fetch first 1 rows only
但下面這種方式不允許
select market_code into v_market_code
from tb_market_code fetch first 1 rows only;
選第一條記錄的字段到一個(gè)變量以以下方式代替
declare v_market_code char(1);
declare cursor1 cursor for select market_code from tb_market_code
fetch first 1 rows only for update;
open cursor1;
fetch cursor1 into v_market_code;
close cursor1;
1.4 游標(biāo)的使用
注意commit和rollback
使用游標(biāo)時(shí)要特別注意如果沒(méi)有加with hold 選項(xiàng),在Commit和Rollback時(shí),該游標(biāo)將被關(guān)閉。Commit 和Rollback有很多東西要注意。特別小心
游標(biāo)的兩種定義方式
一種為
declare continue handler for not found
begin
set v_notfound = 1;
end;
declare cursor1 cursor with hold for select market_code from tb_market_code for update;
open cursor1;
set v_notfound=0;
fetch cursor1 into v_market_code;
while v_notfound=0 Do
--work
set v_notfound=0;
fetch cursor1 into v_market_code;
end while;
close cursor1;
這種方式使用起來(lái)比較復(fù)雜,但也比較靈活。特別是可以使用with hold 選項(xiàng)。如果循環(huán)內(nèi)有commit或rollback 而要保持該cursor不被關(guān)閉,只能使用這種方式。
另一種為
pcursor1: for loopcs1 as cousor1 cursor as
select market_code as market_code
from tb_market_code
for update
do
end for;
這種方式的優(yōu)點(diǎn)是比較簡(jiǎn)單,不用(也不允許)使用open,fetch,close。
但不能使用with hold 選項(xiàng)。如果在游標(biāo)循環(huán)內(nèi)要使用commit,rollback則不能使用這種方式。如果沒(méi)有commit或rollback的要求,推薦使用這種方式(看來(lái)For這種方式有問(wèn)題)。
修改游標(biāo)的當(dāng)前記錄的方法
update tb_market_code set market_code=0 where current of cursor1;
不過(guò)要注意將cursor1定義為可修改的游標(biāo)
declare cursor1 cursor for select market_code from tb_market_code
for update;
for update 不能和GROUP BY、 DISTINCT、 ORDER BY、 FOR READ ONLY及UNION, EXCEPT, or INTERSECT但 UNION ALL除外)一起使用。
1.5 類似decode的轉(zhuǎn)碼操作
oracle中有一個(gè)函數(shù) select decode(a1,1,n1,2,n2,n3) aa1 from
db2沒(méi)有該函數(shù),但可以用變通的方法
select case a1
when 1 then n1
when 2 then n2
else n3
end as aa1 from
1.6 類似charindex查找字符在字串中的位置
Locate(‘y","dfdasfay")
查找"y" 在"dfdasfay"中的位置。
1.7 類似datedif計(jì)算兩個(gè)日期的相差天數(shù)
days(date(‘2001-06-05")) – days
1 DB2編程
1.1 建存儲(chǔ)過(guò)程時(shí)Create 后一定不要用TAB鍵
create procedure
的create后只能用空格,而不可用tab健,否則編譯會(huì)通不過(guò)。
切記,切記。
1.2 使用臨時(shí)表
要注意,臨時(shí)表只能建在user tempory tables space 上,如果database只有system tempory table space是不能建臨時(shí)表的。
另外,DB2的臨時(shí)表和sybase及oracle的臨時(shí)表不太一樣,DB2的臨時(shí)表是在一個(gè)session內(nèi)有效的。所以,如果程序有多線程,最好不要用臨時(shí)表,很難控制。
建臨時(shí)表時(shí)最好加上 with replace選項(xiàng),這樣就可以不顯示的drop 臨時(shí)表,建臨時(shí)表時(shí)如果不加該選項(xiàng)而該臨時(shí)表在該session內(nèi)已創(chuàng)建且沒(méi)有drop,這時(shí)會(huì)發(fā)生錯(cuò)誤。
1.3 從數(shù)據(jù)表中取指定前幾條記錄
select * from tb_market_code fetch first 1 rows only
但下面這種方式不允許
select market_code into v_market_code
from tb_market_code fetch first 1 rows only;
選第一條記錄的字段到一個(gè)變量以以下方式代替
declare v_market_code char(1);
declare cursor1 cursor for select market_code from tb_market_code
fetch first 1 rows only for update;
open cursor1;
fetch cursor1 into v_market_code;
close cursor1;
1.4 游標(biāo)的使用
注意commit和rollback
使用游標(biāo)時(shí)要特別注意如果沒(méi)有加with hold 選項(xiàng),在Commit和Rollback時(shí),該游標(biāo)將被關(guān)閉。Commit 和Rollback有很多東西要注意。特別小心
游標(biāo)的兩種定義方式
一種為
declare continue handler for not found
begin
set v_notfound = 1;
end;
declare cursor1 cursor with hold for select market_code from tb_market_code for update;
open cursor1;
set v_notfound=0;
fetch cursor1 into v_market_code;
while v_notfound=0 Do
--work
set v_notfound=0;
fetch cursor1 into v_market_code;
end while;
close cursor1;
這種方式使用起來(lái)比較復(fù)雜,但也比較靈活。特別是可以使用with hold 選項(xiàng)。如果循環(huán)內(nèi)有commit或rollback 而要保持該cursor不被關(guān)閉,只能使用這種方式。
另一種為
pcursor1: for loopcs1 as cousor1 cursor as
select market_code as market_code
from tb_market_code
for update
do
end for;
這種方式的優(yōu)點(diǎn)是比較簡(jiǎn)單,不用(也不允許)使用open,fetch,close。
但不能使用with hold 選項(xiàng)。如果在游標(biāo)循環(huán)內(nèi)要使用commit,rollback則不能使用這種方式。如果沒(méi)有commit或rollback的要求,推薦使用這種方式(看來(lái)For這種方式有問(wèn)題)。
修改游標(biāo)的當(dāng)前記錄的方法
update tb_market_code set market_code=0 where current of cursor1;
不過(guò)要注意將cursor1定義為可修改的游標(biāo)
declare cursor1 cursor for select market_code from tb_market_code
for update;
for update 不能和GROUP BY、 DISTINCT、 ORDER BY、 FOR READ ONLY及UNION, EXCEPT, or INTERSECT但 UNION ALL除外)一起使用。
1.5 類似decode的轉(zhuǎn)碼操作
oracle中有一個(gè)函數(shù) select decode(a1,1,n1,2,n2,n3) aa1 from
db2沒(méi)有該函數(shù),但可以用變通的方法
select case a1
when 1 then n1
when 2 then n2
else n3
end as aa1 from
1.6 類似charindex查找字符在字串中的位置
Locate(‘y","dfdasfay")
查找"y" 在"dfdasfay"中的位置。
1.7 類似datedif計(jì)算兩個(gè)日期的相差天數(shù)
days(date(‘2001-06-05")) – days
標(biāo)簽:
DB2
相關(guān)文章:
1. MYSQL(電話號(hào)碼,身份證)數(shù)據(jù)脫敏的實(shí)現(xiàn)2. Windows10環(huán)境安裝sdk8的圖文教程3. 根據(jù)IP跳轉(zhuǎn)到用戶所在城市的實(shí)現(xiàn)步驟4. MySQL兩千萬(wàn)數(shù)據(jù)優(yōu)化&遷移5. navicat for mysql導(dǎo)出數(shù)據(jù)庫(kù)的方法6. 恢復(fù)從 Access 2000、 Access 2002 或 Access 2003 中數(shù)據(jù)庫(kù)刪除表的方法7. mysql查詢的控制語(yǔ)句圖文詳解8. SQL語(yǔ)句中的ON DUPLICATE KEY UPDATE使用9. MySQL 性能優(yōu)化,讓數(shù)據(jù)庫(kù)跑的更快10. MySQL5.7 mysqldump備份與恢復(fù)的實(shí)現(xiàn)
排行榜
