Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Needed Help Expand / Collapse
Author
Message
Posted Friday, May 10, 2013 1:10 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, May 21, 2014 3:53 AM
Points: 27, Visits: 329
declare @LinkedServer varchar(15)
declare @DatabaseName varchar(20)
declare @SQL varchar(250)
set @LinkedServer = '[SURE]'
set @DatabaseName = 'master'
--select * from @LinkedServer.@DatabaseName.sys.databases
set @SQL = 'select * from '+@LinkedServer+'.'+@DatabaseName+'.sys.databases'
print @SQL
exec @SQL

---- Result
select * from [SURE].master.sys.databases
Msg 203, Level 16, State 2, Line 10
The name 'select * from [SURE].master.sys.databases' is not a valid identifier.


Working Perfectly:
select * from[SURE].master.sys.databases
Post #1451432
Posted Friday, May 10, 2013 1:27 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 1:13 PM
Points: 3,007, Visits: 3,192
Hi - you've got some missing brackets here. You can either use:-

exec (@SQL)

or

exec sp_executesql @SQL

If you use the second method you will have to declare @sql as ntext, nchar or nvarchar


-------------------------------
Posting Data Etiquette - Jeff Moden
Smart way to ask a question

There are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand (the world). There is no such thing as a dumb question. ― Carl Sagan
I would never join a club that would allow me as a member - Groucho Marx
Post #1451441
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse