将查询从 PL SQL 写入 SQL SERVER

Write a query from PLSQL to SQ 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;    

我试着把它翻译成SQL服务器

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 

我有很多错误,如何改正?

试试这个:

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`