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 2008 Forums
 Transact-SQL (2008)
 Join, OpenQuery and Variables...

Author  Topic 

ewomack
Starting Member

33 Posts

Posted - 2011-02-07 : 14:06:52
Hello,

If this is possible it will simplify my life incredibly.

Can I join to a linked server using OpenQuery?

Right now my join code is:

INNER JOIN OpenQuery(LinkedServer, @sql) AS rq ON sr.ID = rq.ID


The @sql contains a dynamic query that access a function on the linked server.

I should also mention that I'm on a SQL 2000 box (through no fault of my own ).

Thank you!

Ed Womack
www.getmilked.com

ewomack
Starting Member

33 Posts

Posted - 2011-02-07 : 14:13:39
Sorry, I should have also mentioned that the current code is giving me an "incorrect syntax" error on the join line I posted and I've been trying to escape out the quotes to no avail (yet).

Ed Womack
www.getmilked.com
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-02-07 : 14:22:41
You can't execute it that way. The ENTIRE statement, including the OPENQUERY portion and the JOIN, would have to be in the @sql variable.

Also, you do NOT want to join local tables to remote data sources, either with OPENQUERY or 4 part names. It is better to pull all of the data from OPENQUERY into a local (temp) table and join the remaining local tables to it instead.
Go to Top of Page

ewomack
Starting Member

33 Posts

Posted - 2011-02-07 : 14:41:59
Thank you, this method was suggested to me by someone else and I was skeptical when I couldn't get it to work. I will go back to temp tables, which was where I started.

Thanks!

Ed Womack
www.getmilked.com
Go to Top of Page
   

- Advertisement -