SQL update multi-Table Association

Source: Internet
Author: User

1. Create a test table
Create table t_user (
ID int identity primary key,
UserID varchar (50) not null,
UserName varchar (50) null,
DeptID int not null,
Phone varchar (50) null,
Fax varchar (50) null
)

Create table t_dept (
ID int identity primary key,
DeptName varchar (50) null,
Phone varchar (50) null,
Fax varchar (50) null
)

INSERT t_user
Select n '001', N 'zhang san', 1, N '000000', N '000000'
UNIONAll select n '002 ', N 'Li si', 2, N '000000', N '000000'
UNIONAll select n '003 ', N 'wang wu', 2, N '000000', N '000000'
UNIONAll select n '004 ', N 'zhao 6', 3, N '000000', N '000000'

INSERT t_dept
Select n 'dashboard ', N '123', N '123'
UNIONAll select n 'marketing Department ', N '000000', N '000000'
UNIONAll select n 'aftersales Department ', N '000000', N '000000'

Ii. SQL update (t_user data is restored after each update)

1. update t_user set u. phone = d. phone, u. fax = d. fax from t_user u, t_dept d where u. deptID = d. ID

Error: Unable to bind the identifier "u. phone" consisting of multiple parts ".
2. update u set u. phone = d. phone, u. fax = d. fax from t_user u, t_dept d where u. deptID = d. ID

 

 Correct

Select * from t_user

 

 ID  UserID  UserName  DeptID  Phone  Fax
 1  001  Zhang San  1  88888011  99999011
 2  002  Li Si  2  88888022  99999022
 3  003  Wang Wu  2  88888022  99999022
 4  004  Zhao Liu  3  88888033  99999033

 

 

3. update t_userU set u. phone = d. phone, u. fax = d. fax from t_dept d where u. deptID = d. ID

Error: syntax error near 'u.

4. update t_user set phone = d. phone, fax = d. fax from t_dept d where deptID = d. ID

Correct. The result is the same as 2.

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.