Monday, March 13, 2006

SQL 2005 - Excute Remotely

Prior to SQL Server 2005 you could execute EXECUTE command only on the local server, with SQL Server 2005 we have AT parameter which can be used for executing the statement on a remote linked server.
Example: Setup a linked server using SP_AddLinkedServer:
-- Add the linked server to the local machine
EXEC sp_addlinkedserver 'SQLSERVER2', 'SQL Server'

--Enable the linked server to allow RPC calls
Exec SP_Serveroption 'SQLSERVER2','RPC OUT',TRUE

-- Now you are ready to execute T-SQL statements across linked servers using AT command
EXEC('Select * from AdventureWorksDW..DatabaseLog') AT SQLSERVER2

No comments: