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 > Subselect in a SELECT

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-14-11, 18:02
P4ul P4ul is offline
Registered User
 
Join Date: Jul 2011
Posts: 10
Subselect in a SELECT

Hi All,

I'm trying retrieve a column from a table where that column name is stored in another table.

Here is an example

Tab1
Col1, Col2
SomeData, Col1

Tab2
Col1, Col2
Hello, SomeData

select (select Col2 from Tab1 where Col1=SomeData) from Tab2
where Col2=SomeData

The sub query is only retrieving one row.

Here's the error I'm getting:

SQL10007N Message "4700" could not be retrieved. Reason code: "4".

Here's the version info:

Product: DB2 Connect Enterprise Edition
Version: DB2 v8.2
Fixpack: 7
OS: Windows XP Verison 5.1 SP3

Can this be done?

Any help much appreciated.

Cheers
Paul
Reply With Quote
  #2 (permalink)  
Old 07-14-11, 18:09
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Can you show the results you are expecting. I don't quite understand what you are trying to do.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 07-14-11, 18:17
P4ul P4ul is offline
Registered User
 
Join Date: Jul 2011
Posts: 10
So without the sub-select the query would look like this:

select Col1 from Tab2
where Col2=SomeData

Result: Hello

But I want to retrieve the column (i.e Col1 in this query) from another table (i.e. Tab1)
Reply With Quote
  #4 (permalink)  
Old 07-14-11, 18:19
P4ul P4ul is offline
Registered User
 
Join Date: Jul 2011
Posts: 10
The column headings for Tab2 are stored in a column in another table
Reply With Quote
  #5 (permalink)  
Old 07-14-11, 18:20
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Quote:
Originally Posted by P4ul View Post
So without the sub-select the query would look like this:

select Col1 from Tab2
where Col2=SomeData

Result: Hello

But I want to retrieve the column (i.e Col1 in this query) from another table (i.e. Tab1)
Please show the actual output you expect given a set of existing data in the two tables. Simply "describing" the output is not sufficient for me to understand what you are talking about.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #6 (permalink)  
Old 07-14-11, 18:22
P4ul P4ul is offline
Registered User
 
Join Date: Jul 2011
Posts: 10
I think I've described the problem quite well.

Thanks anyway.
Reply With Quote
  #7 (permalink)  
Old 07-14-11, 18:38
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
@P4ul The answer is in here


----
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #8 (permalink)  
Old 07-14-11, 19:19
P4ul P4ul is offline
Registered User
 
Join Date: Jul 2011
Posts: 10
I couldn't find a solution in that deck, so I'm assuming this isn't supported. Kinda makes sense. Thanks for help.
Reply With Quote
  #9 (permalink)  
Old 07-16-11, 07:32
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
You'll need a two-stage process: one query whose output is a SELECT statement (with the "col1" in it), then execute that second query.
This can be achieved through either a .bat script with output redirect, or using SQL PL. Or just manually, of course, by passing the output of the first query to a second db2 run (by cut&paste into your command window).
Here is the query to generate the second SELECT statement:
Code:
SELECT 'select '|| Col2 ||' from Tab2 where Col2='''|| Col1 || ''';'
FROM Tab1 WHERE Col1='SomeData'
If Tab1 contains more than one row with 'SomeData' in its first column, this will generate all corresponding SELECT statements on Tab2.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/

Last edited by Peter.Vanroose; 07-16-11 at 07:35.
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