简体   繁体   中英

Copy multiple columns from a table to another table with extra columns

I want to copy records from one table to another table with extra columns.
The table columns looks like this

Table1: A,B,C,...,X
Table2: A,B,C,...,X,Y,Z

I know that i can do it in the following way

Insert into table2
  Select
  A,
  B,
  C, 
  ...
  X,
  '1',
  '1'
  from table1

But it is a very inefficient way if the table contains lots of columns. I need to do the similar procedure for several tables. Is there a better way to do it?
I tried the following but it doesn't work

insert into table2 select * , '1', '1'  from table1;

The thing you tried should work, just add alias.

INSERT INTO table2
SELECT t1.*,
       '1',
       '1'
  FROM table1 t1;

Insert into table2 ( A, B, C, ... , X, Y , Z) SELECT ( A, B, C, ... , X, '1' , '1') from table1 ;

In this case Y & Z are Strings (characters).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM