Sunday, June 10, 2018

OPENQUERY - Run SQL's on/against another database



Executes the specified pass-through query on the specified linked server. 
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. 
Although the query may return multiple result sets, OPENQUERY returns only the first one.


Syntax

OPENQUERY ( linked_server ,'query' )  


Executing a SELECT pass-through query

The following example uses a pass-through SELECT query to select the row inserted in example C.
SQL
SELECT * FROM OPENQUERY (OracleSvr, 'SELECT name FROM joe.titles WHERE name = ''NewTitle''');  

Recently Executed queries

 SELECT     txt.TEXT AS [SQL Statement],     qs.EXECUTION_COUNT [No. Times Executed],     qs.LAST_EXECUTION_TIME AS [Last Time Executed],   ...