将查询从PL SQL写入SQL SERVER

时间:2015-01-16 17:19:07

标签: sql-server plsql

我有这个查询wrtiien 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;    

我尝试将其翻译为SQL SERVER

set @NextDayID =(SELECT DAY_START_DAY FROM #l_rec )
SELECT  CASE WHEN sched_interval_type  = 'DAYS' then         
        CASE WHEN @l_max_date is null then     
        CASE WHEN  @NextDayID is not null then     
           @l_start_date  =  DATEADD(DAY, (DATEDIFF(DAY, ((@NextDayID + 5) % 7), GETDATE()) / 7) * 7 + 7, ((@NextDayID + 5) % 7))
        else    
           @l_start_date =CONVERT(DATETIME, CONVERT(DATE, @p_date))    
        end 

我遇到很多错误,如何纠正错误?

1 个答案:

答案 0 :(得分:0)

试试这个:

DECLARE @NextDayID INT
SELECT @NextDayID = DAY_START_DAY FROM #l_rec 

SELECT @l_start_date 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), GETDATE()) / 7) * 7 + 7, ((@NextDayID + 5) % 7))
          else CONVERT(DATETIME, CONVERT(DATE, @p_date)) end as [start_date]
          from <<someTableName>> --<<--Replace the tag with the table whihc has the column `sched_interval_type`