OpenQuery usage

Executes the specified pass-through query on the specified linked server. This 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. This is subject to the capabilities of the OLE DB provider. Although the query may return multiple result sets, OPENQUERY returns only the first one.

 

Openquery 函数是不确定性的函数,就是说,对于相同的输入值,不保证每次返回的值都是相同的。

All rowset functions are nondeterministic. This means these functions do not always return the same results every time they are called, even with the same set of input values.

 

一,Syntax

OPENQUERY ( linked_server ,'query' )

Any call to OPENDATASOURCE, OPENQUERY, or OPENROWSET in the FROM clause is evaluated separately and independently from any call to these functions used as the target of the update, even if identical arguments are supplied to the two calls. In particular, filter or join conditions applied on the result of one of those calls have no effect on the results of the other.

 

二,Examples

1,Executing an UPDATE pass-through query

UPDATE OPENQUERY (OracleSvr, 'SELECT name FROM joe.titles WHERE id = 101') 
SET name = 'ADifferentName';

2,Executing an INSERT pass-through query

INSERT OPENQUERY (OracleSvr, 'SELECT name FROM joe.titles')
VALUES ('NewTitle');

3,Executing a DELETE pass-through query

DELETE OPENQUERY (OracleSvr, 'SELECT name FROM joe.titles WHERE name = ''NewTitle''');

 

参考文档: MSDN OPENQUERY (Transact-SQL)

你可能感兴趣的:(OpenQuery usage)