Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I need a query to get the data from two tables which are in two different databases.
I have the columns like RegId,GaurId,transID,payamount,status,paymethod in table A in database A and having the coloumns like eventid,CAC,Amount,paystatus in table B in Database B.How to union these tables.Can anyone help me regarding this?
Posted

1 solution

Hey if the two databases are on the same server its just simply dong like so

SQL
SELECT col1, col2, col3 FROM Database1.Schema1.Table1
UNION 
SELECT col1, col2, col3 FROM Database2.Schema2.Table2


And remember to union you must seklect the same numbetr of columns from both and the columns must be the same datatype.
 
Share this answer
 
v2
Comments
priya9826 19-Mar-13 10:51am    
But the some of the coloumns are different.It throws the error for me
priya9826 19-Mar-13 10:52am    
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.

This is the error which has thrown for me.
frostcox 19-Mar-13 11:14am    
You cannot do a union if the columns are of different datatypes.
priya9826 19-Mar-13 11:26am    
whats the resolution for this?then how can I extract the data from 2 tables
phil.o 19-Mar-13 11:55am    
Make two distinct queries.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900