I need to execute three dynamic SQL statements synchronously on a linked server (SQL Server 2005) like this:
declare @statement nvarchar(max);
set @statement = 'exec ' + @server_name + '.' + @database_name + '.dbo.Foo;exec ' + @server_name + '.' + @database_name + '.dbo.Bar;exec ' + @server_name + '.' + @database_name + '.dbo.BigTime';
exec sp_executesql @statement;
To test this, I have a linked server setup that links to itself.
When executing this local (by removing the @server_name
), using SQL Profiler I see that it executes just fine as one statement. However, when I execute this via the linked server (using the @server_name
variable), I see in SQL Profiler that each statement gets executed separately, with sp_reset_connection
getting executed after each one.
The trace looks something like this:
Audit Login ....
exec database.dbo.Foo
exec sp_reset_connection
Audit Logout
Audit Login ....
exec database.dbo.Bar
exec sp_reset_connection
Audit Logout
Audit Login ....
exec database.dbo.BigTime
exec sp_reset_connection
Audit Logout
This is causing me problems. Is there any why I can specify to SQL Server to not call sp_reset_connection
between statements?