Oracle UPDATE比SELECT更新更多行?

时间:2018-04-28 16:28:01

标签: sql oracle

我有这个查询来列出订单号,订单上的电子邮件地址和主数据中的发票地址。基本上我的想法是,我需要更新任何未开票的订单,并使用主数据中的发票地址复制订单上的发票地址(因为订单创建后它已更改等)。

我写了这个SELECT,列出了预期的结果,大约25个左右。我已经手动验证了每个,这是需要更新的数据。

  select auf_adr.auf_nr, auf_adr.email, kust_adr.ku_email
     from auf_adr,auf_kopf,
          kust_adr
     where auf_adr.auf_nr = auf_kopf.auf_nr
       and auf_kopf.kunr = kust_adr.ku_nr
       and auf_adr.adr_art = 2
       and kust_adr.ku_adr_art = 1
       and auf_adr.email != kust_adr.ku_email
       and
         (select sum(auf_stat.rg_anz)
          from auf_stat
          where auf_stat.auf_nr = auf_kopf.auf_nr) = 0;

太好了,我将它转换为UPDATE,但它更新了2487385行!我做错了什么?

update auf_adr
  set email =
    (select kust_adr.ku_email
     from auf_kopf,
          kust_adr
     where auf_adr.auf_nr = auf_kopf.auf_nr
       and auf_kopf.kunr = kust_adr.ku_nr
       and auf_adr.adr_art = 2
       and kust_adr.ku_adr_art = 1
       and auf_adr.email != kust_adr.ku_email
       and
         (select sum(auf_stat.rg_anz)
          from auf_stat
          where auf_stat.auf_nr = auf_kopf.auf_nr) = 0);

3 个答案:

答案 0 :(得分:1)

据推测,你打算这样:

update auf_adr
  set email = (select k.ku_email
               from auf_kopf k join
                    kust_adr ka
                    on k.kunr = ka.ku_nr
               where auf_adr.auf_nr = k.auf_nr
                     ka.ku_adr_art = 1 and
                     auf_adr.email <> ka.ku_email and
                     (select sum(s.rg_anz)
                      from auf_stat s
                      where s.auf_nr = k.auf_nr
                     ) = 0
              )
    where auf_adr.adr_art = 2 and
          exists (select 1
                  from auf_kopf k join
                       kust_adr ka
                       on k.kunr = ka.ku_nr
                  where auf_adr.auf_nr = k.auf_nr
                        ka.ku_adr_art = 1 and
                        auf_adr.email <> ka.ku_email and
                        (select sum(s.rg_anz)
                         from auf_stat s
                         where s.auf_nr = k.auf_nr
                        ) = 0
                  );

答案 1 :(得分:0)

如果我从oracle中的select更新 我会用

update (
   select * from yourtable 
) set "yourcol" = 'yourvalue'

示例:

CREATE TABLE TestTable
    ("col" int, "col2" int, "col3" int)
;

INSERT ALL 
    INTO TestTable ("col", "col2", "col3")
         VALUES (1, 2, 3)
    INTO TestTable ("col", "col2", "col3")
         VALUES (2, 3, 4)
    INTO TestTable ("col", "col2", "col3")
         VALUES (3, 4, 5)
    INTO TestTable ("col", "col2", "col3")
         VALUES (4, 5, 6)
    INTO TestTable ("col", "col2", "col3")
         VALUES (5, 6, 7)
    INTO TestTable ("col", "col2", "col3")
         VALUES (6, 7, 8)
    INTO TestTable ("col", "col2", "col3")
         VALUES (7, 8, 9)
    INTO TestTable ("col", "col2", "col3")
         VALUES (8, 9, 10)
    INTO TestTable ("col", "col2", "col3")
         VALUES (9, 10, 11)
    INTO TestTable ("col", "col2", "col3")
         VALUES (10, 11, 12)
    INTO TestTable ("col", "col2", "col3")
         VALUES (11, 12, 13)
    INTO TestTable ("col", "col2", "col3")
         VALUES (12, 13, 14)
    INTO TestTable ("col", "col2", "col3")
         VALUES (13, 14, 15)
SELECT * FROM dual
;

--Update Data
update (
  select * from TestTable
) set "col" = "col2" * 3;

select * from TestTable;

SQL小提琴链接:http://sqlfiddle.com/#!4/82c54/3/1

答案 2 :(得分:0)

这是因为更新查询中没有where条件。

您的查询语法为:

UPDATE tablename
SET email = (some query);

这将更新表格中的所有记录