SQL - using a variable for an IN clause

后端 未结 7 1394
醉话见心
醉话见心 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:12

    CREATE FUNCTION [dbo].[func_ParseStringToTable] (@stringIN varchar(2000)) 
    RETURNS @tOUT TABLE(RoomID int) AS
    BEGIN
    DECLARE @pos int
    SET @pos=CHARINDEX(',',@StringIN)
    WHILE @pos>0
         BEGIN
         INSERT @tOUT(RoomID) SELECT LEFT(@StringIN,CHARINDEX(',',@StringIN)-1)
         SET @stringIN = SUBSTRING(@StringIN,CHARINDEX(',',@StringIN)+1,LEN(@StringIN))
         SET @pos=CHARINDEX(',',@StringIN)
         END
    IF LEN(@StringIN)>0
         BEGIN
         INSERT @tOUT(RoomID) SELECT @StringIN
         END
    RETURN 
    END
    

    usage...

    SELECT * FROM table WHERE id IN (func_ParseStringToTable(@ids))
    

提交回复
热议问题