Check - Compare> Refresh SQL Table

I would like to write a stored procedure where I can update the table based on the following criteria.

Table 
EmployeeID  GroupID Group#
123     G123        3
456     G456        3
789     G789        3
101     G101        3

View
GroupID_Granular    GroupID_Middle  GroupID_Executive
G123            M123            E123
G789            M789            E789

If the GroupID is found in the "Views GroupID_Granular" column, refresh the table, set GroupID = GroupID_Executive and set Group No. 1.

I am not sure how to check / compare and then run Update cmd.

thank

+5
source share
1 answer

This can be done easily with JOIN:

UPDATE t
SET 
    t.GroupID = v.GroupID_Executive, 
    t.[Group#] = 1
FROM YourTable t
JOIN YourView v ON v.GroupID_Granular = t.GroupID 

Sql Fiddle Here

+5
source

All Articles