About updating SQL

Source: Internet
Author: User

A, field:
Sdeptkey: Master Region
Sdeptid
Sdeptname: the name of the local device.
Employee table B, field:
Sempkey: Master Region
Sdeptkey: foreign key: A. sdeptkey
Sdeptid
Sempid: employee ID
Sempname: Name

Now B. sdeptkey has expired, and the relationship between B. deptid = A. deptid is required. Modify it.
There are two methods to update SQL in SQL Server:
1. SQL Server-specific explain Method
Update B set sdeptkey = A. sdeptkey from a where a. sdeptid = B. sdepid

2. standard SQL
Update B set sdeptkey = (select sdeptkey from a where a. sdeptid = B. sdeptid)

In SQL Server, the two regions are different:
Region:
The first method is faster, and SQL server has a reverse join (which will take advantage of the index) processing.
The second method is slower. If the value is not found, the system returns NULL and the update value is null.

Region 2:
The data volume of Table B is as follows:
Sempkey, sdeptkey, sdeptid...
---------------------------
001,789, A01...
002,456, A01...
003,002, A02...
004,003, a03...
---------------------------

The data in table A is as follows:
Sdeptkey, sdeptid...
---------------------------
001, A01
---------------------------

After the first update SQL statement is executed in rows, the result of Table B is as follows:
Sempkey, sdeptkey, sdeptid...
---------------------------
001,001, A01...
002,001, A01...
003,002, A02...
004,003, a03...
---------------------------
After the second update SQL statement is executed in rows, the result of Table B is as follows:
Sempkey, sdeptkey, sdeptid...
---------------------------
001,001, A01...
002,001, A01...
003, null, A02...
004, null, a03...
---------------------------
That is to say,
Update B set sdeptkey = A. sdeptkey from a where a. sdeptid = B. sdepid
When using the SQL standard syntax, you should add a where clause where exists (select * from a where a. sdeptid = B. sdeptid), that is:
Update B set sdeptkey = (select sdeptkey from a where a. sdeptid = B. sdeptid)
Where exists (select * from a where a. sdeptid = B. sdeptid)

In Oracle, the update SQL statement of the benchmark is not the same as that of SQL Server.
 

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.