以下的文章主要描述的是DB2编程,我们首先是从建存储过程时Create 后一定不要用TAB键开始对其进行讲述的,如果你对DB2编程,心存好奇的话,以下的文章将会揭开它的神秘面纱。
1.1 建存储过程时Create 后一定不要用TAB键
create procedure
- 1.
的create后只能用空格,而不可用tab健,否则编译会通不过。
切记,切记。
1.2 使用临时表
要注意,临时表只能建在user tempory tables space 上,如果database只有system tempory table space是不能建临时表的。
另外,DB2的临时表和sybase及oracle的临时表不太一样,DB2的临时表是在一个session内有效的。所以,如果程序有多线程,***不要用临时表,很难控制。
建临时表时***加上 with replace选项,这样就可以不显示的drop 临时表,建临时表时如果不加该选项而该临时表在该session内已创建且没有drop,这时会发生错误。
1.3 从数据表中取指定前几条记录
select * from tb_market_code fetch first 1 rows only
- 1.
但下面这种方式不允许
select market_code into v_market_code
from tb_market_code fetch first 1 rows only;
- 1.
- 2.
选***条记录的字段到一个变量以以下方式代替
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.
- 2.
- 3.
- 4.
- 5.
- 6.
1.4 游标的使用
注意commit和rollback
使用游标时要特别注意如果没有加with hold 选项,在Commit和Rollback时,该游标将被关闭。Commit 和Rollback有很多东西要注意。特别小心
游标的两种定义方式
一种为
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;
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
这种方式使用起来比较复杂,但也比较灵活。特别是可以使用with hold 选项。如果循环内有commit或rollback 而要保持该cursor不被关闭,只能使用这种方式以上的相关内容就是对DB2编程序技巧部分内容的介绍,望你能有所收获。
【编辑推荐】