How to use SQL SERVER's OpenQuery

1. OpenQuery usage instructions

  Execute the specified pass-through query on the specified linked server. The server is an OLE DB data source. OPENQUERY can be referenced in the FROM clause of a query as if it were a table name. OPENQUERY can also be referenced as the target table of an INSERT, UPDATE, or DELETE statement. But this depends on the capabilities of the OLE DB provider. Although a query may return multiple result sets, OPENQUERY returns only the first one.

2. OpenQuery syntax

OPENQUERY ( linked_server ,'query' ) 

2.1 Parameter description

linked_server
represents the identifier of the linked server name.
Insert image description here
How to configure the link can refer to my previous documents: How to add a mysql linked server to the SQL Server database (Windows system) and how to add an Oracle linked server to the SQL Server database (Windows system)

'query'
A query string to be executed in the linked server. The maximum length of this string is 8 KB.

2.2 Notes

OPENQUERY does not accept a variable for its parameters.

OPENQUERY cannot be used to execute extended stored procedures against linked servers. However, extended stored procedures can be executed on linked servers by using four-part names. For example:

EXEC SeattleSales.master.dbo.xp_msver  

Any call to OPENDATASOURCE, OPENQUERY, or OPENROWSET in the FROM clause is evaluated separately from any call to these functions used as update targets, even if the parameters supplied to both calls are the same. Specifically, filters or join conditions applied to the results of any one of the above calls do not affect the results of the other calls.

3. Example

3.1 Execute SELECT pass query

The following example uses the SELECT pass query to select data for example id = 1

SELECT * FROM OPENQUERY(MySQLTest,'SELECT * FROM test_table WHERE id = ''1''')

Insert image description here

3.2 Execute UPDATE delivery query

The following example uses an UPDATE pass query for MySQLTest, the linked server name created in the example.
Insert image description here
The above error occurred when my environment was executed, and the reason has not yet been found.

3.3 Execute INSERT pass query

The following example uses an INSERT pass-through query for MySQLTest, the linked server name created in the example.
Insert image description here

3.4 Execute DELETE pass query

The following example uses a DELETE pass query for MySQLTest, the linked server name created in the example.
Insert image description here

The above error occurred when my environment was executed, and the reason has not yet been found.

Guess you like

Origin blog.csdn.net/slb190623/article/details/132782364