SQL Server procedure declare a list

后端 未结 6 669
粉色の甜心
粉色の甜心 2020-12-24 05:28

My SQL code is fairly simple. I\'m trying to select some data from a database like this:

SELECT * FROM DBTable
WHERE id IN (1,2,5,7,10)

I w

6条回答
  •  误落风尘
    2020-12-24 05:41

    That is not possible with a normal query since the in clause needs separate values and not a single value containing a comma separated list. One solution would be a dynamic query

    declare @myList varchar(100)
    set @myList = '(1,2,5,7,10)'
    exec('select * from DBTable where id IN ' + @myList)
    

提交回复
热议问题