SQL Server batch Modification

Source: Internet
Author: User

1. When using the data in one table to update the data in another table, the T-SQL provides a variety of writing methods (two listed below), but it is recommended to use the first method, although the traditional, but the structure is clear.

Note that when you use data in one table to update data in another table, the two tables must be associated!

1.

Update T1
        Set t1.c2 = t2.c2
      From T2

Where t1.c1 = t2.c1

2.

Update T1
        Set t1.c2 = t2.c2

From T1 inner join T2
        On t1.c1 = t2.c1

Example:

UPDATE temp_Org_employee   SET temp_Org_employee.mobile    = c.tele  FROM   temp_Org_employee e       INNER JOIN          (SELECT a.name, b.ID, a.tele, b.mobile             FROM    EmplTeleTb a                  LEFT JOIN                     (SELECT *                        FROM temp_Org_employee                       WHERE DepartmentId IN                                   (SELECT ID                                      FROM org_Department                                     WHERE CompanyID =                                              'T200812051418466710000194')) b                  ON a.Name = b.EmplName            WHERE a.tele <> b.mobile) c       ON e.ID = c.ID

Example 2:

UPDATE temp_Org_employee    SET temp_Org_employee.mobile    = c.tele  FROM (SELECT a.name, b.ID, a.tele, b.mobile          FROM    EmplTeleTb a               LEFT JOIN                  (SELECT *                     FROM temp_Org_employee                    WHERE DepartmentId IN                                (SELECT ID                                   FROM org_Department                                  WHERE CompanyID = 'T200812051418466710000194'))                  b               ON a.Name = b.EmplName         WHERE a.tele <> b.mobile) c WHERE temp_Org_employee.ID = c.ID

Example 3:

UPDATE temp_Org_employee    SET mobile = c.tele  FROM temp_Org_employee e,(SELECT a.name, b.ID, a.tele, b.mobile          FROM    EmplTeleTb a               LEFT JOIN                  (SELECT *                     FROM temp_Org_employee                    WHERE DepartmentId IN                                (SELECT ID                                   FROM org_Department                                  WHERE CompanyID = 'T200812051418466710000194'))                  b               ON a.Name = b.EmplName         WHERE a.tele <> b.mobile) c WHERE e.ID = c.ID

2. the alias of the table specified in the from clause cannot be set
Column_name
The qualifier of the modified field in the clause.
    For example, the following content is invalid:

Update temp_org_employee
Set E. Mobile = C. Tele
From temp_org_employee E, (select a. Name, B. ID, A. Tele, B. Mobile
From emplteletb
Left join
(Select *
From temp_org_employee
Where else mentid in
(Select ID
From org_department
Where companyid = 't20081205141846670000194 '))
B
On a. Name = B. emplname
Where a. Tele <> B. Mobile) c
Where E. ID = C. ID

To make the preceding example legal, delete alias E from the column name or use its own table name.

Related Article

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.