SQL use comma-separated values with IN clause

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

    Here is a workaround I found to do what you are trying to achieve

    CREATE Procedure [dbo].[sp_getUserRoles](
       @pGroupIDs varchar(50)
        )
         As
        BEGIN
            SELECT * FROM CheckList_Groups Where (',' + @pGroupIDs +',' LIKE '%,' + CONVERT(VARCHAR, id) + ',%')
       End
    

    This gets your comma delimited list and compares it to the id's(which are represented like so ',1,', ',2,' etc) in the table using LIKE

提交回复
热议问题