I have the following stored procedure:
CREATE Procedure [dbo].[sc_Server_Link]
@ServerName varchar(255),
@ServerSource varchar(255),
@DatabaseName varchar(255),
@UserName varchar(255),
@UserPass varchar(255)
AS
BEGIN
IF NOT EXISTS ( SELECT 1 FROM sys.sysservers WHERE srvname = @ServerName )
BEGIN
EXEC sp_addlinkedserver
@server = @ServerName,
@srvproduct = '',
@provider = 'sqlncli',
@datasrc = @ServerSource,
@location = '',
@provstr = '',
@catalog = @DatabaseName
--Add credentials and options to this linked server
IF NOT EXISTS ( SELECT 1 FROM sys.linked_logins WHERE remote_name = @UserName)
EXEC sp_addlinkedsrvlogin
@rmtsrvname = @ServerName,
@useself = 'false',
@rmtuser = @UserName,
@rmtpassword = @UserPass
--Set Options
EXEC sp_serveroption @ServerName, 'rpc out', true;
END
END
Executing the procedure works fine but afterwards, every time I try to do a transaction... the transaction works but I get the following errors:
Msg 15002, Level 16, State 1, Procedure sp_addlinkedserver, Line 71
The procedure 'sys.sp_addlinkedserver' cannot be executed within a transaction.Msg 15002, Level 16, State 1, Procedure sp_addlinkedsrvlogin, Line 35
The procedure 'sys.sp_addlinkedserver' cannot be executed within a transaction.Msg 15002, Level 16, State 1, Procedure sp_serveroption, Line 37
The procedure 'sys.sp_addlinkedserver' cannot be executed within a transaction.
- How can I stop these errors?
- How can I correct my stored procedure so this won't happen again?
Thanks for your help!