use [FirstSQLDatabase]; go declare @cSecondDb as Varchar(100); declare @sql as varchar(max); set @cSecondDb = 'SecondSQLDatabase'; set @sql = 'select * from ' + @cSecondDb + '.dbo.mytable'; exec (@sql);and replace dbo with the appropriate schema name if it's not dbo.
>use [FirstSQLDatabase] >go >select * from SecondSQLDatabase..mytable where 1=1 >>
>use [FirstSQLDatabase] >go >declare @cSecondDb as Varchar(100) >set @cSecondDb = 'SecondSQLDatabase' >select * from @cSecondDb..mytable where 1=1 -- this is the line that causes the syntax error. >>