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)
 Can I do openquery within openquery?

Author  Topic 

umniza
Starting Member

19 Posts

Posted - 2010-07-07 : 16:06:11
Hi,
I want to narrow results of my query by values that come from a different connection. Originally I wanted to load these values into array and do something like
where myvalues in (array)

then found out that sql does not work with arrays. Next I'm tryng to do openquery from within main select, getting syntax errors. I am not sure if this is not allowed or I messed up with apostrophes

SET @SQL='
(select * from openquery
('+ @One +',''
select *
from state
where
(city in
(select city from openquery
('+ @Two +',''
select city
from state_smaller ''
)
)
)
with ur ''
) as A
) B '

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-07-07 : 19:35:01
you are trying to do this in what? a stored procedure, or just a some test query? do you have linked servers?please post the error and what you are stuffing in The One and @two. what are you trying to achieve? why not just do

SELECT * from where x in (select x from server.database.table) etc

<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

umniza
Starting Member

19 Posts

Posted - 2010-07-08 : 16:29:01
Yes, this is stored procedure. All our stored procedures use openquery to connect to a specific database, so @One and @Two are DSNs for 2 different databases. These databases are on dedicated servers, they are linked servers to the sql server where my stored procedure lives. Looking at your example, I think I would have to do
select * from ( select * from server1.database1.table1 where x in (select x from server2.database2.table2))
Which is what I'm doing now except I am using openquery to connect.

I am not sure how exactly all the DSNs are setup, I know that server names they are on sometimes change. If I hardcode server_name.database.table and the server name changes, report will obviously fail, so cant do that. That's probably why openquery is used for all our stored procedures.

Go to Top of Page

umniza
Starting Member

19 Posts

Posted - 2010-07-08 : 17:02:41
So another thing I tried is stuffing the values I need from database @Two into temporary table that I defined initially. Use the table when connected to @One to narrow my data. This is what I am doing (there is more logic in the where clauses, I took it out to leave the shell for readability, all the particulars are non-essential anyway

ALTER PROCEDURE [dbo].[FluidStores]
(
@One varchar(20),
@Two varchar(20)
)

AS
Begin
DECLARE @SQL as varchar(8000)
DECLARE @SQL_t as varchar(8000)
DECLARE @SQL_t_insert as varchar(8000)
DECLARE @SQL_t_drop as varchar(8000)
DECLARE @TABLE_NAME VARCHAR(50)

SET NOCOUNT ON

SET @TABLE_NAME = '##FLUID_STORES'
SET @SQL_t = 'CREATE TABLE '+@TABLE_NAME +'([store] [VARCHAR] (10) NOT NULL) '
EXEC (@SQL_t)

SET @SQL_t_insert = 'INSERT INTO '+@TABLE_NAME +' (store)
SELECT fluid_stores_from_Two.store FROM OPENQUERY
('+@Two +','+
'''select store
from dbo.transactionlog
where column1 like ''''FLUID%''''
'''+') AS fluid_stores_from_Two'
EXEC(@SQL_t_insert)


SET @SQL = '
SELECT *
FROM OPENQUERY
( ' +@Two+ ',' +
'''select Store from TransactionLog''' + '
) all_stores_from_Two

INNER JOIN

( select *
from openquery
( '+ @One +',''
select route
from cartons
/* if i comment out the next line, everything works just fine */
where route in (SELECT DISTINCT store FROM '+ @TABLE_NAME +')
with ur ''
)
) stores_from_One on (stores_from_One.route = all_stores_from_Two.store)

'

EXEC (@SQL)

SET @SQL_t_drop = 'DROP TABLE ' + @TABLE_NAME
EXEC (@SQL_t_drop)

End



This is the exact error I'm getting :

Server: Msg 7399, Level 16, State 1, Line 3
OLE DB provider 'MSDASQL' reported an error.
3432
[OLE/DB provider returned message: [IBM][CLI Driver][DB2/AIX64] SQL0204N "WMUSER01.##FLUID_STORES" is an undefined name. SQLSTATE=42704
]
OLE DB error trace [OLE/DB Provider 'MSDASQL' IColumnsInfo::GetColumnsInfo returned 0x80004005: ].




It appears that it's looking for table ##fluid_stores within @One database, which seems strange to me. I thought '+TABLE_NAME+' means this TABLE_NAME is outside of @One's realm.

PS : I hope this makes some sense...
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-07-08 : 20:56:25
add PRINT @SQL and what problem do you see?

<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

umniza
Starting Member

19 Posts

Posted - 2010-07-09 : 02:07:26
Code (modified so it will run and produce print output)


ALTER PROCEDURE [dbo].[fluid_simple]
( @One varchar(20), @Two varchar(20) )

AS
Begin
DECLARE @SQL as varchar(8000)
DECLARE @SQL_t as varchar(8000)
DECLARE @SQL_t_insert as varchar(8000)
DECLARE @SQL_t_drop as varchar(8000)
DECLARE @TABLE_NAME VARCHAR(50)

SET NOCOUNT ON

SET @TABLE_NAME = '##FLUID_STORES'
SET @SQL_t = 'CREATE TABLE '+@TABLE_NAME +'([store] [VARCHAR] (10) NOT NULL) '
EXEC (@SQL_t)

SET @SQL_t_insert = 'INSERT INTO '+@TABLE_NAME +' (store)
SELECT x.store FROM OPENQUERY('+@Two +','+
'''select distinct case
when tl_typeid = 1 then column7
when tl_typeid = 35 then column4
end as store
from transactionlog
where (column1 like ''''FLUID%'''' or column3 like ''''FLUID%'''')
and tl_typeid in (1, 35)'''+') AS x'

PRINT(@SQL_t_insert )
EXEC(@SQL_t_insert)

SET @SQL = '
SELECT distinct coalesce (Two.store, One.rte_id) as store
FROM OPENQUERY
( ' +@Two+ ',' +
'''
select case when tl_typeid = 1 then column7
when tl_typeid = 35 then column4
end as Store
from TransactionLog
where tl_typeid = 35
and datediff(dd, tl_date,GetDate()) <= 0
and column1 like ''''FLUID%''''
''' + '
) Two

INNER JOIN

( select *
from openquery
( '+ @One +',''
select distinct rte_id
from carton_hdr
where stat_code = 50
and rte_id in (SELECT DISTINCT store FROM '+ @TABLE_NAME +') with ur ''
)
) One on (One.rte_id = Two.store)
'

PRINT(@SQL)
EXEC (@SQL)

SET @SQL_t_drop = 'DROP TABLE ' + @TABLE_NAME
EXEC (@SQL_t_drop)

End



-----------------------------------------------
Print Output :


Server: Msg 7399, Level 16, State 1, Line 3
OLE DB provider 'MSDASQL' reported an error.

INSERT INTO ##FLUID_STORES (store)
SELECT x.store FROM OPENQUERY(THD_PM_TLDB_5085,'select distinct case
when tl_typeid = 1 then column7
when tl_typeid = 35 then column4
end as store
from transactionlog
where (column1 like ''FLUID%'' or column3 like ''FLUID%'')
and tl_typeid in (1, 35)') AS x


SELECT distinct coalesce (Two.store, One.rte_id) as store
FROM OPENQUERY
( THD_PM_TLDB_5085,'
select case when tl_typeid = 1 then column7
when tl_typeid = 35 then column4
end as Store
from TransactionLog
where tl_typeid = 35
and datediff(dd, tl_date,GetDate()) <= 0
and column1 like ''FLUID%''
'
) Two

INNER JOIN

( select *
from openquery
( DPR671DC,'
select distinct rte_id
from carton_hdr
where stat_code = 50
and rte_id in (SELECT DISTINCT store FROM ##FLUID_STORES)
with ur '
)
) One on (One.rte_id = Two.store)

[OLE/DB provider returned message: [IBM][CLI Driver][DB2/AIX64] SQL0204N "WMUSER01.##FLUID_STORES" is an undefined name. SQLSTATE=42704
]
OLE DB error trace [OLE/DB Provider 'MSDASQL' IColumnsInfo::GetColumnsInfo returned 0x80004005: ].

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-07-09 : 03:16:03
You need to double-up the quotes in the inner OPENQUERY

We do it like this to make it easier to write:

SET @SQL='
openquery
('+ @Two +',''
select city
from state_smaller ''
)'

SELECT @SQL = REPLACE(@SQL, '''', '''''')

SELECT @SQL='
openquery
('+ @One +',''
select *
from state
where
(city in
(select city from ' + @SQL + '
)
) with ur ''
)'

SELECT @SQL='
(select * from ' + @SQL + '
as A
) B '

PRINT @SQL

I get:

(select * from
openquery
(ServerOne,'
select *
from state
where
(city in
(select city from
openquery
(ServerTwo,''
select city
from state_smaller ''
)
)
) with ur '
)
as A
) B

bits in Blue look suspicious to me
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-07-09 : 11:41:59
manually do the following, anything in RED i believe is problematic

1. CREATE TABLE ##FLUID_STORES ([store] [VARCHAR] (10) NOT NULL)
2.
INSERT INTO ##FLUID_STORES (store)
SELECT x.store FROM OPENQUERY(THD_PM_TLDB_5085,'select distinct case
when tl_typeid = 1 then column7
when tl_typeid = 35 then column4
end as store
from transactionlog
where (column1 like ''FLUID%'' or column3 like ''FLUID%'')
and tl_typeid in (1, 35)') AS x

3. Does this work and bring anything?
SELECT DISTINCT store FROM  ##FLUID_STORES


4.
SELECT distinct coalesce (Two.store, One.rte_id) as store
FROM OPENQUERY
( THD_PM_TLDB_5085,'
select case when tl_typeid = 1 then column7
when tl_typeid = 35 then column4
end as Store
from TransactionLog
where tl_typeid = 35
and datediff(dd, tl_date,GetDate()) <= 0
and column1 like ''FLUID%''
'
) Two

INNER JOIN

( select *
from openquery
( DPR671DC,'
select distinct rte_id
from carton_hdr
where stat_code = 50
and rte_id in (SELECT DISTINCT store FROM ##FLUID_STORES)
with ur '
)
) One on (One.rte_id = Two.store)

You get the idea right, if you cannot step through it manually in SSMS it will not work in sproc. Also what the heck is the following

[IBM][CLI Driver][DB2/AIX64] SQL0204N "WMUSER01.##FLUID_STORES"

<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

umniza
Starting Member

19 Posts

Posted - 2010-07-09 : 11:57:40
quote:
Originally posted by Kristen

You need to double-up the quotes in the inner OPENQUERY




Thaks Kristen, so you are saying it's not illegal to do openquery from within another openquery. I suspected that I might have messed up with quotes, I still am confused how they are used exactly, I usually just keep adding them until it works. This one I played with for a while and could not get it to work. I will try your code next.
Go to Top of Page

umniza
Starting Member

19 Posts

Posted - 2010-07-09 : 12:12:43
quote:
Originally posted by yosiasz

manually do the following, anything in RED i believe is problematic

I know it is problematic :), because without it sp compiles and runs fine. But that is what I need to do.

quote:
Originally posted by yosiasz

3. Does this work and bring anything?
SELECT DISTINCT store FROM  ##FLUID_STORES




yes, I tested it first thing - the values I need get inserted into temp table with no issues

quote:
Originally posted by yosiasz
[IBM][CLI Driver][DB2/AIX64] SQL0204N "WMUSER01.##FLUID_STORES"



this is the error i'm getting, WMUSER01 is the schema name for database @One. This tells me it's treating my ##FLUID_STORES table as if it belonged to @One's database. I dont know how to make it see that it's not.
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-07-09 : 12:23:09
:) so either qualify it with it's database schema name or create it in @One or make it a table variable

<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

umniza
Starting Member

19 Posts

Posted - 2010-07-09 : 14:40:57
quote:
Originally posted by yosiasz

:) so either qualify it with it's database schema name or create it in @One or make it a table variable



But my table is a table variable, I declared, created and populated it first thing in the sp.

Creating table on @One database is not possible, it's completely locked to me, I can only query this database, not create/update anything there.
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-07-09 : 15:44:56
you are not creating a table variable. you are creating a temp table which would be created on tempdb. using table variable will help you

declare @TableVar table (store [VARCHAR] (10) NOT NULL)


<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

umniza
Starting Member

19 Posts

Posted - 2010-07-09 : 15:54:54
quote:
Originally posted by umniza

quote:
Originally posted by Kristen

You need to double-up the quotes in the inner OPENQUERY



Thaks Kristen, ... I will try your code next.



I am soooooo confused with the quotes . Is there anyway I could get below code reviewed for the correctness of quotes. I tried to model after Kristen's code, it didnt work (I admit I couldnt follow the flow of it once I started retrofitting my code to Kristen's sample).

Stored procedure code :



ALTER PROCEDURE [dbo].[fluid_simple_doubleup]
( @One varchar(20),@Two varchar(20))

AS
Begin
DECLARE @SQL as varchar(8000)

SET @SQL = 'SELECT distinct *

FROM OPENQUERY
( ' +@Two+ ',' +
'''
select case when tl_typeid = 1 then column7
when tl_typeid = 35 then column4
end as Store
from TransactionLog
where tl_typeid = 35
and datediff(dd, tl_date,GetDate()) <= 0
and column1 like ''''FLUID%''''
''' + '
) Two
INNER JOIN
( select *
from openquery
( '+ @One +',''
select distinct rte_id
from carton_hdr
where stat_code = 50
and rte_id in
( select * OPENQUERY
('+@Two +',''
select distinct case
when tl_typeid = 1 then column7
when tl_typeid = 35 then column4
end as store
from transactionlog
where (column1 like ''''FLUID%'''' or column3 like ''''FLUID%'''')
and tl_typeid in (1, 35)''
)
)
with ur ''
)
) One on (One.rte_id = Two.store)
'

End

print (@SQL)
EXEC (@SQL)


Error :


Server: Msg 156, Level 15, State 1, Line 23
Incorrect syntax near the keyword 'select'.
Server: Msg 170, Level 15, State 1, Line 28
Line 28: Incorrect syntax near 'FLUID'.

Print Output :

SELECT distinct *

FROM OPENQUERY
( THD_PM_TLDB_5085,' select case when tl_typeid = 1 then column7
when tl_typeid = 35 then column4
end as Store
from TransactionLog
where tl_typeid = 35
and datediff(dd, tl_date,GetDate()) <= 0
and column1 like ''FLUID%''
'
) Two
INNER JOIN
( select *
from openquery
( DPR671DC,'
select distinct rte_id
from carton_hdr
where stat_code = 50
and rte_id in
( select * OPENQUERY
(THD_PM_TLDB_5085,'
select distinct case
when tl_typeid = 1 then column7
when tl_typeid = 35 then column4
end as store
from transactionlog
where (column1 like ''FLUID%'' or column3 like ''FLUID%'')
and tl_typeid in (1, 35)'
)
)
with ur '
)
) One on (One.rte_id = Two.store)

Go to Top of Page

umniza
Starting Member

19 Posts

Posted - 2010-07-09 : 16:04:30
quote:
Originally posted by yosiasz

you are not creating a table variable. you are creating a temp table which would be created on tempdb. using table variable will help you

declare @TableVar table (store [VARCHAR] (10) NOT NULL)



oh, I see... ok, let me try that too
Go to Top of Page

umniza
Starting Member

19 Posts

Posted - 2010-07-09 : 16:44:34
quote:
Originally posted by yosiasz

you are not creating a table variable. you are creating a temp table which would be created on tempdb. using table variable will help you

declare @TableVar table (store [VARCHAR] (10) NOT NULL)



Trying with the table now. What am I doing wrong?

declare @FLUID_STORES table (store [VARCHAR] (50) NOT NULL)

SET NOCOUNT ON


SET @SQL_t_insert =

'INSERT INTO @FLUID_STORES (store)
SELECT x.store FROM OPENQUERY('+@Two+',
'+
'''select distinct case
when tl_typeid = 1 then column7
when tl_typeid = 35 then column4
end as store
from transactionlog
where (column1 like ''''FLUID%'''' or column3 like ''''FLUID%'''')
and tl_typeid in (1, 35)
'''
+') AS x
'

PRINT(@SQL_t_insert )
EXEC(@SQL_t_insert)


Print Output :


INSERT INTO @FLUID_STORES (store)
SELECT x.store FROM OPENQUERY(THD_PM_TLDB_5085,
'select distinct case
when tl_typeid = 1 then column7
when tl_typeid = 35 then column4
end as store
from transactionlog
where (column1 like ''FLUID%'' or column3 like ''FLUID%'')
and tl_typeid in (1, 35)
') AS x


Error :
Server: Msg 137, Level 15, State 2, Line 1
Must declare the variable '@FLUID_STORES'.


Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-07-09 : 17:12:32
ok shapeshifter you keep changing you query every time you post :)

what field are you selecting in following query? as always try it manually

'select distinct case
when tl_typeid = 1 then column7
when tl_typeid = 35 then column4
end as store
from transactionlog
where (column1 like ''FLUID%'' or column3 like ''FLUID%'')
and tl_typeid in (1, 35)


<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-07-09 : 17:14:49
also this instead otherwise that table variable becomes ..if forgot the word, out of process yaddi yadda

SET NOCOUNT ON


SET @SQL_t_insert =

'declare @FLUID_STORES table (store [VARCHAR] (50) NOT NULL)

INSERT INTO @FLUID_STORES (store)
SELECT x.store FROM OPENQUERY('+@Two+',
'+
'''select distinct case
when tl_typeid = 1 then column7
when tl_typeid = 35 then column4
end as store
from transactionlog
where (column1 like ''''FLUID%'''' or column3 like ''''FLUID%'''')
and tl_typeid in (1, 35)
'''
+') AS x
'


<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

umniza
Starting Member

19 Posts

Posted - 2010-07-09 : 17:25:58
quote:
Originally posted by yosiasz

ok shapeshifter you keep changing you query every time you post :)



I am not changing it, I havent gotten to actual query, got stuck at populating the table :)
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-07-09 : 17:32:52
SET NOCOUNT ON

DECLARE @SQL_t_insert VARCHAR(max)
SET @SQL_t_insert = 'declare @FLUID_STORES table (store [VARCHAR] (50) NOT NULL) '
SET @SQL_t_insert = @SQL_t_insert +

' INSERT INTO @FLUID_STORES (store)
SELECT x.store FROM OPENQUERY('+@Two+',
'+
'''select distinct you_need_a_field_name_here case
when tl_typeid = 1 then column7
when tl_typeid = 35 then column4
end as store
from transactionlog
where (column1 like ''''FLUID%'''' or column3 like ''''FLUID%'''')
and tl_typeid in (1, 35)
'''
+') AS x
'



PRINT(@SQL_t_insert )
EXEC(@SQL_t_insert)

<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

umniza
Starting Member

19 Posts

Posted - 2010-07-09 : 17:38:29
Moving declare inside the quotes fixed that error. However now when I add last statement to execute the drop, I get error :
Line 1: Incorrect syntax near '@FLUID_STORES'. Is this scope issue? I can only reference it within @SQL_t_insert?



SET @SQL_t_insert =

'declare @FLUID_STORES table (store [VARCHAR] (50) NOT NULL)
INSERT INTO @FLUID_STORES (store)
SELECT x.store FROM OPENQUERY('+@Two+',
'+
'''select distinct case
when tl_typeid = 1 then column7
when tl_typeid = 35 then column4
end as store
from transactionlog
where (column1 like ''''FLUID%'''' or column3 like ''''FLUID%'''')
and tl_typeid in (1, 35)
'''
+') AS x
'

PRINT(@SQL_t_insert )
EXEC(@SQL_t_insert)


SET @SQL_t_drop = 'DROP TABLE @FLUID_STORES'
EXEC (@SQL_t_drop)


Print out :
----------

declare @FLUID_STORES table (store [VARCHAR] (50) NOT NULL)
INSERT INTO @FLUID_STORES (store)
SELECT x.store FROM OPENQUERY(THD_PM_TLDB_5085,
'select distinct case
when tl_typeid = 1 then column7
when tl_typeid = 35 then column4
end as store
from transactionlog
where (column1 like ''FLUID%'' or column3 like ''FLUID%'')
and tl_typeid in (1, 35)
') AS x



Go to Top of Page
    Next Page

- Advertisement -