Sql 如何防止update语句将某些记录设置为null?

Sql 如何防止update语句将某些记录设置为null?,sql,oracle,sql-update,Sql,Oracle,Sql Update,我正在尝试使用下面的查询对一个表和一个现有表进行更新。代码列记录需要更新,但只更新其中的一部分。我的查询似乎有效,但任何本应留在代码列中的记录最终都被设置为NULL。如何修改此查询以保持这些记录不变 质疑 表1:待更新的代码 t1 address city state flag code 123 aaa il b 400 567 bbb il b 400 345

我正在尝试使用下面的查询对一个表和一个现有表进行更新。代码列记录需要更新,但只更新其中的一部分。我的查询似乎有效,但任何本应留在代码列中的记录最终都被设置为NULL。如何修改此查询以保持这些记录不变

质疑

表1:待更新的代码

   t1               
address city    state   flag    code
123      aaa      il     b       400
567      bbb      il     b       400
345      bbb      il     b      -500
789      ddd      il     b       600
546      ccc      il     b       700
表2:用于更新T1的代码列

t2              
address city    state   flag    code
   123   aaa      il      b     -555
   444   bbb      il      b     -555
   345   bbb      il      b     -555
   888   kkk      il      b     -555
   546   ccc      il      b     -555



What the query currently outputs

     current output             
    address city    state   flag    code
       123   aaa      il      b     400
       444   bbb      il      b     NULL
       345   bbb      il      b     -500
       888   kkk      il      b     NULL
       546   ccc      il      b     -700
我希望查询保持未触及的记录,这些记录与更新表中的记录不匹配,如下所示

  What I want               
address city    state   flag    code
  123    aaa      il      b     400
  444    bbb      il      b    -555
  345    bbb      il      b    -500
  888    kkk      il      b    -555
  546    ccc      il      b    -700

谢谢

最好的方法是在
where
子句中使用
exists

update t1 x
    set x.code = (select code
                  from t2 y
                  where x.address = y.address and x.city = y.city and x.state = y.state and x.flag = y.flag and rownum = 1
                 )
    where x.aflag like '%b%' and
          exists (select code
                  from t2 y
                  where x.address = y.address and x.city = y.city and x.state = y.state and x.flag = y.flag and rownum = 1
                 );

最好的方法是在
where
子句中使用
exists

update t1 x
    set x.code = (select code
                  from t2 y
                  where x.address = y.address and x.city = y.city and x.state = y.state and x.flag = y.flag and rownum = 1
                 )
    where x.aflag like '%b%' and
          exists (select code
                  from t2 y
                  where x.address = y.address and x.city = y.city and x.state = y.state and x.flag = y.flag and rownum = 1
                 );

t2
将同时告诉您要更新的行和值。为此,请编写updatetable查询:

update
(
  select t1.code, t2.code as new_code
  from t1
  join t2 on  t2.address = t1.address
          and t2.city    = t1.city city 
          and t2.state   = t1.state
          and t2.flag    = t1.flag
  where t1.flag like '%b%'
)
set code = new_code;
要使其正常工作,
地址、城市、州、标志
t2
中必须是唯一的,因此DBMS知道它正获得一条包含更新数据的记录


Updatetable查询在需要更新多个列的情况下尤其有用。

t2
将告诉您要更新哪些行以及使用哪些值。为此,请编写updatetable查询:

update
(
  select t1.code, t2.code as new_code
  from t1
  join t2 on  t2.address = t1.address
          and t2.city    = t1.city city 
          and t2.state   = t1.state
          and t2.flag    = t1.flag
  where t1.flag like '%b%'
)
set code = new_code;
要使其正常工作,
地址、城市、州、标志
t2
中必须是唯一的,因此DBMS知道它正获得一条包含更新数据的记录


Updatetable查询特别适用于需要更新多个列的情况。

有些人喜欢,有些人不喜欢。它与您最初的update语句惊人地相似:

MERGE INTO t1 x
USING (SELECT DISTINCT address, city, state, flag, code from t2) y
   ON (x.address = y.address AND x.city = y.city AND x.state = y.state AND x.flag = y.flag)
 WHEN MATCHED THEN 
      UPDATE SET x.code = y.code
       WHERE x.flag LIKE '%b%';

有些人喜欢,有些人不喜欢。它与您最初的update语句惊人地相似:

MERGE INTO t1 x
USING (SELECT DISTINCT address, city, state, flag, code from t2) y
   ON (x.address = y.address AND x.city = y.city AND x.state = y.state AND x.flag = y.flag)
 WHEN MATCHED THEN 
      UPDATE SET x.code = y.code
       WHERE x.flag LIKE '%b%';

一个where子句说where代码不是空的,你说你想更新t1,但是你显示了t2的地址,还有一个where子句说where代码不是空的,你说你想更新t1,但是你显示了t2的地址??