I have a question regarding my attempt to combine two tables in sql server...
I have one table that has about 30 columns (table a), and a second table that has about 5 (table b), those 5 being present in the 30 column table. I want to be able to add table b to the end of table a, and just use 0 values for the nonexistent columns in the new rows.
Is there a way to do this? Obviously a regular union wouldnt work if I want to keep the other columns in table a.
You can just insert the rows in:
This will apply the default values for the remaining columns. The defaults would typically be NULL, but could be changed in the create table statement. In order to get 0s, you would have to include the columns and specify values:
The basic idea is this, assuming
col3
does not exist inTableB
:Be careful to keep the data types the same within each column.
To keep the
TableB
records at the end, do: