SQL Server – In clause with a declared variable [duplicate]

This is an example where I use the table variable to list multiple
values in an IN clause. The obvious reason is to be able to change
the list of values only one place in a long procedure.

To make it even more dynamic and alowing user input, I suggest
declaring a varchar variable for the input, and then using a WHILE to
loop trough the data in the variable and insert it into the table
variable.

Replace @your_list, Your_table and the values with real stuff.

DECLARE @your_list TABLE (list varchar(25)) 
INSERT into @your_list
VALUES ('value1'),('value2376')

SELECT *  
FROM your_table 
WHERE your_column in ( select list from @your_list )

The select statement abowe will do the same as:

SELECT *  
FROM your_table 
WHERE your_column in ('value','value2376' )

Leave a Comment

tech