SQL - using a variable for an IN clause

后端 未结 7 1382
醉话见心
醉话见心 2021-01-21 09:50

I wish to do something like the following:

declare @FrameNumber nvarchar(20)
set @FrameNumber = \'(p1, p2)\'

select from myTable where c1 in @FrameNumber


        
相关标签:
7条回答
  • 2021-01-21 10:16

    final solution:

    DECLARE @FrameNumbers TABLE (FrameNumber NVARCHAR(20) PRIMARY KEY)
    
    DECLARE @pos int
    SET @pos=CHARINDEX(',',@FrameNumber)
    WHILE @pos>0 BEGIN
         INSERT @FrameNumbers SELECT LEFT(@FrameNumber,CHARINDEX(',',@FrameNumber)-1)
         SET @FrameNumber = SUBSTRING(@FrameNumber,CHARINDEX(',',@FrameNumber)+1,LEN(@FrameNumber))
         SET @pos=CHARINDEX(',',@FrameNumber)
    END
    IF LEN(@FrameNumber)>0 BEGIN
         INSERT @FrameNumbers SELECT @FrameNumber
    END
    
    select from myTable where c1 in (select FrameNumber from @FrameNumbers)
    

    thanks Quassnoi and Sam, this solution is just a combination of your solutions.

    0 讨论(0)
提交回复
热议问题