Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign a list of integers to an @var

Tags:

tsql

I can:

declare @idOrder int
set @idOrder = 21319

I want:

declare @idOrder int
set @idOrder = (21319, 21320)

for use in a series of statements where the 'WHERE' clause uses the IN operator

delete Orders
where idOrder in @idOrder

instead of

delete Orders
where idOrder in (21319, 21320)
like image 786
justSteve Avatar asked Jan 23 '23 07:01

justSteve


1 Answers

You can't do that as long as it's an int, as that's not a valid value for that datatype. A datatype that could take several integers is a table

declare @idOrder table (id int)

insert into @idOrder values(21319)
insert into @idOrder values(21320)

delete from Orders where idOrder in (select id from @idOrder)
like image 132
David Hedlund Avatar answered Feb 14 '23 02:02

David Hedlund