簡體   English   中英

使用現有數據的SQL INSERT \\ UPDATE查詢

[英]SQL INSERT\UPDATE query using existing data

我正在將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

有人可以提供正確的例子嗎?

也許是這樣的嗎?

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'

您會收到錯誤消息,因為您沒有填寫所有非空列。 表中至少必須有一列不可為空且沒有默認值的列。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM