Oracle的DECODE函數
1.句法
DECODE(條件,值1,翻譯值1,值2,翻譯值2,...值n,翻譯值n,缺省值)
該函數含義如下:
IF 條件=值1 THEN
RETURN (翻譯值1)
ELSIF 條件=值2 THEN
RETURN (翻譯值2)
......
ELSIF 條件=值n THEN
RETURN (翻譯值n)
ELSE
RETURN (缺省值)
END IF
2. 實例
1)創建業績表
create table scott.sale(
month char(6),
sell number(10,2)
);
2)插入數據
insert into scott.sale values(01, 1000);
insert into scott.sale values(02, 2000);
insert into scott.sale values(03, 9000);
insert into scott.sale values(04, 7000);
insert into scott.sale values(05, 4000);
insert into scott.sale values(06, 5000);
3)創建評估表
create table scott.archive(
month char(6),
grade char(10)
);
4)根據業績表向評估表插入數據
--if sell = 900 then
--grade := 'excellent'
--else if sell = 700 then
--grade := 'perfect'
--else if sell = 500 then
--grade := 'good'
--else
--grade := 'normal'
insert into scott.archive
(select month, decode(sell, 9000, 'excellent', 7000, 'perfect', 5000, 'good', normal')
from scott.sale);
DECODE(條件,值1,翻譯值1,值2,翻譯值2,...值n,翻譯值n,缺省值)
該函數含義如下:
IF 條件=值1 THEN
RETURN (翻譯值1)
ELSIF 條件=值2 THEN
RETURN (翻譯值2)
......
ELSIF 條件=值n THEN
RETURN (翻譯值n)
ELSE
RETURN (缺省值)
END IF
2. 實例
1)創建業績表
create table scott.sale(
month char(6),
sell number(10,2)
);
2)插入數據
insert into scott.sale values(01, 1000);
insert into scott.sale values(02, 2000);
insert into scott.sale values(03, 9000);
insert into scott.sale values(04, 7000);
insert into scott.sale values(05, 4000);
insert into scott.sale values(06, 5000);
3)創建評估表
create table scott.archive(
month char(6),
grade char(10)
);
4)根據業績表向評估表插入數據
--if sell = 900 then
--grade := 'excellent'
--else if sell = 700 then
--grade := 'perfect'
--else if sell = 500 then
--grade := 'good'
--else
--grade := 'normal'
insert into scott.archive
(select month, decode(sell, 9000, 'excellent', 7000, 'perfect', 5000, 'good', normal')
from scott.sale);