Thursday 20 October 2011

Update a table with another table's data with a conditions in sql server


Update a table with another table's data with a conditions in sql server


This is the example to update table from another table where Empid is same and according some conditions.

We have to update 'tblEmpdetails' table
'Acno' as mention on second table 'tblEmpAcNo' with the condition that department must be 'Hr', Both table given bellow and updating query also given bellow.

Table 1 :- tblEmpDetails  
EmpIdageDepAcno
E120IT09876565
E230Hr75445678
E331Hr45674570
Table2 :- tblEmpAcNo 
EmpIdAcno
E102537565
E285245678
E341546456
E434074570
E545379200


UPDATE tblEmpDetails SET tblEmpDetails.AcNo = tblEmpAcNo.AcNo
FROM tblEmpAcNo
WHERE tblEmpDetails.EmpId = tblEmpAcNo.EmpId
AND tblEmpDetails.Dept = 'Hr'
GO

No comments:

Post a Comment