SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Quoting in a @sql help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Ethel
Starting Member

4 Posts

Posted - 06/25/2014 :  09:40:22  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

Germany
8765 Posts

Posted - 06/25/2014 :  09:43:23  Show Profile  Visit webfred's Homepage  Reply with Quote
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 - 06/25/2014 :  10:22:29  Show Profile  Reply with Quote
Nice works, thanks a lot!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000