SELECT from table WHERE items IN (@variable)




You need to make query like "SELECT * from tbl WHERE items IN ('item1', 'item2', 'item3', 'item4')" and you want to send items to IN clause as variable. It’s nice when, for example, you want to execute Stored procedure or, as in example below, use INSERT ... SELECT construction. Then you need to make a fint with quotes.

Below I write an SQL code of my solution.

 Variables:

@Items – parameter for IN, for example ('item1', 'item2', 'item3', 'item4')

@WhereClause – variable with escaped quotes for correct SQL-query, one quote should be escaped with three ones

 

I created a varchar(max) variable @sql, set value for it and added to SQL-query

 



MarkiMarta.com. Notes of web-specialist
Since 2009
18+
Nice way to select all the child elements in SQL

  The task is to select all the records where parent equal ‘1’ and parent of parent equal ‘1’ etc....

Close