Skip to content
Advertisement

Copy data from one set of tables to another with extra columns [closed]

Is there any quick way for the below one? instead of generating Insert into Select SQL queries manually one by one for all 500 tables?

I have to copy data from one schema to another. I have about 500 tables. Source and target tables columns are the same, except target has few additional constant columns. Constant columns in target are create_date which is system date, userID which would be a functional ID and another column is ID which is Identity column.

I got the answer from Iain Ward. Looking for insert into select * query

Advertisement

Answer

Have a look at SQL Server’s SELECT INTO statement. It let’s you select data from an existing table, including any additional data, and insert it into a new one like this:

SELECT *, SYSDATETIME(), GETUSERID()
INTO newtable [IN externaldb]
FROM oldtable
WHERE condition;
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement