我向现有表添加了一个新列(列 acadid、orgid、childid),现在我想为其插入值。
alter table table1 add new_parent int
insert into table (new_parent) select parent from (select parent
from table2 o inner join table1 ou
on ou.orgid=o.orgunitid) np
这里:
select parent
from table2 o inner join table1 ou
on ou.orgid=o.orgunitid
此查询为我提供了新父列的多个值(多行)。
但是上面的代码给了我以下错误:
Cannot insert the value NULL into column 'acadid', table 'tempdb.dbo.table1______________________________________________________________000000014F2B'; column does not allow nulls. INSERT fails. The statement has been terminated.
如何修复它?
最佳答案
出现此错误消息的原因是您使用 alter table 语句向 table1 表添加了新列,但没有更新此列的值现有行的列,您正在添加新行,并且仅此列填充了 insert 语句,并且另一列上可能存在不允许为空的约束(主键约束?)。
相反,您可能希望使用以下更新来更新现有行的新列的值:
update table1
set table1.new_parent = table2.parent
from table1 inner join table2 on table2.orgid=table1.orgunitid
关于mysql - 在列中插入多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22540319/