如果语法错误,SQL嵌套if else

时间:2021-06-12 15:41:58
declare @timeid int

if(datename(Dw,getdate())='Monday')
begin
set @timeid=3
end
Else if(datename(Dw,getdate())='Sunday' or datename(Dw,getdate())='Saturday')
begin
  set @timeId=2
end

ELSE   -- for Tuesday to Friday

begin
 if(convert(varchar(11),getdate(),108)<='08:30:00')
  begin
   set @timeId=1
  end
 else
  begin
   set @timeId=0
  end
end

select @timeid

Error is being thrown:

错误被抛出:

Msg 156, Level 15, State 1, Line 13
Incorrect syntax near the keyword 'Else'.
Msg 102, Level 15, State 1, Line 32
Incorrect syntax near '@timeid'.

Msg 156,Level 15,State 1,Line 13关键字'Else'附近的语法不正确。消息102,级别15,状态1,行32'@ timeid'附近的语法不正确。

Please help.

请帮忙。

2 个解决方案

#1


3  

OK, I'm pretty sure your syntax error is the varchar(11) should be varchar.

好的,我很确定你的语法错误是varchar(11)应该是varchar。

Notwithstanding, this is not good use of SQL; first, you use getdate() 3 times - each time will be different (by milliseconds) this could mean ticking over from Friday to Saturday between the first and last call, or from before 08:30 to after 08:30.

尽管如此,这并不能很好地利用SQL;首先,你使用getdate()3次 - 每次都会有所不同(毫秒),这可能意味着在第一次和最后一次通话之间,或者从08:30之前到08:30之间,从周五到周六。

Try this:

尝试这个:

declare @timeid int
declare @nowtime datetime

select @nowtime=getdate()

select @timeid = CASE datename(Dw,@nowtime)
                   WHEN 'Monday' THEN 3
                   WHEN 'Sunday' THEN 2
                   WHEN 'Saturday' THEN 2
                   ELSE
                     CASE 
                       WHEN convert(varchar,getdate(),108)<='08:30:00' THEN 1
                       ELSE 0
                     END
                 END

#2


2  

@DaleM beat me to it, but yeah, here's another rewrite for clarity:

@DaleM打败了我,但是,是的,这是为了清晰起见的另一个重写:

DECLARE @DOW_SUNDAY int
DECLARE @DOW_MONDAY int
DECLARE @DOW_SATURDAY int

SET @DOW_SUNDAY = 1
SET @DOW_MONDAY = 2
SET @DOW_SATURDAY = 7

DECLARE @now datetime
DECLARE @day_of_week int

SET @now = GETDATE()
SET @day_of_week = DATEPART(DW, @now)

SELECT CASE WHEN @day_of_week = @DOW_MONDAY
            THEN 3
            WHEN @day_of_week IN (@DOW_SATURDAY, @DOW_SUNDAY)
            THEN 2
            WHEN CONVERT(varchar, @now, 108) <= '08:30:00'
            THEN 1
            ELSE 0
        END

#1


3  

OK, I'm pretty sure your syntax error is the varchar(11) should be varchar.

好的,我很确定你的语法错误是varchar(11)应该是varchar。

Notwithstanding, this is not good use of SQL; first, you use getdate() 3 times - each time will be different (by milliseconds) this could mean ticking over from Friday to Saturday between the first and last call, or from before 08:30 to after 08:30.

尽管如此,这并不能很好地利用SQL;首先,你使用getdate()3次 - 每次都会有所不同(毫秒),这可能意味着在第一次和最后一次通话之间,或者从08:30之前到08:30之间,从周五到周六。

Try this:

尝试这个:

declare @timeid int
declare @nowtime datetime

select @nowtime=getdate()

select @timeid = CASE datename(Dw,@nowtime)
                   WHEN 'Monday' THEN 3
                   WHEN 'Sunday' THEN 2
                   WHEN 'Saturday' THEN 2
                   ELSE
                     CASE 
                       WHEN convert(varchar,getdate(),108)<='08:30:00' THEN 1
                       ELSE 0
                     END
                 END

#2


2  

@DaleM beat me to it, but yeah, here's another rewrite for clarity:

@DaleM打败了我,但是,是的,这是为了清晰起见的另一个重写:

DECLARE @DOW_SUNDAY int
DECLARE @DOW_MONDAY int
DECLARE @DOW_SATURDAY int

SET @DOW_SUNDAY = 1
SET @DOW_MONDAY = 2
SET @DOW_SATURDAY = 7

DECLARE @now datetime
DECLARE @day_of_week int

SET @now = GETDATE()
SET @day_of_week = DATEPART(DW, @now)

SELECT CASE WHEN @day_of_week = @DOW_MONDAY
            THEN 3
            WHEN @day_of_week IN (@DOW_SATURDAY, @DOW_SUNDAY)
            THEN 2
            WHEN CONVERT(varchar, @now, 108) <= '08:30:00'
            THEN 1
            ELSE 0
        END