sql - Declare and set a variable with more than one possible value -


is possible set variable have more 1 value?

in example below, set @variable , use store numbers 1,4,7 , 12

and use variable in where statment.

the below purely example see if possible or not

declare @variable int;  set @variable = in(1,4,7,12)  select * dbo.euactivestores eu eu.[store no] = @variable 

any advice on if possible, or similar great.

i believe making more dynamic query

use table variable here

declare @tbl table(v int); insert @tbl values(1),(4),(7),(12); 

you can use in where clause here:

where eu.[store no] in(select v @tbl); 

fully working example:

declare @tbl table(v int); insert @tbl values(1),(4),(7),(12);  declare @mockup table(id int identity,somevalue varchar(100)); insert @mockup values('val 1'),('val 2'),('val 3'),('val 4'),('val 5')                          ,('val 6'),('val 7'),('val 8'),('val 9'),('val 10')                          ,('val 11'),('val 12'),('val 13'),('val 14'),('val 15'); 

--only values table

select * @mockup m m.id in(select v @tbl); 

--or negation of above

select * @mockup m m.id not in(select v @tbl); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -