Update association Other tables Bulk update data

Source: Internet
Author: User

When you update a table in bulk, you need to rely on a different table when you update a column of a table, which can be a where conditional clause, or the value of the field that you want to update depends on another table

There are usually two ways of doing
1. Using Stored Procedures
2. Loop through the program code
Here is a more efficient, concise approach, batch update SQL, a SQL can replace the troublesome loop process, with MS SQL Server, Oracle, DB2 under the wording
--Key points: T4 and T1 are the same table,primary key.
-and associate it so that you can refer to the fields of the table you want to update in the SELECT statement
UPDATE Table1 as T1
SET (field1,field2) = (SELECT Field21, Field22
From Table2 T2
INNER JOIN Table3 T3
on T3. Field31 = t2. Field23
INNER JOIN Table4 T4
on T3. FIELD32 = T4. Filed41
WHERE T2. Field24 >= '
and T1.fid = T4.fid);
----------------------------MS SQL Server--------------------------------------

UPDATE T1
SET Field1 = Field21, Field2 = Field22
From Table2 T2
INNER JOIN Table3 T3
on T3. Field31 = t2. Field23
INNER JOIN Table4 T4
on T3. FIELD32 = T4. Filed41
WHERE (T2. FIELD24 >= ")
and T1.fid = T4.fid);
----------------------------Oracle--------------------------------------------

UPDATE Table1 T1
SET (field1,field2) = (SELECT Field21, Field22
From Table2 T2
INNER JOIN Table3 T3
on T3. Field31 = t2. Field23
INNER JOIN Table4 T4
on T3. FIELD32 = T4. Filed41
WHERE (T2. FIELD24 >= ")
and T1.fid = T4.fid))
WHERE EXISTS (SELECT Field21, Field22
From Table2 T2
INNER JOIN Table3 T3
on T3. Field31 = t2. Field23
INNER JOIN Table4 T4
on T3. FIELD32 = T4. Filed41
WHERE (T2. FIELD24 >= ")
and T1.fid = T4.fid));
---------------------------------DB2------------------------------------------

UPDATE Table1 as T1
SET (field1,field2) = (SELECT Field21, Field22
From Table2 T2
INNER JOIN Table3 T3
on T3. Field31 = t2. Field23
INNER JOIN Table4 T4
on T3. FIELD32 = T4. Filed41
WHERE (T2. FIELD24 >= ")
and T1.fid = T4.fid))
WHERE EXISTS (SELECT Field21, Field22
From Table2 T2
INNER JOIN Table3 T3
on T3. Field31 = t2. Field23
INNER JOIN Table4 T4
on T3. FIELD32 = T4. Filed41
WHERE (T2. FIELD24 >= ")
and T1.fid = T4.fid));

-----Reproduced from the blog park

Update association Other tables Bulk update data

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.