简体   繁体   中英

How to insert the data of a subset table into a superset table using select statement?

I am trying to make a dynamic query where I want to insert data into the superset table from subset table.

Here are my tables

Table A
|---------------------|------------------|
|          id         |  EmployeeName    |
|---------------------|------------------|
|           1         |       ABC1       |
|---------------------|------------------|
|           2         |       ABC2       |
|---------------------|------------------|
|           3         |       ABC3       |
|---------------------|------------------|

Table B
|----------------------|---------------------|------------------|
|          id          |       empid         |  EmployeeName    |    
|----------------------|---------------------|------------------|
|                      |                     |                  |
|----------------------|---------------------|------------------|
|                      |                     |                  |
|----------------------|---------------------|------------------|
|                      |                     |                  |
|----------------------|---------------------|------------------|

I want to insert data of table A into table B

here is the query I am trying

insert into B (select * from A); 

It is not working and showing me that both tables have unequal number of columns.

By default an insert statement expects that all target columns are provided. You can change that by enumerating the columns you want to insert in. This is a good practice that you should should stick to, since it makes the queries easier to read and maintain.

Presumably, you want:

insert into b(id, employeeName)
select id, employeeName
from a

You need to map the columns so them can match. See documentation

Try this one:

insert into B (id, EmployeeName) select id, EmployeeName from A; 

Insert into B(id, EmployeeName) values(select * from A); //This will work only If notNull constraint is not set on empid.

This is the most dynamic query that you can have for this situation. We had mentioned column names of table B because you are providing values of only 2 columns. If you dont want to use column name then you need thre remaining data in some other table and modify the table B a little bit.

 Table A
|---------------------|------------------|
|          id         |  EmployeeName    |
|---------------------|------------------|
|           1         |       ABC1       |
|---------------------|------------------|
|           2         |       ABC2       |
|---------------------|------------------|
|           3         |       ABC3       |
|---------------------|------------------|

Table B
|----------------------|---------------------|------------------|
|          id          |       EmployeeName  |      empid       |    
|----------------------|---------------------|------------------|
|                      |                     |                  |
|----------------------|---------------------|------------------|
|                      |                     |                  |
|----------------------|---------------------|------------------|
|                      |                     |                  |
|----------------------|---------------------|------------------|
Table C
|---------------------|------------------|
|          id         |       empid      |
|---------------------|------------------|
|           1         |       1000       |
|---------------------|------------------|
|           2         |       1001       |
|---------------------|------------------|
|           3         |       1002       |
|---------------------|------------------|

Now you can run the query: insert into B values(select * from A inner join C on A.id=C.id); this will populate your table A.

This is the final solution that worked for me because Id is an auto increment column and if you enter null into it, nothing changes.

INSERT INTO B (SELECT NULL, A.* FROM A);

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