在列中插入多个值

时间:2014-03-20 17:09:45

标签: mysql sql sql-server database sql-server-2008

我在现有表中添加了一个新列(列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

此查询为新父列提供了多个值(多行)。

但上面的代码给出了以下错误:

  

无法将值NULL插入列' acadid',表中   ' tempdb.dbo.table1_的 _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _000000014F2B' ;列不允许空值。 INSERT失败。声明一直如此   终止。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

您收到此错误消息的原因是您使用 alter table 语句向 table1 表添加了一个新列,而不是更新此值对于现有行的列,您要添加的新行只有此列填充了 insert 语句,并且在另一列上可能存在不允许空值的约束(主键约束?)。 / p>

除此之外,您可能希望使用更新更新现有行的新列的值,如:

update table1
set table1.new_parent = table2.parent
from table1 inner join table2 on table2.orgid=table1.orgunitid