Using a comma-separated parameter in an IN clause

后端 未结 5 1966
余生分开走
余生分开走 2021-01-05 11:34

I have \'param1, param2, parma3\' coming from SSRS to a stored procedure as a varchar parameter: I need to use it in a query\'s IN cla

5条回答
  •  伪装坚强ぢ
    2021-01-05 12:24

    you can use split function and use it as in following way here my split fnSplitString return splitdata

    select * from tb1 where id in(select splitdata from dbo.fnSplitString((select col1 from tb12 where id=3),','))
    
    
    create FUNCTION [dbo].[fnSplitString] 
    ( 
        @string NVARCHAR(MAX), 
        @delimiter CHAR(1) 
    ) 
    RETURNS @output TABLE(splitdata NVARCHAR(MAX) 
    ) 
    BEGIN 
        DECLARE @start INT, @end INT 
        SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
        WHILE @start < LEN(@string) + 1 BEGIN 
            IF @end = 0  
                SET @end = LEN(@string) + 1
    
            INSERT INTO @output(splitdata)  
            VALUES(SUBSTRING(@string, @start, @end - @start)) 
            SET @start = @end + 1 
            SET @end = CHARINDEX(@delimiter, @string, @start)
    
        END 
        RETURN 
    END
    

提交回复
热议问题