[QODBC-Desktop] Troubleshooting - How to use parameters in OPENQUERY
Posted by Jack - QODBC Support on 04 May 2015 01:58 PM

Problem Description:

How to use parameters in OPENQUERY

Solutions:

OPENQUERY does not accept variables for its arguments. You need to Pass Basic Values as below example:

Select Query:

DECLARE @TSQL varchar(8000), @ID varchar(25)

SELECT @ID = '19650'

SELECT @TSQL = 'SELECT * FROM OPENQUERY(QRemote ,''SELECT * FROM ReceivePayment WHERE ReceivePayment.RefNumber = ''''' + @ID + ''''''')'

EXEC (@TSQL)

Update Query:

DECLARE @TSQL varchar(8000), @ID varchar(25), @CName varchar(25)

SELECT @ID = '80000146-1513345553'

SELECT @CName = 'New Company'

SELECT @TSQL = 'Update OPENQUERY(QRemote ,''SELECT * FROM Customer WHERE Customer.ListID = ''''' + @ID + ''''''')' + 'SET CompanyName = ''' + @CName + ''''

EXEC (@TSQL)

Please refer below link:
How to pass a variable to a linked server query
Including parameters in OPENQUERY

(3 vote(s))
Helpful
Not helpful

Comments (0)
Post a new comment
 
 
Full Name:
Email:
Comments:
CAPTCHA Verification 
 
Please complete the captcha below (we use this to prevent automated submissions).