Oracle batch update

Source: Internet
Author: User

Requirements:

Update the same records in Table t1 (t_statbuf) and Table t1 (T_Mt) to table t1.

1. incorrect syntax:

[SQL]
  1. 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]
  1. 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]
  1. UpdateMy_time_test1 t1Set(MDATE, disallow) = (SelectMDATE, disallowFrom
  2. My_time_test t2WhereT1.disparts = t2.disparts)WhereExists (Select1From
  3. My_time_test t2WhereT1.disparts = t2.disparts );

My business statement:

[SQL]
  1. UpdateT_Mt t1Set(Stat, OStat, RptTime) = (
  2.  SelectStat, Stat, RptTimeFromT_statbuf t2WhereT1.MsgId = t2.MsgId)WhereExists (
  3.  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.