最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
DB2编程序技巧 (三)
时间:2022-07-02 21:22:23 编辑:袖梨 来源:一聚教程网
另一种为
pcursor1: for loopcs1 as cousor1 cursor as
select market_code as market_code
from tb_market_code
for update
do
end for;
这种方式的优点是比较简单,不用(也不允许)使用open,fetch,close。
但不能使用with hold 选项。如果在游标循环内要使用commit,rollback则不能使用这种方式。如果没有commit或rollback的要求,推荐使用这种方式(看来For这种方式有问题)。
修改游标的当前记录的方法
update tb_market_code set market_code='0' where current of cursor1;
不过要注意将cursor1定义为可修改的游标
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的转码操作
oracle中有一个函数 select decode(a1,'1','n1','2','n2','n3') aa1 from
db2没有该函数,但可以用变通的方法
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计算两个日期的相差天数
days(date(‘2001-06-05’)) – days(date(‘2001-04-01’))
days 返回的是从 0001-01-01 开始计算的天数
1.8 写UDF的例子
C写见sqllibsamplescliudfsrv.c
1.9 创建含identity值(即自动生成的ID)的表
建这样的表的写法
CREATE TABLE test
(t1 SMALLINT NOT NULL
GENERATED ALWAYS AS IDENTITY
(START WITH 500, INCREMENT BY 1),
t2 CHAR(1));
在一个表中只允许有一个identity的column.
pcursor1: for loopcs1 as cousor1 cursor as
select market_code as market_code
from tb_market_code
for update
do
end for;
这种方式的优点是比较简单,不用(也不允许)使用open,fetch,close。
但不能使用with hold 选项。如果在游标循环内要使用commit,rollback则不能使用这种方式。如果没有commit或rollback的要求,推荐使用这种方式(看来For这种方式有问题)。
修改游标的当前记录的方法
update tb_market_code set market_code='0' where current of cursor1;
不过要注意将cursor1定义为可修改的游标
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的转码操作
oracle中有一个函数 select decode(a1,'1','n1','2','n2','n3') aa1 from
db2没有该函数,但可以用变通的方法
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计算两个日期的相差天数
days(date(‘2001-06-05’)) – days(date(‘2001-04-01’))
days 返回的是从 0001-01-01 开始计算的天数
1.8 写UDF的例子
C写见sqllibsamplescliudfsrv.c
1.9 创建含identity值(即自动生成的ID)的表
建这样的表的写法
CREATE TABLE test
(t1 SMALLINT NOT NULL
GENERATED ALWAYS AS IDENTITY
(START WITH 500, INCREMENT BY 1),
t2 CHAR(1));
在一个表中只允许有一个identity的column.
相关文章
- 银与绯吉尔兰技能怎么样 吉尔兰战斗特性解析 07-14
- 异环异象管理局局长是谁 艾尔菲德角色详细介绍 07-14
- 欧意易ok官网下载版 欧意易app下载 07-14
- 迷你世界马车怎么做 马车制作方法介绍 07-14
- 碳碳岛许愿池有什么作用 许愿池建造方法介绍 07-14
- 最终幻想14水晶世界怎么招募雇员 招募雇员方法介绍 07-14