oracle batch update

I personally think it's well written

http://blog.csdn.net/wanglilin/article/details/7200201

need:

Update the records with the same id in the t2 (t_statbuf) table as the t1 (T_Mt) table into the t1 table.

1. Wrong spelling:

 1 update table_name t1 set (a,b,c)=( select a,b,c from table_name_2 t2 where t1.a=t2.a);  

This way of writing will update all rows in the t1 table: if t1.a=t2.a, the records found in t2 will be updated into t1; if t1.a<>t2.a, the records in t1 will be updated. is updated to be empty (null).

Correct spelling:

1 update table_name t1 set (a,b,c)=( select a,b,c from table_name_2 t2 where t1.a=t2.a)
2 where exists(select 1 from table_name_2 t2 where t1.a=t2.a);  

Parse:

The correct way to write it is to add a sentence where exists(select 1 from table_name_2 t2 where t1.a=t2.a);

The meaning of this sentence is: if there is t1.a=t2.a, it will be updated, otherwise, it will not be updated, so it will not cause all records in the t1 table to be updated.

example:

update table_name_1 set (a,b) = (select 1,2 from dual where 1=2);

This result will update all records in table_name_1 to empty (null), because the latter 1=2 does not hold.

Summarize:

When updating, we must clarify the qualifications and test!

My test statement:

1 update my_time_test1 t1 set (MDATE,DISCRIPT) =(select MDATE,DISCRIPT from   
2 my_time_test t2 where t1.DISCRIPT=t2.DISCRIPT) where exists (select 1 from   
3 my_time_test t2 where t1.DISCRIPT=t2.DISCRIPT);  

My business statement:

1 update T_Mt t1 set (Stat,OStat,RptTime) =(  
2 select Stat,Stat,RptTime from t_statbuf t2 where t1.MsgId=t2.MsgId) where exists(  
3 select 1 from t_statbuf t2 where t1.MsgId=t2.MsgId); --If there are t1 and t2 equal, update it. Without where exists, it is updated regardless of whether it exists or not. If it does not exist, the result will be updated to empty, but the record is still there.  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325020749&siteId=291194637