2013-10-29 24 views
0

我在Oracle8i企业版版本8.1.7.4.0中使用SQL * Plus。使用现有数据的SQL INSERT UPDATE查询

我有一个表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,[email protected] 

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

当我不喜欢INSERT INTO customer_address (no, type, email) VALUES ('1','1','[email protected]');一个测试,我看到这样的错误消息:

ORA-01400: cannot insert NULL into 

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

+4

你确定你的表中没有其他不可空列吗? – mucio

+0

谢谢。是的,还有另外两列NOT NULL,但都是'0',我想我需要包括那些。什么是复制数据的正确例子? – user2656114

+0

看来错误是由于该行已经存在(不知何故)造成的,所以我需要的是'UPDATE'而不是'INSERT'。有人可以修改一个例子吗? – user2656114

回答

0

也许是这样的?

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

谢谢。我得到'ORA-00001:唯一约束'? – user2656114

+0

您无法复制主键。可能你的pk是由一个序列产生的。在这种情况下,当您选择新值时,您应该编写customer_address_table_sequence.nextval而不是字段的名称。 –

+0

谢谢,但你能解释一下,因为我不明白? – user2656114

0

如果包括来自该国的正确顺序的所有字段,你也可避免在表名后指定它们

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

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

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