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 > Inheritance query in DB2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-27-06, 10:59
Breako Breako is offline
Registered User
 
Join Date: Jan 2006
Posts: 119
Inheritance query in DB2

I have two objects which represent an inheritance.
Employee inherits from Person. To represent this, I have two tables: TPerson and TEmployee. I want to SELECT from both tables at the same time using the UNION ALL.
I want to do a UNION SELECT from both tables in the one query.
The problem is that the UNION syntax means that you must have the same number of columns in both tables.
There are some columns in TEmployee that are not in TPerson. For example suppose column EmployeeNumber is not in TPerson.
In oracle I can do:

SELECT EmployeeNumber, x1, x2, x3
FROM TEMPLOYEE
UNION ALL
SELECT null as EmployeeNumber, x1, x2, x3
FROM TPERSON.

However I can't do this in DB2, I try to do:

SELECT EmployeeNumber, x1, x2, x3
FROM TEMPLOYEE
UNION ALL
SELECT cast(null as INT), x1, x2, x3
FROM TPERSON

but this does not work.

I get: DB2 SQL error: SQLCODE: -206, SQLSTATE: 42703 i.e. An undefined column, attribute, or parameter name was detected.

Any ideas?
Reply With Quote
  #2 (permalink)  
Old 01-27-06, 12:55
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Are you sure EmployeeNumber is INT?
__________________
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 01-27-06, 15:57
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
SQL0206N "<name>" is not valid in the context where it is
used.

Explanation:

This error can occur in the following cases:
...

o For a SELECT or DELETE statement, the specified column is not
a column of any of the tables or views identified in a FROM
clause in the statement.
Check the spelling.
Reply With Quote
  #4 (permalink)  
Old 01-27-06, 17:03
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
Can you give the DLL (create table) scripts for both tables ?
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
  #5 (permalink)  
Old 01-28-06, 03:18
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
you might also try


SELECT EmployeeNumber, x1, x2, x3
FROM TEMPLOYEE
UNION ALL
SELECT NULLIF( 1 , 1 ), x1, x2, x3
FROM TPERSON
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