SQL uses parameters in openquery

Source: Internet
Author: User

Openquery is a technology used by SQL Server to interact with other servers. Through openquery, SQL Server can directly access other database resources. Other databases use the linked server in the openquery expression. Use sp_linkedservers to find all the linked server. openquery expressions in the current database. You can write these expressions as follows:

 
Select * From Openquery([Linkedservername],'Select * From Table1 where rownum <10')

It is worth noting that if you want to execute the following database statements in openquery,

 
Select * FromTbl_empWhereEmpname= 'Leo'

'Leo 'must be transmitted in double quotation marks as follows,

 
Select * From Openquery([Linkedservername],'Select * From Table1 whereEmpname = ''leo ''')

 

Today, I encountered a request to upload a parameter to openquery. It took me half a day to get it out, and it was hard to find it online.CodeShare it with others, hoping to help others. Haha

Declare @ SQL Varchar(1000)
Declare @ Organizationid Varchar(10)
Select @ Organizationid =(SelectOrganization_idFromMyorgsWhereOrganization_name= 'Mt'')
Set @ SQL = 'Select * From tablename where organization_id ='+@ Organizationid
Set @ SQL = 'Select * From openquery (betsycrp2,''' + Replace(@ SQL,'''','''''')+ ''')'
Exec(@ SQL)

 

 

 

Http://www.cnblogs.com/Dannier/archive/2011/09/21/openquery.html

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.