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 > Access column by number in result set

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-04-12, 01:13
raks81 raks81 is offline
Registered User
 
Join Date: Jul 2009
Posts: 8
Access column by number in result set

I have a bunch of queries that is stored in a file provided by a vendor and I know that it always returns rows with 2 columns in them. For eg:

Code:
SELECT EMPID, NAME FROM EMPLOYEE;
SELECT BOOK_ID, BOOKNAME FROM BOOKS;
However I cannot modify them in anyway.

I want to use these statements in a generic sql statement like 'SELECT col2 from (vendor-sql-statement)' to access values in column 2. For eg:

Code:
-- Get all the employee names without the employee ids:
select [col2] from (SELECT EMPID, NAME FROM EMPLOYEE) 
-- Get all the book names without the book ids:
select [col2] from (SELECT BOOK_ID, BOOKNAME FROM BOOKS)
Is there a way of accessing doing this in DB2?
Reply With Quote
  #2 (permalink)  
Old 01-04-12, 02:00
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
How(or from what client program) do you want to execute the statements?
If you read the statements into your program, then I thought that you can modify them.

Anyway, if you want to use the statements as is,
how about enclosing them by "SELECT col2 FROM (" and ") t(col1, col2)"?

For example:
Code:
SELECT col2 FROM (SELECT EMPID, NAME FROM EMPLOYEE) t(col1, col2)
SELECT col2 FROM (SELECT BOOK_ID, BOOKNAME FROM BOOKS) t(col1, col2)
Reply With Quote
  #3 (permalink)  
Old 01-04-12, 02:11
raks81 raks81 is offline
Registered User
 
Join Date: Jul 2009
Posts: 8
Solved!

Thanks for the reply, that solved it.

I was never aware that we could specify such alias columns names in that syntax.

Thanks again!


Quote:
Originally Posted by tonkuma View Post
How(or from what client program) do you want to execute the statements?
If you read the statements into your program, then I thought that you can modify them.

Anyway, if you want to use the statements as is,
how about enclosing them by "SELECT col2 FROM (" and ") t(col1, col2)"?

For example:
Code:
SELECT col2 FROM (SELECT EMPID, NAME FROM EMPLOYEE) t(col1, col2)
SELECT col2 FROM (SELECT BOOK_ID, BOOKNAME FROM BOOKS) t(col1, col2)
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