How to transfer data from a table to another table with many columns?

I have one table with 20 columns that I need to transfer to a new table with 25 columns. I was wondering if there is such a possibility? I will greatly appreciate any source that leads me to a solution. Thank you so much for your time and help.

Below is a short example:

table.1 includes, say, 4 columns in the following order: t1.First_name, t1.last_name, t1.Phone_number, t1.Address

and I want to transfer this data to table 2 which includes these columns in the following order: t2.First_name, t2.Last_name, t2.Gender, t2.Phone_number, t2.Phone_type, t2.Address

+3


source to share


3 answers


INSERT INTO table2 ( First_name, Last_name, Gender, Phone_number, Phone_type, Address)
SELECT  First_name, last_name, 'M', Phone_number, 'cell', Address
FROM    table1

      



For columns that do not exist in the original table, you need to provide default or NULL values ​​and update them later.

+3


source


It depends on the extra columns - do they support NULL values, etc.?

Usually you can do something like INSERT INTO ... SELECT ( https://www.mssqltips.com/sqlservertutorial/2522/insert-into-sql-server-table-with-select-command/ )



eg.

    INSERT INTO [dbo].[table2] (First_name, Last_name, Gender, Phone_number, Phone_type, Address)
    SELECT First_name, last_name, NULL, Phone_number, NULL, Address
    FROM [dbo].[table1]

      

+2


source


insert into t2(FIRST_NAME,LAST_NAME,PHONE_NUMBER,ADDRESS) 
(select * from t1);

      

+1


source







All Articles