I am trying to execute a link server within a stored procedure in T-SQL.
However, I am getting the error
The procedure 'sys.sp_addlinkedserver' cannot be executed within a transaction.
This seems obvious enough. But I need to use dynamic SQL to execute 2 queries afterwards, however I am trying to add the link server before executing the queries, so not sure why the error appears.
Here is the code.
@sqlDBName VARCHAR(MAX),
@sqlServerName VARCHAR(MAX)
AS
EXEC sp_addlinkedserver
@sqlServerName,
N'SQL Server';
DECLARE @impUpd VARCHAR(MAX)
SET @impUpd = '
UPDATE Customers SET NAME = 'Name'
FROM Customers c
JOIN ['+@sqlServerName +'].['+@sqlDBName +'].[dbo].[CUSTOMERS2]
..etc
exec(@impUpd)
Any help would be good.
Thanks