I am trying to run the below statement, which if I print the statement it shows the full on statement, but when I use the Exec command it throws an error?
Declare @sql varchar(4000), @FQN varchar(4000) Create Table #b ( acaca varchar(20) ,ramalad varchar(4000) ) Insert Into #b Values ('23', 'server.database.dbo.table'), ('44', 'server.database.dbo.table') Set @FQN = (Select ramalad from #b where acaca = '44') Set @Sql = 'Select * from '+@FQN+' ' Print @SQL Exec @Sql
Printing my statement gives me Select * from server.database.dbo.table
However when it reaches the Exec statement it gives me an error of belwo. Yes this is a linked server, but if I run the select statement direct it produces the results error free. Why does the inline present an issue?
Msg 7202, Level 11, State 2, Line 7 Could not find server 'Select * from server' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.
- Edited by Dwarf Goat 12 hours 57 minutes ago