使用现有数据的SQL INSERT \ UPDATE查询

时间:2013-10-29 13:30:08

标签: sql oracle sqlplus

我正在使用SQL * Plus和Oracle8i企业版8.1.7.4.0版。

我有一张表customer_address

no - customer number

type - 0 main, 1 delivery, 2 invoice

email - customer e-mail address

每个客户的电子邮件地址都设置为类型0,例如:

SELECT no, type, email FROM customer_address WHERE cunu = '1';

1,0,customer@domain.com

我需要为0到1类的每个客户复制电子邮件地址吗?

当我像INSERT INTO customer_address (no, type, email) VALUES ('1','1','test@domain.com');那样进行测试时,我会看到如下错误消息:

ORA-01400: cannot insert NULL into

有人能提供正确的例子吗?

3 个答案:

答案 0 :(得分:0)

也许是这样的?

insert into customer_address
   (no,type,email, primarykeyfieldofthetable, otherfields)
select
   no,'1',email, sequenceofprimarykey.nextval, otherfields
from
  customer_address
where type='0'

答案 1 :(得分:0)

如果按正确顺序包含所有字段,也可以避免在表名

之后指定它们
INSERT INTO customer_address
     SELECT no,
            '1',
            email, 
            otherfields
       FORM customer_address
      WHERE cunu ='1'
        AND type='0'

答案 2 :(得分:0)

您收到错误,因为您没有填写所有非空列。表中必须至少有一列不可为空并且没有默认值。

insert into customer_address (no, type, email, notnullcol1, notnullcol2)
  select no, 1 as type, email, notnullcol1, notnullcol2
  from customer_address
  where type = 0
;
相关问题