(Component Integration Services only) Establishes a connection to a remote server, passes a query buffer to the remote server from the client, and relays the results back to the client.
sp_remotesql server, query [, query2, ... , query254]
sp_remotesql FREDS_SERVER, "select @@version"
create procedure freds_version as exec sp_remotesql FREDS_SERVER, "select @@version" go exec freds_version go
sp_remotesql DCO_SERVER, "insert into remote_table (numbercol,intcol, floatcol,datecol )", "values (109.26,75, 100E5,'10-AUG-85')" select @@error
declare @servname varchar(30) declare @querybuf varchar(200) select @servname = "DCO_SERV" select @querybuf = "select table_name from all_tables where owner = 'SYS'" exec sp_remotesql @servname, @querybuf
sp_remotesql establishes a connection to a remote server, passes a query buffer to the remote server from the client, and relays the results back to the client. The local server does not intercept results.
You can use sp_remotesql within another stored procedure.
The query buffer parameters must be a character expression with a maximum length of 255 characters. If you use a query buffer that is not char or varchar, you get datatype conversion errors.
sp_remotesql sets the global variable @@error to the value of the last error message returned from the remote server if the severity of the message is greater than 10.
If sp_remotesql is issued from within a transaction, the SAP ASE server verifies that a transaction has been started on the remote server before passing the query buffer for execution. When the transaction terminates, the remote server is directed to commit the transaction. The work performed by the contents of the query buffer is part of the unit of work defined by the transaction.
If transaction control statements are part of the query buffer, it is the responsibility of the client to ensure that the transaction commit and rollback occur as expected. Mixing Transact-SQL with transaction control commands in the query buffer can cause unpredictable results.
The local server manages the connection to the remote server. Embedding connect to or disconnect commands in the query buffer causes results that require interpretation by the remote server. This is not required or recommended. Typically, the result is a syntax error.
See also connect to...disconnect in Reference Manual: Commands.
Any user can execute sp_remotesql. Permission checks do not differ based on the granular permissions settings.
Values in event and extrainfo columns from the sysaudits table are:
Information | Values |
---|---|
Event | 38 |
Audit option | exec_procedure |
Command or access audited | Execution of a procedure |
Information in extrainfo |
|