SQL use comma-separated values with IN clause

前端 未结 9 1722
终归单人心
终归单人心 2020-12-19 08:49

I am developing an ASP.NET application and passing a string value like \"1,2,3,4\" into a procedure to select those values which are IN (1,2,3,4) but its saying \"Conversion

相关标签:
9条回答
  • 2020-12-19 09:02

    If you dont want to use dynamic sql, the best way ive found is to create a function which turns a delimited string into a table, something like this works for an Integer list:

    CREATE FUNCTION [dbo].[StringToIntList]
    (@str VARCHAR (MAX), @delimeter CHAR (1))
    RETURNS 
        @result TABLE (
            [ID] INT NULL)
    AS
    BEGIN
    
        DECLARE @x XML 
        SET @x = '<t>' + REPLACE(@str, @delimeter, '</t><t>') + '</t>'
    
        INSERT INTO @result
        SELECT DISTINCT x.i.value('.', 'int') AS token
        FROM @x.nodes('//t') x(i)
        ORDER BY 1
    
    RETURN
    END
    

    Then use that in your sp:

    CREATE Procedure [dbo].[sp_getUserRoles](
       @pGroupIDs varchar(50)
        )
         As
        BEGIN
            SELECT * FROM CheckList_Groups Where id in (
               SELECT ID FROM dbo.StringToIntList(@pGroupIds,',')
           )
       End
    
    0 讨论(0)
  • 2020-12-19 09:02

    Sure it can't do that,

    The generated query would be sth like this

    SELECT * FROM CheckList_Groups Where id in ('1,2,3,4')
    

    and sure it can't be executed.

    you can build the query in your stored procedure then execute it with exec

    'SELECT * FROM CheckList_Groups Where id in (' + @pGroupIDs + ')'
    

    or

    SELECT * FROM CheckList_Groups Where charindex(','+id+',' , @pGroupIDs)>0
    

    but you first must add the ',' to start and end of your parameter in your c# code

    0 讨论(0)
  • 2020-12-19 09:02
    DECLARE @TagId  NVARCHAR(100)  = '43,81'
    
    SELECT * FROM TABLE WHERE TagId IN (SELECT TRIM(VALUE) FROM  STRING_SPLIT( @TagId , ',' )  )
    

    USE STRING_SPLIT FUNCTION FOR THIS

    0 讨论(0)
  • 2020-12-19 09:04

    The way you are trying to do this is slightly wrong. You will need to use EXECUTE in order to achieve this.

    CREATE PROCEDURE [dbo].[sp_getUserRoles](@pGroupIDs nvarchar(50))
    As
    BEGIN         
        EXECUTE (N'SELECT * FROM CheckList_Groups Where id in (' + @pGroupIDs + ')';
    END 
    
    0 讨论(0)
  • 2020-12-19 09:08

    The IN clause can't take a bound parameter like that. What it's being given when the query is actually created is SELECT * FROM CheckList_Groups Where id in ('1,2,3,4'). Essentially the IN clause is being passed a single string.

    0 讨论(0)
  • 2020-12-19 09:15

    You need to use SP_executesql to achieve this functionllity

    CREATE Procedure [dbo].[sp_getUserRoles](
       @pGroupIDs varchar(50)
        )
         As
        BEGIN
    
    EXECUTE sp_executesql 
              N'SELECT * FROM CheckList_Groups Where id in (@pGroupIDs)',
              N'@level varchar(50)',
              @level = @pGroupIDs;
    
     End
    
    0 讨论(0)
提交回复
热议问题