0

How do i select a few rows in SQL using the WHERE IN and the set of numbers is an input variable

I want to do the following

select datacolumn from datatable
where primarykey in (1, 2, 3)

I tried the following procedure but it doesn't work

declare @ids NVARCHAR(MAX)

select datacolumn from datatable
where primarykey in (@ids)

I use Sql Server 2008

MarsOne
  • 2,155
  • 5
  • 29
  • 53
  • 5
    IS your `@IDs` comma separated string ? If that's the case then you should follow [this blog](http://sqlperformance.com/2012/07/t-sql-queries/split-strings). This is all you want to know about your issue. – Mahesh Mar 19 '15 at 13:02
  • 1
    This is a duplicate of [this question](http://stackoverflow.com/q/337704/1048425), **However**, I think the most comprehensive answer on this comes in this article [Splitting Strings : Now with less T-SQL](http://sqlperformance.com/2012/08/t-sql-queries/splitting-strings-now-with-less-t-sql). which follows on from [Split strings the right way – or the next best way](http://sqlperformance.com/2012/07/t-sql-queries/split-strings) and [Splitting Strings : A Follow-Up](http://sqlperformance.com/2012/08/t-sql-queries/splitting-strings-follow-up). – GarethD Mar 19 '15 at 13:08
  • Yeah. The top voted comment helped me out more than the marking as duplicate... – MarsOne Mar 19 '15 at 14:02

0 Answers0