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
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, April 09, 2014 4:04 AM
Points: 27, Visits: 321
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



Group: General Forum Members
Last Login: 2 days ago @ 1:16 AM
Points: 2,826, Visits: 2,836
Hi - you've got some missing brackets here. You can either use:-

exec (@SQL)


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