Original: Use of OpenQuery in SQL
OpenQuery is a technique that SQL Server uses to interact with other servers, and the Openquery,sql server provides direct access to other database resources.
Other databases exist in the OPENQUERY expression as linked Server. Use Sp_linkedservers to find all linked servers for the current database.
For example, in a SQL Server 2008 database, you must use OPENQUERY to access a SQL Server 2000 database through a linked server.
The OPENQUERY expression can be written like this:
SELECT * from OpenQuery ([LinkedServerName], ' Select * from T1 where ID < 10 ')
And it's worth noting that if you're going to execute the following database statement in OpenQuery,
Select * from t1 Where Name = ' Ida '
' Ida ' must be transmitted in double quotation marks, as below,
SELECT * from OpenQuery ([LinkedServerName], ' Select * from T1 where name= ' Ida ')
What if you are writing data to a different database? The format is as follows:
INSERT into OPENQUERY ([LinkedServerName], ' Select Id,name,....
From [Dbname].dbo.tbname where 1=0 ') Select Id,name,...
where ...
Use of OpenQuery in SQL