Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Quoting in a @sql help

Author  Topic 

Ethel
Starting Member

4 Posts

Posted - 2014-06-25 : 09:40:22
Hi,

Not sure title is right but I have a normal query which is roughly:

select * from table where
host in ('machine1', 'machine2')

I want to create a dynamic query where the table name changes but the rest is the same. I'm having trouble working out how to do the IN statement as the quotes cause syntax errors:

SET @sql ' select*from '+@table+' where host in ????? ' exec (@sql)

So the only dynamic bit is the table.

Any help is appreciated!

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2014-06-25 : 09:43:23
Escaping the single quotes with another single quote but not the needed first and last...

SET @sql ' select * from '+@table+' where host in (''machine1'', ''machine2'')'


Too old to Rock'n'Roll too young to die.
Go to Top of Page

Ethel
Starting Member

4 Posts

Posted - 2014-06-25 : 10:22:29
Nice works, thanks a lot!
Go to Top of Page
   

- Advertisement -