Requirements:
Update the same records in Table t1 (t_statbuf) and Table t1 (T_Mt) to table t1.
1. incorrect syntax:
[SQL]
- UpdateTable_name t1Set(A, B, c) = (SelectA, B, cFromTable_name_2 t2WhereT1.a = t2.a );
In this way, all rows in Table t1 will be updated: If t1.a = t2.a, the records found in Table T2. if t1.a <> t2.a, records in t1 are updated to null ).
Correct syntax:
[SQL]
- UpdateTable_name t1Set(A, B, c) = (SelectA, B, cFromTable_name_2 t2WhereT1.a = t2.a)WhereExists (Select1FromTable_name_2 t2WhereT1.a = t2.a );
Resolution:
The correct method is to add a sentenceWhere exists (select 1 from table_name_2 t2 where t1.a = t2.a );
If t1.a = t2.a exists, it is updated. Otherwise, it is not updated, so it will not cause all records in Table t1 to be updated.
Example:
Update table_name_1 set (a, B) = (select 1, 2 from dual where 1 = 2 );
In this result, all records in table_name_1 are updated to null because 1 = 2 is not valid.
Summary:
During update, you must confirm the conditions and test the conditions!
My test statement:
[SQL]
- UpdateMy_time_test1 t1Set(MDATE, disallow) = (SelectMDATE, disallowFrom
- My_time_test t2WhereT1.disparts = t2.disparts)WhereExists (Select1From
- My_time_test t2WhereT1.disparts = t2.disparts );
My business statement:
[SQL]
- UpdateT_Mt t1Set(Stat, OStat, RptTime) = (
- SelectStat, Stat, RptTimeFromT_statbuf t2WhereT1.MsgId = t2.MsgId)WhereExists (
- Select1FromT_statbuf t2WhereT1.MsgId = t2.MsgId );-- If t1 and t2 are equal, update them. If the where exists clause is not added, it is updated and does not exist no matter whether the storage does not exist. The result is updated to null, but the record is still