Use of OpenQuery in SQL

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.