Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Pass variable to Openquery Expand / Collapse
Author
Message
Posted Friday, October 26, 2012 7:57 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 2:13 PM
Points: 166, Visits: 360
Hi I am getting the following error message trying to pass a variable into an openquery, can someone point out how to correct the error: Incorrect syntax near '+'
Thank you very much...

declare @string varchar(max)
set @string = 'abcd'
select * from openquery
(TEST_Server, 'select * from Table1 where Field1 = ' + @string + '')
Post #1377598
Posted Friday, October 26, 2012 8:10 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, December 16, 2014 7:16 AM
Points: 3,931, Visits: 1,102
Try the following syntax

declare @string varchar(max)
set @string = 'abcd'
select * from openquery
(TEST_Server, 'select * from Table1 where Field1 = ' + @string + '')



Post #1377607
Posted Friday, October 26, 2012 8:14 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 2:13 PM
Points: 166, Visits: 360
Hi vyas, thank you for the reply. That's the same syntax that I originally had, or at least I can't see the difference in your query, am I missing something?
Post #1377611
Posted Friday, October 26, 2012 10:13 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, November 3, 2014 10:21 PM
Points: 256, Visits: 2,009
I think the problem is that you are declaring the variable outside the query. Try putting everything into the OpenQuery:

Select * from OpenQuery(TestServer,'declare @string varchar(max) Set @string=''abcd'' Select * From Table1 where Field1=@string')

HTH

Elliott
Post #1377713
Posted Monday, October 29, 2012 6:50 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 1:37 AM
Points: 997, Visits: 3,089
OPENQUERY does not accept variables for its arguments. If you need to build the query dynamically you can use dynamic SQL.

DECLARE @string VARCHAR(MAX),
@SQL NVARCHAR(MAX);

SET @string = 'abcd';

SET @SQL = 'SELECT * FROM OPENQUERY(TEST_Server, ''SELECT * FROM Table1 WHERE Field1 = ''''' + @string + ''''''')';

EXEC sp_executesql @SQL;





The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Post #1378228
Posted Monday, October 29, 2012 8:33 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, November 3, 2014 10:21 PM
Points: 256, Visits: 2,009
Re: "OpenQuery does not accept variables" --

This runs fine in my set-up:

Select * from OpenQuery(MyLinkedServer,'Declare @name varchar(50) Set @name=''RemoteDB'' Select * from sys.databases where name=@name')

Note the declaration of the variable within the query statement (and the assignment of a value with double quotes).

HTH
Elliott

Post #1378271
Posted Monday, October 29, 2012 8:39 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:52 PM
Points: 5,317, Visits: 12,353
Elliott Berkihiser (10/29/2012)
Re: "OpenQuery does not accept variables" --

This runs fine in my set-up:

Select * from OpenQuery(MyLinkedServer,'Declare @name varchar(50) Set @name=''RemoteDB'' Select * from sys.databases where name=@name')

Note the declaration of the variable within the query statement (and the assignment of a value with double quotes).

HTH
Elliott



In your example, OpenQuery is not 'accepting' a variable. The OpenQuery argument is static, even though it contains a variable.



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.

It is better to keep your mouth shut and appear stupid than to open it and remove all doubt. (Mark Twain)
Post #1378278
Posted Monday, October 29, 2012 9:14 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, November 3, 2014 10:21 PM
Points: 256, Visits: 2,009
Yes, I agree.
Post #1378298
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse