I want to specify WHERE col1 is in (1,2,3).
However, I have a long list of 1,2,3... and I use it in multiple queries so I want to store it as a variable.
I tried:
DECLARE @list varchar = 1,2,3
or
DECLARE @list varchar = '1','2','3'
I checked a variable type matrix and couldn't find an applicable variable type.
Any idea? I would prefer not to create it as a table as it is only a list and I want to use it as a list and not a table.
No sure what version of sql you are using, but you could store them as XML or JSON. Not really sure what you are using them for, so, as always, it depends
as @ScottPletcher mentioned use tables but if you cannot use the following
DECLARE @list varchar(10) = '1,2,3'
declare @sql4 table(horsename varchar(150), COL1 int)
insert into @sql4
select distinct top 10 name, column_id from sys.all_columns
select horsename, COL1, b.*
from @sql4 as a
join dbo.DelimitedSplit8K(@list,',') as b on a.COL1 = b.ItemNumber
@JeffModen is author of DelimitedSplit8K. Search it online
You should also look into table types
CREATE TYPE UT_Employee AS TABLE
(
Emp_Id int NOT NULL
)