信息收集于互聯網,只用于自己學習。呵呵!
-- 創建一個自定義單值類型
create distinct type var_newtype
as decimal(5,2) with comparisons;
create
-- 刪除一個自定義單值類型
drop distinct type var_newtype;
drop distinct type var_newtype;
-- 創建一個自定義結構數據類型
create type my_type as(
username varchar(20),
department integer,
salary decimal(10,2))
not final
mode db2sql;
create type my_type as(
-- 修改自定義結構數據類型,我目前還沒有發現刪除屬性的方法.
alter type my_type
add attribute hiredate date;
-- 刪除自定義結構數據類型
drop type my_type;
drop type my_type;
-- 獲取系統當前日期
select current date from sysibm.sysdummy1;
select current time from sysibm.sysdummy1;
select current timestamp from sysibm.sysdummy1;
VALUES current date;
VALUES current time;
VALUES current timestamp;
-- VALUES的更多用法
VALUES 2+5;
VALUES 'hello lavasoft!';
values 56
union all
values 45;
union all
values 45;
values 1,2,3,4,5,6
union all
values 7,8,9,10,11,12
order by 1;
union all
values 7,8,9,10,11,12
order by 1;
-- 更多變態級DB2 SQL寫法,AnyOneTable表示任意一個存在的表
select 234 from AnyOneTable;
select distinct 234 from AnyOneTable;
select distinct 234 as 1 from AnyOneTable;
select distinct 234 from AnyOneTable;
select distinct 234 as 1 from AnyOneTable;
select 'DB2變態級的SQL哈哈' from AnyOneTable;
select distinct 'DB2變態級的SQL哈哈' from AnyOneTable;
select distinct 'DB2變態級的SQL哈哈' as 1 from AnyOneTable;
select distinct 'DB2變態級的SQL哈哈' from AnyOneTable;
select distinct 'DB2變態級的SQL哈哈' as 1 from AnyOneTable;
-- 定義變量,還可以設定默認值,給變量賦值
declare var1 char(2);
declare var2 int default 0;
declare var1 char(2);
declare var2 int default 0;
set var1 = 'aa';
set var2 =23;
set var2 =23;
--創建一個動態游標變量
declare d_cur integer;
declare d_cur integer;
-- 給變量賦值的另一種方法
values expr1, expr2, expr3 into a, b, c;
-- 相當于
set a = expr1;
set b = expr2;
set c = expr3;
values expr1, expr2, expr3 into a, b, c;
set a = expr1;
set b = expr2;
set c = expr3;
-- 還有一種賦值方式
set prodname = (case
when (name is not null) then name
when (namestr is not null) then namestr
else defaultname
end);
-- 相當于
set prodname = coalesce(name, namestr, defaultname);
--這個類似oracle的decode()和nvl()函數的合并.
set prodname = coalesce(name, namestr, defaultname);
-- 定義一個游標
declare cur1 cursor with return to client for select * from dm_hy;
declare cur2 cursor for select * from dm_hy; -- 靜態游標
-- 創建數據表,并添加注釋,插入數據.
CREATE TABLE tbr_catalog (
);
insert into tbr_catalog(id, type, name, parentid, cataloglevel, description)
values (1, 0, '系統報表', 0, 0, '');
insert into tbr_catalog(id, type, name, parentid, cataloglevel, description)
values (2, 1, '用戶報表', 0, 0, '');
-- 創建外鍵
alter table tbr_storage
add constraint fk_tbr_storage
foreign key (catalogid)
references tbr_catalog(id);
alter table tbr_storage
-- 更改表,添加列
alter table aaa add sex varchar(1);
alter table aaa add sex varchar(1);
-- 更改表,刪除列
alter table aaa drop column sex;
-- 去掉參數前后的空格
rtrim(dm_hy.mc);
rtrim(dm_hy.mc);
-- 定義臨時表,通過已有person表來創建
declare global temporary table gbl_temp
like person
on commit delete rows --提交時刪除數據
not logged -- 不在日志中紀錄
in usr_tbsp -- 選用表空間
-- 創建有兩個字段的臨時表
-- 定義一個全局臨時表tmp_hy
declare global temporary table session.tmp_hy
(
dm varchar(10),
mc varchar(10)
)
with replace -- 如果存在此臨時表,則替換
not logged; -- 不在日志里紀錄
declare global temporary table session.tmp_hy
-- 通過查詢批量插入數據
inster into tab_bk(select code,name from table book);
-- select ... into的用法
select * into :h1, :h2, :h3, :h4
from emp
where empno = '528671';
-- 語句的流程控制
if() then
open cur1
fetch cur1 into t_equipid;
while(at_end<>1)do
......
set t_temp=0;
end while;
close cur1;
else
......
end if;
else
end if;
-- 外連接
select empno,deptname,projname
from (emplyoee
left outer join project
on respemp=empon)
left outer join department
on mgrno=empno;
-- in、like、order by(... ASC|DESC)的用法
select * from book t
where t.name like '%J_編程%'
and t.code in('J565333','J565222');
select * from book t
where t.name like '%J_編程%'
and t.code in('J565333','J565222');
order by t.name asc
-- 匯總表(概念復雜,難以理解,不常用)
create summary table sumy_stable1
as (select workdept,
count(*) as reccount,
sum(salary) as salary,
sum(bonus) as bonus
from employee group by workdept)
data initially deferred
refresh immediate;
create summary table sumy_stable1
-- 使用SQL一次處理一個集合語義
-- (優化前) select語句中每行的過程層和數據流層之間都有一個上下文切換
declare cur1 cursor for col1,col2 from tab_comp;
open cur1;
fetch cur1 into v1,v2;
while SQLCODE<> 100 do
if (v1>20) then
insert into tab_sel values(20,v1);
else
insert into tab_sel values(v1,v2);
end if;
fetch cur1 into v1,v2;
end while;
-- (優化前) select語句中每行的過程層和數據流層之間都有一個上下文切換
declare cur1 cursor for col1,col2 from tab_comp;
open cur1;
fetch cur1 into v1,v2;
while SQLCODE<> 100 do
end while;
-- (優化后)沒有過程層和數據流層之間的上下文切換
declare cur1 cursor for col1,col2 from tab_comp;
open cur1;
fetch cur1 into v1,v2;
while SQLCODE<> 100 do
insert into tab_sel(select (case
when col1>20 then 20
else col1
end),
col2
from tab_comp);
fetch cur1 into v1,v2;
end while;
declare cur1 cursor for col1,col2 from tab_comp;
open cur1;
fetch cur1 into v1,v2;
while SQLCODE<> 100 do
end while;
-- DB2函數分三類:列函數、標量函數、表函數
-- 列函數輸入一組數據,輸出單一結果。
-- 標量函數接收一個值,返回另外一個值。
-- 表函數只能用于SQL語句的from字句中,它返回一個表的列,類似于一個已創建的常規表。
-- 列函數輸入一組數據,輸出單一結果。
-- 標量函數接收一個值,返回另外一個值。
-- 表函數只能用于SQL語句的from字句中,它返回一個表的列,類似于一個已創建的常規表。
-- 下面是個標量函數的例子。
create function (salary int,bonus_percent int)
returns int
language SQL contains SQL
return(
)
-- 下面是表函數
create function get_marks(begin_range int,end_range int)
example 1: define a scalar function that returns the tangent of a value using the existing sine and cosine functions.
example 2: define a transform function for the structured type person.
example 3: define a table function that returns the employees in a specified department number.
example 4: define a scalar function that reverses a string.
example 4: define the table function from example 4 with auditing.