随笔 - 7, 文章 - 12, 评论 - 17, 引用 - 0
数据加载中……

DB2编程基础一

1、从数据表中取指定前几条记录
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;     
    
选第一条记录的字段到一个变量以以下方式代替
    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;

2、while、if和when地用法
while v_notfound=0 Do
set v_notfound=1;
fetch cursor1 into v_market_code;
end while;

if(或when) v_notfound=0 then
set v_notfound=1;
fetch cursor1 into v_market_code;
end if;


3、类似charindex查找字符在字串中的位置
Locate(‘y’,’dfdasfay’)
查找’y’ ’dfdasfay’中的位置。 

4、 类似datedif计算两个日期的相差天数
days(date(‘2001-06-05’)) – days(date(‘2001-04-01’))
days 
返回的是从  0001-01-01 开始计算的天数 

5、 预防字段空值的处理
SELECT DEPTNO ,DEPTNAME ,COALESCE(MGRNO ,'ABSENT'),ADMRDEPT
FROM DEPARTMENT
   COALESCE
函数返回()中表达式列表中第一个不为空的表达式,可以带多个表达式。
   
sqlserverisnull类似,但isnull好象只能两个表达式;oracleNVL 
     

6、取得处理的记录数
declare v_count int;
update tb_test set t1=’0’
where t2=’2’;
--
检查修改的行数,判断指定的记录是否存在
get diagnostics v_ count=ROW_COUNT;     
只对update,insert,delete起作用.
不对select into 有效 

7、 类型转换函数
select cast ( current time as char(8)) from tb_market_code 

8、 values的使用
如果有多个 set  语句给变量付值,最好使用values语句,改写为一句。这样可以提高效率。
 
但要注意,values不能将null值付给一个变量。
values(null) into out_return_code;
这个语句会报错的。

posted on 2007-04-29 15:17 刘浪 阅读(223) 评论(0)  编辑  收藏 所属分类: Stored Proceduce


只有注册用户登录后才能发表评论。


网站导航: