using dynamic IN clause in MSSQL

前端 未结 2 1501
小蘑菇
小蘑菇 2020-12-20 20:01

Why the following SQL does not fetch me anything

DECLARE @Status AS VARCHAR(400)
SET @status = \'\'\'Closed\'\',\'\'OPEN\'\'\'
select * from MYTABLE where st         


        
2条回答
  •  一向
    一向 (楼主)
    2020-12-20 20:19

    You can if you want do some dynamic SQL but I think it is not really competitive..

      DECLARE   @Status nVARCHAR(400),
                @SQL nvarchar(500)
    
    SET @status = '''Closed'''+','+'''OPEN'''
    set @SQL = '
    select * from [MYTABLE] where status in('+@status +')'
    
     exec sp_executesql @SQL
    GO
    

提交回复
热议问题