If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > With Clause

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-03-12, 06:29
edwin_fredrick edwin_fredrick is offline
Registered User
 
Join Date: Oct 2004
Posts: 44
With Clause

I having the following query using WITH clause.

with tbl as ( select 'A' as COL1 From SYSIBM.SYSDUMMY1 ) select * from tbl

The above query works fine but when i want to use is as sub table i am not able to do this in DB2, but works on Oracle & SQL Server.


select * from (with tbl as ( select 'A' as COL1 From SYSIBM.SYSDUMMY1 ) select * from tbl) a

Any ideas to make the above SQL work? I know it does not make sense to use is as sub-stable, but from one of our C++ application, the SELECT has to be the first word.

Best Regards
Edwin
Reply With Quote
  #2 (permalink)  
Old 01-03-12, 06:36
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
The sample is too simple.
So, I couldn't understand why you used common-table-expression.

Anyhow, try this...
select * from ( select * from (select 'A' as COL1 From SYSIBM.SYSDUMMY1 ) tbl) a
Reply With Quote
  #3 (permalink)  
Old 01-03-12, 07:07
edwin_fredrick edwin_fredrick is offline
Registered User
 
Join Date: Oct 2004
Posts: 44
Thanks for your reply.

I have just simplified my requirement for posting. Actually we need to use WITH, as CTE table is used multiple times in the query.
Reply With Quote
  #4 (permalink)  
Old 01-03-12, 08:12
lelle12 lelle12 is offline
Registered User
 
Join Date: Mar 2003
Posts: 86
DB2 does not support embedding cte's in sub selects, but you always rewrite this as:

select * from (with tbl as ( select 'A' as COL1 From SYSIBM.SYSDUMMY1 ) select * from tbl) a

with tbl as ( select 'A' as COL1 From SYSIBM.SYSDUMMY1 ), a as (select * from tbl)
select * from a;
Reply With Quote
  #5 (permalink)  
Old 01-03-12, 08:24
lelle12 lelle12 is offline
Registered User
 
Join Date: Mar 2003
Posts: 86
Now I see that you have to start with select. The only solution I can think of is to wrap the cte in a table function:

create function f ( ... )
returns table ( ... )
return
with ... select ...

and then select from that function in your program:

select * from table(f(...));

/Lennart
Reply With Quote
  #6 (permalink)  
Old 01-03-12, 08:46
edwin_fredrick edwin_fredrick is offline
Registered User
 
Join Date: Oct 2004
Posts: 44
Thanks Lennart !!

The function approach would be solution in DB2 for this issue.

In SQL Server & DB2 we can using functions to return table and is pretty simple unlike in Oracle where we need to use REF CURSOR which adds additional complication.

Our application needs to support all the 3 database mentioned, So i was trying to keep the SQLs as common as possible.

Thanks all for your valuable suggestions.
Reply With Quote
  #7 (permalink)  
Old 01-03-12, 10:00
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by edwin_fredrick View Post
In SQL Server & DB2 we can using functions to return table and is pretty simple unlike in Oracle where we need to use REF CURSOR which adds additional complication.
Not true, Oracle supports table functions as well. You can create a function that you use like this:
Code:
select * from table(my_function());
Check out "pipelined functions" in the manual.
Reply With Quote
  #8 (permalink)  
Old 01-03-12, 14:56
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by edwin_fredrick View Post
The function approach would be solution in DB2 for this issue.

Our application needs to support all the 3 database mentioned, So i was trying to keep the SQLs as common as possible.
You don't need table functions for this purpose: creating a VIEW would be easier, and more transparent to the application:
Code:
create view xxx as
  with tbl as ( select 'A' as COL1 From SYSIBM.SYSDUMMY1 ) select * from tbl
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #9 (permalink)  
Old 01-09-12, 02:26
edwin_fredrick edwin_fredrick is offline
Registered User
 
Join Date: Oct 2004
Posts: 44
Yeah True, Since i have many parameters to be passed, Use of functions would be ideal IMHO.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On