我有DB2,在这里我要填写一个名为赛季场与W代表冬季和S避暑的存储过程。该程序已完成,但我不能处理的逻辑。有人能告诉我这是如何工作?
这是在数据仓库中的一个维表。
INSERT into ABC.TIME_DIMEMSION (
DATE,
SEASON,
QUERTER)
VALUES(
DATE(myDate),
CASE
WHEN MONTH(myDate) = 1 THEN
SET SEASON= 'W';
WHEN MONTH(myDate) = 2 THEN
SET SEASON= 'W';
WHEN MONTH(myDate) = 2 THEN
SET SEASON= 'S';
ELSE
SET SEASON= 'X';
END CASE.
QUARTER(loaddate));
我发现了一个错误
看看,你可以在其他语句中使用CASE expression的描述。
它不同于Case Statement。
你必须在你的情况下使用CASE expression
:
INSERT into ABC.TIME_DIMEMSION (
DATE,
SEASON,
QUERTER)
VALUES(
DATE(myDate),
CASE MONTH(myDate)
WHEN 1 THEN 'X'
WHEN 2 THEN 'Y'
ELSE 'Z'
END,
QUARTER(loaddate)
);
-- or
INSERT into ABC.TIME_DIMEMSION (
DATE,
SEASON,
QUERTER)
VALUES(
DATE(current timestamp),
CASE
WHEN MONTH(current timestamp)=1 THEN 'X'
WHEN MONTH(current timestamp)=2 THEN 'Y'
ELSE 'Z'
END,
QUARTER(current timestamp)
);
做到这一点的方法之一(还有其他方法),在存储过程中:
注意:不要使用保留字表的列名!
declare v_season varchar(10);
declare v_themonth integer;
set v_themonth = month(somedate);
set v_season = case when v_themonth in (12,1,2) then 'Winter'
when v_themonth in (3,4,5) then 'Spring'
when v_themonth in (6,7,8) then 'Summer'
when v_themonth in (9,10,11) then 'Autumn'
end;
insert into abc.time_dimension( thedate, season)
values ( somedate, v_theseason);