views:

38

answers:

1

I am working with an IBM Maximo database that issues workorders automatically. When the workorders are issued (aka Inserted into the database workorder table), I would like to auto-assign a supervisor, owner, and owner group based on a set of criteria. This only needs to happen IF the supervisor, owner, and owner group aren't already assigned. Often times a "parent workorder" has the information, but it needs to be copied into the "child" workorders (as you will see in the criteria below). The criteria for ALL of the triggers is:

WHERE status<>'COMP'
AND historyflag=0
AND istask=0

Here is the criteria for the trigger:

-If the Owner Group and Supervisor have a value, skip the record. (Do nothing)

-If the Owner Group and/or Supervisor is blank or null, and the corresponding PARENT Work Order field is not Null, copy the Owner Group and/or Supervisor from the PARENT Work Order record.

-If the Parent Work Order Owner Group and/or Supervisor is blank or null, then assign the Owner Group and Supervisor per the table values below: (I have removed names for security's sake, but all the columns are correct, i.e. B3 is supposed to have SuperA as the supervisor)

Site / OwnerGroup / Supervisor
ABC / @ABCGroup / @ABCSupervisor
DEF / @DEFGroup / @DEFSupervisor

**NOTE: SITE is not a table column, it is really the first 3 characters of the workorder.location field. For example, the location could be ABC-1234, meaning it is at site ABC, building 1234 (unfortunately, these are NOT stored in separate columns, they are only present together in the location column). In this SQL query, all buildings at a location are serviced by the same ownergroup/supervisor, so all other queries we currently use are using workorder.location='ABC%'

I've done plenty of selects, updates, and stored procedures, but this is my first trigger and want to make sure I don't royally screw up the database! Any and all help is greatly appreciated!

For those unfamiliar with Maximo, the table is: dbo.workorder and the fields are: location,ownergroup,supervisor


UPDATE1:
Here is some additional information that may be of importance.
Locations:
First off, workorder.location will contain values such as ABC-1234, meaning it is at site ABC, building 1234 (though these are NOT separate values, it's combined). In this SQL query, all buildings at a location are serviced by the same ownergroup/supervisor, so all queries use something similar to workorder.location='ABC%'.

Here is what I would like the logic to look like for the final query:

If the supervisor field is missing, first look to see if it has a parent, and if so, does the parent have a supervisor? If not, assign based on the table above.

If the ownergroup field is missing, first look to see if it has a parent, and if so, does the parent have an ownergroup? If not, assign based on the table above.

This is why I am thinking a case statement maybe the best option. Also, I currently have a list of variables such as "@ASupervisor, @B1Supervisor, @B2Supervisor,...etc" so that I can change them in the future if need be. To save a lot of redundant code, is it possible to do something like:
(in this example, location is ABC-1234, ownergroup SHOULD be @ABCGroup, supervisor should be @ABCSupervisor, where @ABCGroup and @ABCSupervisor are set earlier in the code)

If the supervisor field is missing, first look to see if it has a parent, and if so, does the parent have a supervisor (then copy it's supervisor)? If not, assign supervisor X.
Where X = '@' + '(the first three characters of the location)' + 'Supervisor' (in this example, X=@ABCSupervisor)

Is this possible??


UPDATE 2:

I have spoken with the person who asked for this database change and we have changed some thinking here. First, parent locations and child locations should always be the same (if they're not, that's a WHOLE OTHER issue). All sites (first 3 letters of location) should have the same ownergroup and supervisor, so essentially we can just look to see if a workorder entry has a NULL value in either field, then assign it based on the location. I believe the following code will work (but would like someone to review it before I implement it on the system)

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER dbo.AutoAssign
   ON  dbo.workorder
   AFTER INSERT,UPDATE
AS 
BEGIN
    SET NOCOUNT ON;

    DECLARE @ABCSupervisor varchar(30)
    DECLARE @DEFSupervisor varchar(30)

    DECLARE @ABCOwnerGroup varchar(20)
    DECLARE @DEFOwnerGroup varchar(20)


    /*EDIT VARIABLES IF FUTURE CHANGES*/
    --SET Supervisor values HERE;
    SET @ABCSupervisor='JOHNDOE' 
    SET @XYZSupervisor='JANEDOE' 

    --SET OwnerGroup values HERE:
    SET @ABCOwnerGroup='ALPHATEAM' 
    SET @XYZOwnerGroup='OMEGATEAM' 

    --UPDATES
    UPDATE dbo.workorder
        SET ownergroup='@'+SUBSTR(location,1,3)+'OwnerGroup'
    WHERE status<>'COMP'
        AND historyflag=0
        AND istask=0    
        AND ownergroup IS NULL
        AND location IS NOT NULL

    UPDATE dbo.workorder
        SET supervisor='@'+SUBSTR(location,1,3)+'Supervisor'
    WHERE status<>'COMP'
        AND historyflag=0
        AND istask=0
        AND supervisor IS NULL
        AND location IS NOT NULL
END
GO

The only issues I see here are that I do not join of some sort on the "inserted" table so that it only affects those entries (and not the entire table every time). If I could get some help on that, it would be greatly appreciated!!

+1  A: 

if you have done some update-statements you are on the right way. put the virtual table INSERTED in your update-statement and join it with a unique-key, e.g. SerialNo: that would be something like that:

create trigger Trig_WorkOrder for insert, update as
BEGIN
update wo
set location = pwo.location, ...
from dbo.workorder as wo, inserted as i, dbo.parentworkorder as pwo
where wo.serialNo = i.SerialNo -- join with the actual table-entry
and wo.pwo_id = pwo.id -- join with the parentworkorder
and i.ownergroup is null -- do it if new value is empty
and (pwo.ownergroup is not null or pwo.ownergroup <> '') -- do it if parentvalue is not empty
and (pwo.Supervisor is not null or pwo.Supervisor <> '') -- do it if parentvalue is not empty

update wo
set location = pwo.location, ...
from dbo.workorder as wo, inserted as i, dbo.standardworkorder as pwo
where wo.serialNo = i.SerialNo 
and wo.location = pwo.location
and wo.ownergroup is null
and (pwo.ownergroup is null or pwo.ownergroup = '')
and (pwo.Supervisor is null or pwo.Supervisor = '')
END

I would sugest to store the default values in a separate table, for convenient joining if the parentvalues are empty.

Put two update-statements inside the trigger an code the where-clause to make sure that only one statement executes...

peace and good luck

Ice
Just a few questions...Is it possible to do a case statement to make it easier to read/manage?Example: (Forgive my C-like style. I've not used SQL's CASE yet)UPDATE WOSWITCH(LOCATION) CASE LIKE 'A%': SET wo.ownergroup='AGroup' SET wo.supervisor='SuperR' CASE LIKE 'B1%': SET wo.ownergroup='BGroup' SET wo.supervisor='SuperH' etc . . .WHERE status<>'COMP' AND historyflag=0 AND istask=0 AND ownergroups IS NULL AND supervisor IS NULL(I'm not sure where I would do the joins though)
D.R.
Urgh, it didn't display properly with formatting. I can repost it possibly?
D.R.
If you like this style, thats also ok. Use "case when x=y then 1 when x=z then 2 ... else 3 end"
Ice
I updated my original post to reflect some new changes. If you wouldn't mind looking over it, that'd be great! I appreciate your help so far. I'm slowly working to a good answer, but unfortunately am still looking at 100's of lines of code to cover all the scenarios (i.e. one or both missing, one or both missing from parent, everything missing, etc)
D.R.