| Author |
Topic |
|
haler
Starting Member
13 Posts |
Posted - 2011-12-19 : 19:04:45
|
| Hi,What i am trying to do is merge multiple tables together into one table. All tables follow the same column name template but some tables do not have that column. I have tried using union but it requires the table to have the same columns exactly.for exampletable 1site, name, rawAddress, streetAddress, phone, facebooktable 2site, name, rawAddress, streetAddress, phone, facebooktable 3site, name, rawAddress, streetAddress, phone, fax, facebooktable 4site, name, rawAddress, streetAddress, phone, faxHow would i go about merging all the tables into one table based on their column name.Thanks |
|
|
singularity
Posting Yak Master
153 Posts |
Posted - 2011-12-19 : 19:08:34
|
| [code]select site, name, rawAddress, streetAddress, phone, null as fax, facebookfrom table1union allselect site, name, rawAddress, streetAddress, phone, null as fax, facebookfrom table2union allselect site, name, rawAddress, streetAddress, phone, fax, facebookfrom table3union allselect site, name, rawAddress, streetAddress, phone, fax, null as facebookfrom table4[/code] |
 |
|
|
haler
Starting Member
13 Posts |
Posted - 2011-12-19 : 19:16:58
|
| Is there a way to do this without having to check each table to see whether the column is there or not? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-19 : 22:27:55
|
| what does that mean? does that mean field doesnt even exist in table or is it like its existing with empty/null values?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
haler
Starting Member
13 Posts |
Posted - 2011-12-19 : 22:52:08
|
| it means the field doesnt exist in some tables |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-20 : 00:09:42
|
| then as shown above you need to put a place holder for that as union/union all requires all seperated queries to have same number of columns and corresponding field datatypes should be compatible with each other------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|