我们可以在SQL中将Case设置为特定的coloumn

时间:2017-11-21 09:13:49

标签: sql-server

declare @Day int
   declare @Flag nvarchar(2)
   set @Flag='L'
   set @Day=1
      select * from [IIFL_Schedule].dbo.IIFL_Corporate
      where Ticker =@Flag and(
            case when  @day=1 then Day1=1 
                when @day=2 then Day2=1 
                when @day=3 then Day3=1
           end )

这部分给出了语法错误    @ day = 1然后Day1 = 1

的情况
Incorrect syntax near '='.

2 个答案:

答案 0 :(得分:1)

试试吧

declare @Day int
declare @Flag nvarchar(2)
set @Flag='L'
set @Day=1

select *
from [IIFL_Schedule].dbo.IIFL_Corporate
where Ticker=@Flag
  and (
        case
          when @day=1 then Day1
          when @day=2 then Day2
          when @day=3 then Day3
        end = 1 -- !!!
      )

或者

declare @Day int
declare @Flag nvarchar(2)
set @Flag='L'
set @Day=1

select *
from [IIFL_Schedule].dbo.IIFL_Corporate
where Ticker=@Flag
  and (
        case @day -- !!!
          when 1 then Day1
          when 2 then Day2
          when 3 then Day3
        end = 1 -- !!!
      )

答案 1 :(得分:0)

尝试以下查询

declare @Day int
   declare @Flag nvarchar(2)
   set @Flag='L'
   set @Day=2
      select * from [IIFL_Schedule].dbo.IIFL_Corporate
      where Ticker =@Flag and 1 =  

      (
            case when  @day=1 then Day1
                when @day=2 then Day2
                when @day=3 then Day3
           end )