SQL use comma-separated values with IN clause

前端 未结 9 1776
终归单人心
终归单人心 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

    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

提交回复
热议问题