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 2005 Forums
 Transact-SQL (2005)
 HOW to use OPENQUERY WITH SQL LOCAL TABLE ??

Author  Topic 

korssane
Posting Yak Master

104 Posts

Posted - 2010-08-23 : 14:28:02
Hi folks,

i am really stuck in here with this .

i want to show some data from an oracle db using the openquery statment. this data is should be pulled based on a Local table field info.

here is my small example but it does not work .

SELECT * FROM OPENQUERY(LINKED_A,
'SELECT fe.KEYFIELD FROM table1@oradb fe ') as b
JOIN sql2k5db AEA ON b.KEYFIELD = AEA.KEYFIELD

N.B : The open query statement works fine separately.


thanks for the help

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-23 : 14:32:51
is @oradb a variable from where you need extract table name?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

korssane
Posting Yak Master

104 Posts

Posted - 2010-08-23 : 15:19:58
hi
it is a dbname / schema basically but this can be ignored ..

i can write my code like this :

SELECT * FROM OPENQUERY(LINKED_A,
'SELECT fe.KEYFIELD FROM table1 fe ') as b
JOIN sql2k5db AEA ON b.KEYFIELD = AEA.KEYFIELD

thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-23 : 15:24:37
ok...if you need to use variable then you need dynamic sql

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

korssane
Posting Yak Master

104 Posts

Posted - 2010-08-23 : 15:31:53
what do you mean by dynamic variable.. and why do i need this ?
is is not like simple join between a local SQL table and another remote one ?
thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-24 : 07:34:14
quote:
Originally posted by korssane

what do you mean by dynamic variable.. and why do i need this ?
is is not like simple join between a local SQL table and another remote one ?
thanks



i was telling dynamic sql not dynamic variable. if part of table name comes from variable, you need to write statement like

EXEC('SELECT * FROM ' + @Variable + '..Table')

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-08-24 : 08:29:36
Also make sure to read this
www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -