I'm trying to execute code against linked server which is coming from a parameter. Below is my code. However I get an error:
'Could not find server 'P_LINKED_SERVER'.
How does one execute against server which is coming from a parameter?
DECLARE @P_LINKED_SERVER AS VARCHAR(30)
SET @LINKED_SERVER = (SELECT DSS_PARAMETER_VALUE FROM [DBO].DSS_PARAMETER WHERE DSS_PARAMETER_NAME = 'Server Name')
DECLARE @sql AS VARCHAR(max)
SET @sql = 'SQL CODE'
EXEC (@sql) AT [@P_LINKED_SERVER];
Many Thanks
The linked server name has to be part of the complete statement and cannot be a parameter. So:
DECLARE @P_LINKED_SERVER AS VARCHAR(30);
SELECT @LINKED_SERVER = DSS_PARAMETER_VALUE
FROM [DBO].DSS_PARAMETER
WHERE DSS_PARAMETER_NAME = 'Server Name';
DECLARE @sql AS NVARCHAR(max);
SET @sql = CONCAT('EXEC SQL CODE AT ', @LINKED_SERVER);
EXEC(@sql);
In your case, you seem to want:
SET @sql = CONCAT('EXEC ''DELETE FROM tablename'' AT ', @LINKED_SERVER);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.