variable stuff | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

variable stuff

Is there a way to set a whole line into a variable? For example, could I do something like this? Instead of Select * from TableName where ColumnName = ‘Stuff’ or
ColumnName = ‘Stuff2’ or ColumnName = ‘More Stuff’ We could type: P = ColumnName = ‘Stuff’ or ColumnName = ‘Stuff2’ or ColumnName = ‘More Stuff’
Select * from TableName where ColumnName = P
Thanks, Ben
No, but you could use IN Select * from TableName where ColumnName in (‘Stuff’, ‘Stuff2, ‘More Stuff’) But if you want the part on the right hand side of IN to be dynamic then probably a table variable would be best. Create the table variable,
insert the values,
select * from X where columnname in (select columnname from @tablevariable) Dave Hilditch
Thank you. Ben
Dave already mentioned dynamic SQL. The whole rest you can find herehttp://www.sommarskog.se/dynamic_sql.html The article also explains why your idea isn’t the best one.
–Frank
http://www.insidesql.de

]]>