So I have a work order table with 3 fields relevant to the question being asked here
(dbo.workorder with some sample values):
location | supervisor | ownergroup
ABC-123 | JSMITH | ALPHA
XYZ-987 | JDOE | OMEGA
ABC-123 | NULL | NULL
XYZ-987 | NULL | NULL
The last two rows are to show that sometimes the supervisor/ownergroup is not filled out when inserting a workorder row which leads to the question! I have another table called “roles” with rows such as below:
role | value
ABCSupervisor | JSMITH
ABCOwnergroup | ALPHA
XYZSupervisor | JDOE
XYZOwnergroup | OMEGA
As you can see from the “roles” table, a role is made up of the first 3 letters of the location (ALWAYS) plus the word Supervisor or Ownergroup. When the 3rd and 4th rows of the “workorder” table are inserted, I’d like to develop a trigger that would attempt to fill in the values for Supervisor/Ownergroup IF there is a match in the “roles” table. If there isn’t a supervisor/ownergroup for that location prefix, then it should default to a set value (let’s say supervisor=’super’ and ownergroup=’og’). Here’s what I have so far, though perhaps a different approach would be better:
CREATE TRIGGER [dbo].[OwnergroupSupervisor]
ON [dbo].[workorder]
AFTER INSERT,UPDATE
AS
BEGIN
SET NOCOUNT ON;
UPDATE wo
SET wo.ownergroup= (THIS IS WHERE I NEED HELP)
FROM dbo.workorder AS wo INNER JOIN inserted AS i ON wo.wonum=i.wonum
WHERE wo.ownergroup IS NULL
END
GO
I’m guessing an IF EXISTS or a CASE of some sort? Probably involving something like LEFT(wo.location,3)+’Ownergroup’ perhaps?
Any and all help is greatly appreciated! Thanks!
Clarification:
wonum is the primary key for the “workorder” table. I am joining the “inserted” table (which contains the rows that are being inserted or updated) to the “workorder” table so that I am only updating the new/updated rows and not the entire “workorder” table. Nothing in my question right now has anything to do with the “roles” table. See update 1 below for a better understanding of what I’m doing…
Update 1:
I’ve worked out one solution, but if there is nothing in the “roles” table, it just leaves the supervisor/ownergroup null instead of changing it to a default value. I can deal with this for now, but would like a better option. Here’s what I have:
CREATE TRIGGER [dbo].[OwnergroupSupervisor]
ON [dbo].[workorder]
AFTER INSERT,UPDATE
AS
BEGIN
SET NOCOUNT ON;
UPDATE wo
SET wo.ownergroup=(SELECT value FROM roles WHERE role=LEFT(wo.location,3)+'Ownergroup')
FROM dbo.workorder AS wo INNER JOIN inserted AS i ON wo.wonum=i.wonum
WHERE wo.ownergroup IS NULL
UPDATE wo
SET wo.supervisor=(SELECT value FROM roles WHERE role=LEFT(wo.location,3)+'Supervisor')
FROM dbo.workorder AS wo INNER JOIN inserted AS i ON wo.wonum=i.wonum
WHERE wo.supervisor IS NULL
END
I have not worked out what would happen if two rows were found in the roles table (although I’m pretty sure the application restricts this so it should be OK). But as I mentioned above, this just keeps the supervisor/ownergroup NULL if no role is found in the roles table.
How you can update the workorder table based on information in roles is as below:
Then you can do a similar update for the Supervisor role.
In theory, the full answer is something like: