I have a stored procedure that I need to convert so that it reads a linked server variable from a table. From what I know, the only way to do so is to use dynamic SQL. The problem is that I'm failing to convert my query.
Original query:
SET @var1 = ''
SELECT @var1 = RECEIVER
FROM databse1.dbo.table1
WHERE SAPNUMBER = @var2
Converted query:
SET @srv = (SELECT server_name
FROM Configuration.dbo.Server_Switch)
SET @var1 = ''
exec (
'SELECT ' + @var1 + '= RECEIVER
FROM ' + @srv + '.databse1.dbo.table1
WHERE SAPNUMBER =' + @var2
)
The error that I'm getting is:
Incorrect syntax near '='
@var1 and @var2 are variables declared beforehand. I'm pretty sure there's a problem in assigning values to these variables in dynamic SQL, hence the '=' error. Can I get some help in converting this query?
Bobby Tablesin thatServer_Switchtable ....