Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Problem in Pass Through Query !!!!

Author  Topic 

bhavyan_jobs
Starting Member

6 Posts

Posted - 2002-11-21 : 06:32:21
Hi There !

I want to run the following pass-through query on the remote server

select * from tableX where colA = @varZ (Query to be passed to remote server)

i'm not getting able how to write the query and pass the variable @varZ in the WHERE Clause using following syntax

select * from openquery(XYZServer, 'select * from tableX where colA = @varZ') (This is the incorrect syntax)

plz suggest something so that i could lland safely

reply ASAP

regards
bhavya

Bhavyanidhi Kukreja
Network Programs India Ltd.
Member Technical Staff
B-1-C, Sector 10
Noida-201301
U.P.
Phone-0120-4536622

samsekar
Constraint Violating Yak Guru

437 Posts

Posted - 2002-11-21 : 07:05:16
select * from openquery(XYZServer, 'select * from tableX where colA = @varZ') (This is the incorrect syntax)

Here @VarZ will be considered as a text not as a variable instead u can use "sp_executeSql" stored procedure. refer BOL for furthur details.



Sekar
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2002-11-21 : 13:50:54
try:


select * from openquery(XYZServer, 'select * from tableX where colA = ' + @varZ)


note that we are dereferencing varZ and appending its value to the string, so its VALUE is what is processed as the SQL statement.

If colA is a char field, you will have to put a quote before and after it. You may need to do this in steps, by declaring a variable first and setting it. Not sure about that, though.

DECLARE @VAL varchar(2000);

SELECT @VAL = 'Select * tableX WHERE ColA = ' + char(39) + @varZ + CHAR(39)

SELECT * FROM Openquery(XYZServer, @VAL)


- Jeff
Go to Top of Page
   

- Advertisement -