Once very good friend of mine asked me how to update an employee master table having fields
emp_id,
emp_name,
department_id, (refer to dept_id of Department_Master table)
department_name
where department_name fields is null and he want to update department_name with the correspondent department_id.
The solution is very simple i told him to do it by cursor or using temporary table and loop through the temp table.
But today when i was toying with MERGE i found one more solution to same question, i have yet not executed it but i m pretty confident it will work and it is the best solution to the above question
MERGE Employee_Master AS ed
USING (SELECT dept_id,dept_name FROM Department_Master) AS dm
ON dm.dept_id = em.dept_id
WHEN MATCHED THEN UPDATE SET em.dept_name = dm.dept_name;
Just try it n see the result.
Happy coding :)
Post Reference: Vikram Aristocratic Elfin Share
emp_id,
emp_name,
department_id, (refer to dept_id of Department_Master table)
department_name
where department_name fields is null and he want to update department_name with the correspondent department_id.
The solution is very simple i told him to do it by cursor or using temporary table and loop through the temp table.
But today when i was toying with MERGE i found one more solution to same question, i have yet not executed it but i m pretty confident it will work and it is the best solution to the above question
MERGE Employee_Master AS ed
USING (SELECT dept_id,dept_name FROM Department_Master) AS dm
ON dm.dept_id = em.dept_id
WHEN MATCHED THEN UPDATE SET em.dept_name = dm.dept_name;
Just try it n see the result.
Happy coding :)
Post Reference: Vikram Aristocratic Elfin Share
No comments:
Post a Comment