SQL update statement

siamandm
BronzeLounger
Posts: 1267
Joined: 01 May 2016, 09:58

SQL update statement

Post by siamandm »

Hi
if i have a table called tblEmployee with these fields
ID autonumber , PK
EmployeeCode int
Salary int
JobTitle short text


now if have another table with the same fields but with new and updated information,
how to write a sql update statement in order to update the original table with new salary and job title?


Regrads

User avatar
HansV
Administrator
Posts: 78628
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SQL update statement

Post by HansV »

Can we match the old and new table on ID? Or on EmployeeCode?
Best wishes,
Hans

siamandm
BronzeLounger
Posts: 1267
Joined: 01 May 2016, 09:58

Re: SQL update statement

Post by siamandm »

the old and new table should match on employeeCode
so employeecode=0001 in the old table is same as the employeecode=0001 in the new table

regards

User avatar
HansV
Administrator
Posts: 78628
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SQL update statement

Post by HansV »

Try this - let me know if you get an error:

UPDATE tblEmployee INNER JOIN tblOther ON tblEmployee.EmployeeCode=tblOther.EmployeeCode SET tblEmployee.Salary=tblOther.Salary, tblEmployee.JobTitle=tblOther.JobTitle

where tblOther is the name of the table with the updated information.
Best wishes,
Hans

siamandm
BronzeLounger
Posts: 1267
Joined: 01 May 2016, 09:58

Re: SQL update statement

Post by siamandm »

so the old and the updated table should have an inner join relationship!
thanks a lot, i will try this

User avatar
HansV
Administrator
Posts: 78628
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SQL update statement

Post by HansV »

You only need to specify the join in the query.
Best wishes,
Hans

siamandm
BronzeLounger
Posts: 1267
Joined: 01 May 2016, 09:58

Re: SQL update statement

Post by siamandm »

Thanks a lot very helpful

Regards

siamandm
BronzeLounger
Posts: 1267
Joined: 01 May 2016, 09:58

Re: SQL update statement

Post by siamandm »

HansV wrote:Try this - let me know if you get an error:

UPDATE tblEmployee INNER JOIN tblOther ON tblEmployee.EmployeeCode=tblOther.EmployeeCode SET tblEmployee.Salary=tblOther.Salary, tblEmployee.JobTitle=tblOther.JobTitle

where tblOther is the name of the table with the updated information.
if I want to update the old table based on two criteria like this sample below, how do it please
Capture.PNG
many thanks
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78628
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SQL update statement

Post by HansV »

That would be

UPDATE NewData INNER JOIN OldData ON NewData.EmployeeCode=OldData.EmployeeCode AND NewData.Item_Code=OldData.Item_Code SET OldData.Qty=NewData.Qty

Please test on a copy of the database first.
Best wishes,
Hans

siamandm
BronzeLounger
Posts: 1267
Joined: 01 May 2016, 09:58

Re: SQL update statement

Post by siamandm »

thanks a lot you are star :)