DB2数据库编程序要用到的小技巧有哪些?


  本文标签:DB2数据库

  以下的文章主要向大家讲述的是DB2数据库编程序的一些小操作技巧,我在一个信誉度很好的网站找到一个关于DB2编程序的一些小操作技巧的资料,拿出来供大家分享,以下的文章就是对相关内容的描述  。
 

  1. 建存储过程时Create 后一定不要用TAB键

  

  1. create procedure 

  的create后只能用空格,而不可用tab健,否则编译会通不过  。 切记,切记  。

  2. 使用临时表

  要注意,临时表只能建在user tempory tables space 上,如果database只有system tempory table space是不能建临时表的  。

  另外,DB2的临时表和sybase及oracle的临时表不太一样,DB2数据库的临时表是在一个session内有效的  。所以,如果程序有多线程,最好不要用临时表,很难控制  。

  建临时表时最好加上 with replace选项,这样就可以不显示的drop 临时表,建临时表时如果不加该选项而该临时表在该session内已创建且没有drop,这时会发生错误  。

  3. 从数据表中取指定前几条记录

  

  1. select * from tb_market_code fetch first 1 rows only 

  但下面这种方式不允许

  

  1. select market_code into v_market_code  
  2. from tb_market_code fetch first 1 rows only;  

  

  选第一条记录的字段到一个变量以以下方式代替

  

  1. declare v_market_code char(1);  
  2. declare cursor1 cursor for select market_code from tb_market_code  
  3. fetch first 1 rows only for update;  
  4. open cursor1;  
  5. fetch cursor1 into v_market_code;  
  6. close cursor1;  

  

  4. 游标的使用

  注意commit和rollback

  使用游标时要特别注意如果没有加with hold 选项,在Commit和Rollback时,该游标将被关闭  。Commit 和Rollback有很多东西要注意  。特别小心

  游标的两种定义方式

  一种为

  

  1. declare continue handler for not found  
  2. begin  
  3. set v_notfound = 1;  
  4. end;  
  5. declare cursor1 cursor with hold for select market_code from tb_market_code for update;  
  6. open cursor1;  
  7. set v_notfound=0;  
  8. fetch cursor1 into v_market_code;  
  9. while v_notfound=0 Do  
  10. --work  
  11. set v_notfound=0;  
  12. fetch cursor1 into v_market_code;  
  13. end while;  
  14. close cursor1;  

  

  这种方式使用起来比较复杂,但也比较灵活  。特别是可以使用with hold 选项  。如果循环内有commit或rollback 而要保持该cursor不被关闭,只能使用这种方式  。

  另一种为

  

  1. pcursor1: for loopcs1 as cousor1 cursor as  
  2. select market_code as market_code  
  3. from tb_market_code  
  4. for update  
  5. do  
  6. end for;  

  

  这种方式的优点是比较简单,不用(也不允许)使用open,fetch,close  。

  但不能使用with hold 选项  。如果在游标循环内要使用commit,rollback则不能使用这种方式  。如果没有commit或rollback的要求,推荐使用这种方式(看来For这种方式有问题)  。

  修改游标的当前记录的方法

  

  1. update tb_market_code set market_code=0 where current of cursor1; 

  不过要注意将cursor1定义为可修改的游标

  

  1. declare cursor1 cursor for select market_code from tb_market_code  
  2. for update;  

  

  for update 不能和GROUP BY、 DISTINCT、 ORDER BY、 FOR READ ONLY及UNION, EXCEPT, or INTERSECT但 UNION ALL除外)一起使用  。

  5. 类似decode的转码操作

  oracle中有一个函数 select decode(a1,1,n1,2,n2,n3) aa1 from

  DB2数据库没有该函数,但可以用变通的方法

  

  1. select case a1  
  2. when 1 then n1  
  3. when 2 then n2  
  4. else n3  
  5. end as aa1 from  

  

  6. 类似charindex查找字符在字串中的位置

  Locate(‘y’,’dfdasfay’)

  查找’y’ 在’dfdasfay’中的位置  。

  7. 类似datedif计算两个日期的相差天数

  

  1. days(date(‘2001-06-05’)) – days(date(‘2001-04-01’)) 

  days 返回的是从 0001-01-01 开始计算的天数

  8. 写UDF的例子

  C写见sqllib\samples\cli\udfsrv.c

  9.创建含identity值(即自动生成的ID)的表

  建这样的表的写法

  

  1. CREATE TABLE test  
  2. (t1 SMALLINT NOT NULL  
  3. GENERATED ALWAYS AS IDENTITY  
  4. (START WITH 500, INCREMENT BY 1),  
  5. t2 CHAR(1));  

  

  在一个表中只允许有一个identity的column.以上的相关内容就是对DB2数据库编程序的一些小操作技巧的介绍,望你能有所收获  。