将查询从 PLSQL 迁移到 SQL 服务器
Migrate a query from PLSQL TO SQL SERVER
我这部分代码是用PL/SQL
写的
if l_rec.sched_interval_type = 'DAYS' then
if l_max_date is null then
if l_rec.DAY_START_DAY is not null then
SELECT next_day(p_date, l_rec.DAY_START_DAY)
INTO l_start_date
FROM dual;
else
l_start_date := TRUNC(p_date);
end if;
else -- previous schedule recs exist...
if l_rec.DAY_START_DAY is not null then
SELECT next_day(GREATEST(l_max_date, p_date), l_rec.DAY_START_DAY)
INTO l_start_date
FROM dual;
else
l_start_date := l_max_date + l_rec.SCHED_AVAIL_DAYS;
end if;
end if;
我需要把它转换成SQL服务器
select
CASE WHEN @l_max_date is null then
SELECT @l_start_date = CASE WHEN (SELECT sched_interval_type from #l_rec) = 'DAYS' and @l_max_date is null and @NextDayID is not null then
DATEADD(DAY, (DATEDIFF(DAY, ((@NextDayID + 5) % 7),@p_date) / 7) * 7 + 7, ((@NextDayID + 5) % 7))
else CONVERT(DATETIME, CONVERT(DATE, @p_date)) end
from #l_rec
ELSE -- previous schedule recs exist...
SELECT @l_start_date = CASE WHEN (SELECT sched_interval_type from #l_rec) = 'DAYS' and @l_max_date is null and @NextDayID is not null then
DATEADD(DAY, (DATEDIFF(DAY, ((@NextDayID + 5) % 7), [dbo].[greatest](@l_max_date, @p_date)) / 7) * 7 + 7, ((@NextDayID + 5) % 7))
else @l_max_date+ (SELECT SCHED_AVAIL_DAYS from #l_rec) end
from #l_rec
END
我对 case.. when
有疑问。有办法修改吗?
在 TSQL 中,CASE
表达式returns某种类型的单个值。 (没有 CASE
语句。)您可以使用类似的东西:
set @L_Start_Date = case
when @L_Max_Date is null then
( select ... )
when <some other condition> then
( some other expression that results in a single value )
else
( expression that provides a default value )
end
请注意,在 SELECT
中不执行向变量赋值,它只是 returns 向 CASE
赋值。
试试这个...
SELECT @l_start_date =
CASE
WHEN @l_max_date is null THEN
CASE
WHEN sched_interval_type = 'DAYS' AND @l_max_date IS NULL AND @NextDayID IS NOT NULL THEN DATEADD(DAY, (DATEDIFF(DAY, ((@NextDayID + 5) % 7),@p_date) / 7) * 7 + 7, ((@NextDayID + 5) % 7))
ELSE CONVERT(DATETIME, CONVERT(DATE, @p_date))
END
ELSE
CASE
WHEN sched_interval_type = 'DAYS' AND @l_max_date IS NULL AND @NextDayID IS NOT NULL THEN DATEADD(DAY, (DATEDIFF(DAY, ((@NextDayID + 5) % 7), [dbo].[greatest](@l_max_date, @p_date)) / 7) * 7 + 7, ((@NextDayID + 5) % 7))
ELSE @l_max_date+ SCHED_AVAIL_DAYS
END
END
FROM #l_rec
我这部分代码是用PL/SQL
写的 if l_rec.sched_interval_type = 'DAYS' then
if l_max_date is null then
if l_rec.DAY_START_DAY is not null then
SELECT next_day(p_date, l_rec.DAY_START_DAY)
INTO l_start_date
FROM dual;
else
l_start_date := TRUNC(p_date);
end if;
else -- previous schedule recs exist...
if l_rec.DAY_START_DAY is not null then
SELECT next_day(GREATEST(l_max_date, p_date), l_rec.DAY_START_DAY)
INTO l_start_date
FROM dual;
else
l_start_date := l_max_date + l_rec.SCHED_AVAIL_DAYS;
end if;
end if;
我需要把它转换成SQL服务器
select
CASE WHEN @l_max_date is null then
SELECT @l_start_date = CASE WHEN (SELECT sched_interval_type from #l_rec) = 'DAYS' and @l_max_date is null and @NextDayID is not null then
DATEADD(DAY, (DATEDIFF(DAY, ((@NextDayID + 5) % 7),@p_date) / 7) * 7 + 7, ((@NextDayID + 5) % 7))
else CONVERT(DATETIME, CONVERT(DATE, @p_date)) end
from #l_rec
ELSE -- previous schedule recs exist...
SELECT @l_start_date = CASE WHEN (SELECT sched_interval_type from #l_rec) = 'DAYS' and @l_max_date is null and @NextDayID is not null then
DATEADD(DAY, (DATEDIFF(DAY, ((@NextDayID + 5) % 7), [dbo].[greatest](@l_max_date, @p_date)) / 7) * 7 + 7, ((@NextDayID + 5) % 7))
else @l_max_date+ (SELECT SCHED_AVAIL_DAYS from #l_rec) end
from #l_rec
END
我对 case.. when
有疑问。有办法修改吗?
在 TSQL 中,CASE
表达式returns某种类型的单个值。 (没有 CASE
语句。)您可以使用类似的东西:
set @L_Start_Date = case
when @L_Max_Date is null then
( select ... )
when <some other condition> then
( some other expression that results in a single value )
else
( expression that provides a default value )
end
请注意,在 SELECT
中不执行向变量赋值,它只是 returns 向 CASE
赋值。
试试这个...
SELECT @l_start_date =
CASE
WHEN @l_max_date is null THEN
CASE
WHEN sched_interval_type = 'DAYS' AND @l_max_date IS NULL AND @NextDayID IS NOT NULL THEN DATEADD(DAY, (DATEDIFF(DAY, ((@NextDayID + 5) % 7),@p_date) / 7) * 7 + 7, ((@NextDayID + 5) % 7))
ELSE CONVERT(DATETIME, CONVERT(DATE, @p_date))
END
ELSE
CASE
WHEN sched_interval_type = 'DAYS' AND @l_max_date IS NULL AND @NextDayID IS NOT NULL THEN DATEADD(DAY, (DATEDIFF(DAY, ((@NextDayID + 5) % 7), [dbo].[greatest](@l_max_date, @p_date)) / 7) * 7 + 7, ((@NextDayID + 5) % 7))
ELSE @l_max_date+ SCHED_AVAIL_DAYS
END
END
FROM #l_rec