DB2编程的正确应用程序

数据库
我们今天主要描DB2编程,本文首先是从建存储过程时Create 后一定不要用TAB键开始对其进行讲述的,以下就是文章的主要内容描述。

 

以下的文章主要描述的是DB2编程,我们首先是从建存储过程时Create 后一定不要用TAB键开始对其进行讲述的,如果你对DB2编程,心存好奇的话,以下的文章将会揭开它的神秘面纱。

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

  1. create procedure  

的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 从数据表中取指定前几条记录

  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;   

1.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不被关闭,只能使用这种方式以上的相关内容就是对DB2编程序技巧部分内容的介绍,望你能有所收获。

【编辑推荐】

  1. DB2 V9.7新特征可以给你带来哪些好处?
  2. 如何轻松实现DB2自动增长主键?
  3. DB2常用函数与Oracle有什么不同?
  4. DB2手工添加数据库的实际操作方案描述
  5. DB2数据库确定某个表的统计信息的收集时间的操作

     

     
责任编辑:佚名 来源: flashas.net
相关推荐

2010-08-11 15:48:04

DB2编程

2010-08-18 16:45:40

IBM DB2 Cat

2010-08-16 10:10:27

DB2常用函数

2010-08-11 17:28:56

DB2 数据仓库技术

2010-08-04 15:23:04

DB2用户密码

2010-08-20 13:33:50

DB2物化视图

2010-08-13 11:02:09

DB2数据库Table

2010-08-19 09:37:41

DB2 V9.7 语句

2010-07-28 14:16:43

DB2驱动类型

2009-09-22 12:25:04

ibmdwDB2

2011-08-10 17:38:21

DB2JDBC

2010-08-13 13:40:47

DB2编程序

2010-06-30 08:41:21

SQL Server嵌

2010-08-13 10:29:35

DB2数据库

2010-08-06 09:33:08

DB2 JDBC连接

2010-08-18 10:29:41

DB2 Capture

2010-08-13 13:31:14

DB2编程序

2010-05-28 15:16:40

MySQL 资源

2010-09-07 16:11:19

执行DB2命令

2010-08-06 13:20:00

DB2锁等待
点赞
收藏

51CTO技术栈公众号