是否可以将所有数据从一个表复制到另一表以及一个额外的列? [英] Is it possible to copy all data from one table to another table, plus an extra column?

查看:48
本文介绍了是否可以将所有数据从一个表复制到另一表以及一个额外的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个稍微不同的表.表A有4列,而表B只有3列.我想将表B中的所有数据复制到表A中,但是我也想为每一行的每一列填充1值.

I have two tables that are very slightly different. Table A has 4 columns, and Table B has only 3. I want to copy all the data from Table B into Table A, but I also want to populate the extra column with the value 1 for every row.

如果不使用多余的列,这将起作用:

This would work if not for the extra column:

insert into TABLEA (COL1, COL2, COL3) select COL1, COL2, COL3 from TABLEB;

不幸的是,表A中的额外列不可为空,因此我不能再执行更新.

Unfortunately, the extra column in Table A is not nullable, so I can't just run an update afterwards.

感谢您的帮助!

推荐答案

指定列并使用常量作为值(请注意,您可以在select子句中混合使用常量和列引用).在这种情况下,我们指定每一行将为列COL4获得常数1.

Specify the column and use a constant for the value (note you can mix constants and column references in a select clause). In this case we're specifying every row will get the constant 1 for column COL4.

insert into TABLEA (COL1, COL2, COL3, COL4)
select COL1, COL2, COL3, 1
from TABLEB;

这篇关于是否可以将所有数据从一个表复制到另一表以及一个额外的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆