DB2常用SQL的写法(
上一篇 / 下一篇 2007-08-01 00:19:25 / 个人分类:数据库
8i oTr+\.I$RJq F;p+?0来源于网络51Testing软件测试网7o:o:f"e^J1N2W
51Testing软件测试网$F*fJ,Z YE4|tDB2常用SQL的写法(持续更新中...)51Testing软件测试网
b{n t:jZ
-- Author: lavasoft51Testing软件测试网}k%R ]bFu
-- Date : 2006-12-1451Testing软件测试网Oh$V4X6^7p Nj$p#|
PvV9[)@\0-- 创建一个自定义单值类型
.['n7?/K*xvr0create distinct type var_newtype
x"]~P8O+jI
s&f~ [;^0 as decimal(5,2) with comparisons;
z P})l0RS0 -- var_newtype 类型名51Testing软件测试网;`,yM3k$tdU!m
D
-- decimal(5,2) 实际的类型51Testing软件测试网 \3~a Z1y_@
-I/cq-F8X3c7Rk^0-- 删除一个自定义单值类型
BqD^ |Nn0drop distinct type var_newtype;
~6{a
?y!DOso0 51Testing软件测试网]yb%i~,Z*m7I0]3L
-- 创建一个自定义结构数据类型51Testing软件测试网u%xS
S~u)v Z b)K
create type my_type as(
:G8B4z4d V
P D0 username varchar(20),
(C%g`I$X0gqk&gD0 department integer,51Testing软件测试网0|EJdK
salary decimal(10,2))
;x3LD.A!H-T+h0 not final
(p4U}U(Rx5fp0 mode db2sql;
Mr#h+b'j8cR0-- 修改自定义结构数据类型,我目前还没有发现删除属性的方法.
7@8WIx_vmGA0alter type my_type51Testing软件测试网M,MX+c,Vz#|w9KzQ
add attribute hiredate date;
"U%TFe\+})I IHU0
.a(Z%[2^0K0-- 删除自定义结构数据类型
y
V|
S2fY
{c){E0drop type my_type;
n7Yyc9R0-- 获取系统当前日期51Testing软件测试网?1[;GpkD?|p5js@
select current date from sysibm.sysdummy1;
C~6Q|8W!]"R!w+y;@0select current time from sysibm.sysdummy1;
\Z#~:{(C5P6e.t'Z
h0select current timestamp from sysibm.sysdummy1;
--sysibm.sysdummy1表是一个特殊的内存中的表,用它可以发现如上面演示的 DB2 寄存器的值。您也可以使用关键字 VALUES 来对寄存器或表达式求值。51Testing软件测试网@qT,u;F%?C7r
VALUES current date;51Testing软件测试网&j%EWn"E
VALUES current time;51Testing软件测试网&W cOl Rff n
VALUES current timestamp;51Testing软件测试网)vQ{'a_sh
%_w5Aea
N3K@0-- VALUES的更多用法
3o/Nw&b^r;@e0VALUES 2+5;51Testing软件测试网]8`'z"dvdh(`
VALUES 'hello lavasoft!';
'Kyv5H
YXd"B0
R!?F*C\r0values 5651Testing软件测试网
k6a(JP(y8i%N`
union all51Testing软件测试网w[ii%O]6e%pf
values 45;
+mkE[6s)l
LK
o$^0 51Testing软件测试网5mc3[c9XZ&?W
values 1,2,3,4,5,651Testing软件测试网 wn7i eLj(H$I!t
union all51Testing软件测试网&ccS1LM7S y
?
values 7,8,9,10,11,1251Testing软件测试网at1}d6{,M4db~
order by 1;
-- 更多变态级DB2 SQL写法,AnyOneTable表示任意一个存在的表
3V.\7WsM
LV0select 234 from AnyOneTable;
"}9E+]{'x~p&mx"j0select distinct 234 from AnyOneTable;51Testing软件测试网tYR} TP
select distinct 234 as 1 from AnyOneTable;
+q$y!u2O!z;qv4Xp]G0 51Testing软件测试网T"ScI[n5VfM
select 'DB2变态级的SQL哈哈' from AnyOneTable;51Testing软件测试网
t,m@"uo M;E
select distinct 'DB2变态级的SQL哈哈' from AnyOneTable;51Testing软件测试网$S,muSYxb]#Pk^
select distinct 'DB2变态级的SQL哈哈' as 1 from AnyOneTable;51Testing软件测试网&W s _``1RS gF
--(嘿嘿,好玩吧,你可以用任意一个表来当sysibm.sysdummy1用.不过不推荐这么做,除非你不记得sysibm.sysdummy1怎么写了,Oracle中(对应dual)也一样!哈哈哈哈!)
}2T+hB"M2qf j0 51Testing软件测试网 A*J(B@/|_
-- 定义变量,还可以设定默认值,给变量赋值51Testing软件测试网7l1Z-T6KMMN
declare var1 char(2);51Testing软件测试网0ldf0~?
declare var2 int default 0;51Testing软件测试网*x%o:FC G#HH:Y:_8O
set var1 = 'aa';
h+I k Y9N/MV0set var2 =23;
U{5CU7B[0
P%C}/QQ0--创建一个动态游标变量
&t)d&T[AJHc`0declare d_cur integer;
1j y$D4e yq ck0
wzt.B"_gZ?0-- 给变量赋值的另一种方法
&x"B:^;wk
_!UO!O Q1]0values expr1, expr2, expr3 into a, b, c;51Testing软件测试网p#h~n9s+z{
-- 相当于51Testing软件测试网z)t)aW;P)X"t|
set a = expr1;51Testing软件测试网3Fc*Z kr9H9G
set b = expr2;51Testing软件测试网f4WKL a8M
set c = expr3;
SJ^%Pk0
e5U$ldd,AGO0-- 还有一种赋值方式51Testing软件测试网I]$Snja+pox
set prodname = (case
)uwQ$Y hu0 when (name is not null) then name
3z:yhK5|*FK0 when (namestr is not null) then namestr51Testing软件测试网%]
^!q ].PO3nS3P
else defaultname51Testing软件测试网Zm6?9y7Y+I
end);
%m&u!ez(~nB8B)d/k0 -- 相当于51Testing软件测试网H QO D"_p1_
set prodname = coalesce(name, namestr, defaultname);51Testing软件测试网}1hW$v H}
--这个类似oracle的decode()和nvl()函数的合并.
$B1X1NGOaV/c0-- 定义一个游标
+\RW;C7gr0declare cur1 cursor with return to client for select * from dm_hy;
Z$e"D'Xk3Evx0declare cur2 cursor for select * from dm_hy; -- 静态游标
-- 创建数据表,并添加注释,插入数据.
Y6HY'O9x8}M0CREATE TABLE tbr_catalog (51Testing软件测试网2t@4H!Np
id bigint not null generated by default as identity,
_3A9iw)n0 type smallint not null,
q(aU)\Osh0 name varchar(255),51Testing软件测试网![@{_;VY6zw4G*V
parentid bigint,51Testing软件测试网R&Faj E`&p$\,m
cataloglevel bigint,
)\ }^in/D:E
`q0 descrīption varchar(255),
Z6F;C)HD;OI'u
mr0 PRIMARY KEY (id)51Testing软件测试网!G$}}y2v5z[
);
*DFc
S\ L0 comment on table tbr_catalog is 'Birt报表目录表';
m
L"e I`6SE0 comment on column tbr_catalog.ID is '标识';51Testing软件测试网 kf%qw@o1B
@N(H
comment on column tbr_catalog.type is '目录类型';
c2Zu'N D0 comment on column tbr_catalog.name is '目录名称';
Nxsn~#HR0 comment on column tbr_catalog.parentid is '目录父标识';
,o#`:ZyE0 comment on column tbr_catalog.cataloglevel is '目录层次';
h4T&M_3C&E0 comment on column tbr_catalog.descrīption is '目录描述';51Testing软件测试网4\ [
UB$] Gd
-- 给数据表插入数据