必须在 case 语句中声明标量变量 date

Must declare the scalar variable date in case satatement

我正在尝试使用一年中几个月的当前时间段更新列。因此,我使用带有月份变量的 switch 语句。但是,我收到错误:

必须声明标量变量

我试过以下方法:

DECLARE @DATE_AP dateTime
DECLARE @month varchar(max)

SET @DATE_AP= DATEADD(year, 0, GETDATE())
SET @month_AP = DATENAME(Month, @DATE_AP)
SELECT CAST(@month as VARchar(10))

 select @periodsetvar = 'select CASE @month 

     when    ''October'' then 
     ''Update tbltimes set Periodyr = 01''

     when    ''November'' then 
     ''Update tbltimes set Periodyr  = 02''

     when    ''December'' then 
    ''Update tbltimes set Periodyr  = 03''

     when    ''January'' then 
    ''Update tbltimes set Periodyr  = 04''

     when    ''February'' then 
     ''Update tbltimes set Periodyr  = 05''

END'

exec (@periodsetvar)

和:

DECLARE @DATE_AP dateTime
DECLARE @month varchar(max)

SET @DATE_AP= DATEADD(year, 0, GETDATE())
SET @month_AP = DATENAME(Month, @DATE_AP)
SELECT CAST(@month as VARchar(10))

 select @periodsetvar = 'select CASE'+   ' '+ @month+'

     when    ''October'' then 
     ''Update tbltimes set Periodyr = 01''

     when    ''November'' then 
     ''Update tbltimes set Periodyr  = 02''

     when    ''December'' then 
    ''Update tbltimes set Periodyr  = 03''

     when    ''January'' then 
    ''Update tbltimes set Periodyr  = 04''

     when    ''February'' then 
     ''Update tbltimes set Periodyr  = 05''

END'

exec (@periodsetvar)

您需要为所有变量添加声明:

DECLARE @DATE_AP dateTime
DECLARE @month varchar(max)
DECLARE @month_AP varchar(max)
DECLARE @periodsetvar varchar(max)

SET @DATE_AP= DATEADD(year, 0, GETDATE())
SET @month_AP = DATENAME(Month, @DATE_AP)
SELECT CAST(@month as VARchar(10))

select @periodsetvar = 'select CASE'+   ' '+ @month+'

 when    ''October'' then 
 ''Update tbltimes set Periodyr = 01''

 when    ''November'' then 
 ''Update tbltimes set Periodyr  = 02''

 when    ''December'' then 
''Update tbltimes set Periodyr  = 03''

 when    ''January'' then 
''Update tbltimes set Periodyr  = 04''

 when    ''February'' then 
 ''Update tbltimes set Periodyr  = 05''

END'

exec (@periodsetvar)

不要使用易受 SQL 注入影响的动态 SQL,而是使用像这样的简单语句:

DECLARE @DATE_AP dateTime
Declare @periodsetvar char(2)

SET @DATE_AP= DATEADD(year, 0, GETDATE())

Set @periodsetvar =  CASE DATENAME(Month, @DATE_AP) 
                        when 'October' then '01'
                        when 'November' then '02'
                        when 'December' then '03'
                        when 'Janurary' then '04'
                        when 'February' then '05'
                     end
Update tbltimes 
set Periodyr  = @periodsetvar

如果您 100% 决定使用动态 SQL 那么您应该确保这样做更安全并且参数化如下:

DECLARE @DATE_AP dateTime
DECLARE @month varchar(10)

SET @DATE_AP= DATEADD(year, 0, GETDATE())
SET @month = DATENAME(Month, @DATE_AP)

 select @periodsetvar = 'select CASE @month 

     when    ''October'' then 
     ''Update tbltimes set Periodyr = 01''

     when    ''November'' then 
     ''Update tbltimes set Periodyr  = 02''

     when    ''December'' then 
    ''Update tbltimes set Periodyr  = 03''

     when    ''January'' then 
    ''Update tbltimes set Periodyr  = 04''

     when    ''February'' then 
     ''Update tbltimes set Periodyr  = 05''

END'

exec sp_executesql @periodsetvar, N'@month varchar(10)', @month

sp_executesql 将允许您在动态 sql 中声明参数,使其执行起来更加安全。

如果您本质上想使用动态 SQL,您需要在动态查询中声明您的 @month 变量:

DECLARE 
    @DATE_AP dateTime,
    @month_AP varchar(max),
    @periodsetvar varchar(max)

SET @DATE_AP= DATEADD(year, 0, GETDATE())
SET @month_AP= DATENAME(Month, @DATE_AP)

SELECT @periodsetvar =
 'declare @month varchar(max)
  set @month = ''' + @month_AP+ '''

    select 
        CASE @month 
            when ''October'' then ''Update tbltimes set Periodyr = 01''
            when ''November'' then ''Update tbltimes set Periodyr  = 02''
            when ''December'' then ''Update tbltimes set Periodyr  = 03''
            when ''January'' then ''Update tbltimes set Periodyr  = 04''
            when ''February'' then ''Update tbltimes set Periodyr  = 05''

END'

exec (@periodsetvar)