sql server 2008 - Trouble with the IN keyword in transact sql -


this question has answer here:

i creating script querying table using in keyword. when type data inside in clause, query performs should. when create variable exact same data in , use variable inside in clause, not. ideas???

here query works

select *  scpcommandeventlog  messageid = 3    , param1 in('11416407','11416410','11416413','11416417',                     '11416419','11416421','11416423','11416427',                 '11416432','11416433','11416434','11416435',                 '11416438','11416443','11416446','11416448',                 '11416451','11416454','11416458','11416462') 

here query doesn't

select * scpcommandeventlog messageid = 3 , param1 in(@list) 

here query populates @list variable

declare @list varchar(max)  select @list = isnull(@list + ',', '') + '''' + cast(itemid varchar) + '''' dbo.itemlist sortid @sortid 

you can't put parameters in query in single variable, need parameter per value in;

select *  scpcommandeventlog  messageid = 3 , param1 in(@p1,@p2,@p3,...) 

you build query using dynamic sql, better way in example may not build list @ , in single query;

select *  scpcommandeventlog  messageid = 3 , param1 in (     select itemid      dbo.itemlist     sortid @sortid ) 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -