Q
Problem solve Get help with specific problems with your technologies, process and projects.

Creating unions between tables with dissimilar data types

How do you create a union between two or more tables with dissimilar column datatypes? The following SQL statement works okay in SQL Server but, in Oracle an error is raised due to improper data type of leading SELECT statement columns, probably do to NULLs. Note: the column's with an _NUM or _ID suffix are integer columns.

 
 SELECT  'PRIORITY' AS TABLETYPE, NULL AS DEPT_ID,
 NULL AS DEPT_NUM,
 NULL AS DEPT, NULL AS LOCATION, PRIORITY_ID,PRIORITY
 FROM PRIORITY
 UNION
 SELECT 'LOCATION' AS TABLETYPE, NULL AS DEPT_ID,
 NULL AS DEPT_NUM,
 NULL AS DEPT, LOCATION, NULL AS PRIORITY_ID, NULL AS
 PRIORITY FROM LOCATION
 UNION
 SELECT 'DEPT' AS TABLETYPE, DEPT_ID, DEPT_NUM, DEPT,
  NULL AS LOCATION, 
 NULL AS PRIORITY_ID,NULL AS PRIORITY  FROM DEPT 

I'm not sure which error you are getting. But I got a query similar to this to work just fine (Oracle 8i). See the following:

ORA9I SQL> select id,name,null as extra from test1
  2  union all
  3  select id,name,extra from test2
  4  union all
  5  select id,null as name,extra from test3;

        ID NAME                 EXTRA
---------- -------------------- --------------------
         1
         2 two
         3 three                none
         4
         5                      some
         6

6 rows selected.
My query is pretty similar to yours. Try your query again. If you need more help, please repost your question along with a cut & paste of your exact steps. The more information the better.

For More Information

  • What do you think about this answer? E-mail the editors at editor@searchDatabase.com with your feedback.
  • The Best Oracle Web Links: tips, tutorials, scripts, and more.
  • Have an Oracle or SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
  • Ask your technical Oracle and SQL questions -- or help out your peers by answering them -- in our live discussion forums.
  • Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.

Dig Deeper on Oracle database design and architecture

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchHRSoftware

Close