SQL插入\使用现有数据更新查询

SQL插入\使用现有数据更新查询,sql,oracle,sqlplus,Sql,Oracle,Sqlplus,我将SQL*Plus与Oracle8i企业版8.1.7.4.0一起使用 我有一个表客户地址: 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.

我将SQL*Plus与Oracle8i企业版8.1.7.4.0一起使用

我有一个表
客户地址

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

当我执行类似
的测试时,将值('1','1','2')插入到客户地址(no,type,email)中test@domain.com');我看到如下错误消息:

ORA-01400: cannot insert NULL into

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

也许是这样的

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

如果以正确的顺序包含所有字段,也可以避免在表名之后指定它们

INSERT INTO customer_address
     SELECT no,
            '1',
            email, 
            otherfields
       FORM customer_address
      WHERE cunu ='1'
        AND type='0'

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

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

您确定表中没有其他不可为空的列吗?谢谢。是的,还有另外两列不为NULL,但都是“0”,我想我还是需要包含它们。但复制数据的正确示例是什么?似乎是因为行已经存在(不知何故)而导致错误,因此我需要的是
更新
而不是
插入
。有人可以修改示例吗?您不能复制主键。可能你的pk是由一个序列生成的。在这种情况下,当您选择新值时,您应该写入customer_address_table_sequence.nextval,而不是字段名称。谢谢,但您能在示例中解释一下吗?我修改了答案。因此,如果客户编号是主键,我将使用
no.nextval
?实际写入
nextval
?系统中可能有一个序列生成客户表no字段的id。但我不知道序列的名称。如果您可以找到原始的insert查询,那么应该在那里提到它。或者,您可以查看“所有序列”表中的序列列表。