SQL 触发器 - Maximo Workorders - 主管/所有者初始化

发布于 2024-09-28 01:36:36 字数 3403 浏览 6 评论 0原文

我正在使用自动发出工单的 IBM Maximo 数据库。当发出工作订单(又名插入数据库工作订单表)时,我想根据一组标准自动分配主管、所有者和所有者组。仅当尚未分配主管、所有者和所有者组时才需要执行此操作。通常,“父工作订单”具有信息,但需要将其复制到“子”工作订单中(正如您将在下面的标准中看到的那样)。所有触发器的条件是:

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

以下是触发器的条件:

- 如果所有者组和主管有值,则跳过该记录。 (不执行任何操作)

- 如果所有者组和/或主管为空或为空,并且相应的 PARENT Work 订单字段不为空,从以下位置复制所有者组和/或主管 父级工单记录。

- 如果父工作订单所有者组和/或主管为空或为空,则 根据下面的表值分配所有者组和主管:(为了安全起见,我删除了名称,但所有列都是正确的,即 B3 应该有 SuperA 作为主管)

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

**注意: SITE 不是表列,它实际上是 workorder.location 字段的前 3 个字符。例如,位置可能是 ABC-1234,这意味着它位于站点 ABC,大楼 1234(遗憾的是,这些位置并未存储​​在单独的列中,它们仅一起出现在位置列中)。在此 SQL 查询中,某个位置的所有建筑物均由同一所有者组/主管提供服务,因此我们当前使用的所有其他查询都使用 workorder.location='ABC%'

我已经完成了大量选择、更新和存储过程,但这是我的第一个触发器,我想确保我不会彻底搞砸数据库!非常感谢任何和所有的帮助!

对于那些不熟悉 Maximo 的人,该表是: dbo.工单 这些字段是: 位置、所有者组、主管


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%'.

这是我希望最终查询的逻辑:

如果supervisor字段缺失,首先查看它是否有parent,如果有,parent是否有supervisor?如果没有,则根据上表进行分配。

如果缺少ownergroup字段,首先查看它是否有父级,如果有,父级是否有所有者组?如果没有,则根据上表进行分配。

这就是为什么我认为案例陈述可能是最好的选择。另外,我目前有一个变量列表,例如“@ASupervisor,@B1Supervisor,@B2Supervisor,...等”,以便我可以在将来需要时更改它们。为了节省大量冗余代码,是否可以执行以下操作:
(在这个例子中,location是ABC-1234,ownergroup应该是@ABCGroup,supervisor应该是@ABCSupervisor,其中@ABCGroup和@ABCSupervisor是在代码前面设置的)

如果supervisor字段丢失,首先查看一下如果它有父级,如果有,父级是否有主管(然后复制其主管)?如果没有,请指定主管 X。
其中 X = '@' + '(位置的前三个字符)' + 'Supervisor'(在本例中,X=@ABCSupervisor)

这可能吗?


UPDATE 2:

我已经与要求更改数据库的人交谈过,我们在这里改变了一些想法。首先,父位置和子位置应该始终相同(如果不是,那就是另一个问题了)。所有站点(位置的前 3 个字母)应具有相同的所有者组和主管,因此本质上我们只需查看工作订单条目在任一字段中是否具有 NULL 值,然后根据位置进行分配。我相信下面的代码可以工作(但希望有人在我在系统上实现它之前检查它)

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

我在这里看到的唯一问题是我没有在“插入”表上加入某种形式,以便它仅影响这些条目(而不是每次都影响整个表)。如果我能得到一些帮助,我将不胜感激!

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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甩你一脸翔 2024-10-05 01:36:36

如果您已经做了一些更新语句,那么您就走对了。
将虚拟表 INSERTED 放入您的更新语句中,并使用唯一键将其连接起来,例如 SerialNo: 会是这样的:

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

我建议将默认值存储在单独的表中,以便在父值为空时方便连接。

将两个更新语句放在触发器内,并在 where 子句中添加代码,以确保只有一个语句执行...

祝您好运

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

浅听莫相离 2024-10-05 01:36:36

如果您使用的是 Maximo 7.x,自动化脚本可以执行您想要的操作。

例如,我们使用自动化脚本来检查子工作订单的字段是否为空。如果是,Maximo 将获取父工单值并填充它。在我们的例子中,它是一个名为 WOPM1 的自定义字段。

WorkOrderSet = mbo.getMboSet("PARENT")

if WorkOrderSet.getMbo(0) is not None:
      SUPERVISOR = WorkOrderSet.getMbo(0).getString("SUPERVISOR")

或者,如果您有一个基本查找来根据位置的前三个填充 OwnerGroup 和 Supervisor,则可以执行 if then 类型逻辑,以便在存在匹配时填充子工作订单中的数据。 v_location 是我为父工作订单位置定义的变量:

if v_location == 'ABC':
    mbo.setValue("ownergroup","ABCOwnerGroup")
    mbo.setValue("supervisor","ABCSupervisor")

If you are using Maximo 7.x, automation scripts can do what you want.

For example, we use an automation script that checks to see if the child work order's field is null. If it is, Maximo will take the parent work order value and fill it. In our case, it a custom field called WOPM1.

WorkOrderSet = mbo.getMboSet("PARENT")

if WorkOrderSet.getMbo(0) is not None:
      SUPERVISOR = WorkOrderSet.getMbo(0).getString("SUPERVISOR")

Alternatively, if you have a basic lookup to fill in OwnerGroup and Supervisor based on the first three of the location, you can do a if then type logic to fill in data in the child work order when there is a match. v_location is the variable I have defined for a parent work order location:

if v_location == 'ABC':
    mbo.setValue("ownergroup","ABCOwnerGroup")
    mbo.setValue("supervisor","ABCSupervisor")
捶死心动 2024-10-05 01:36:36

对于您想要实现的目标,我不确定使用触发器是 Maximo 中的最佳解决方案 - 使用触发器会忽略所有 MBO 业务逻辑。也许您可以考虑自定义 MBO 类或使用 Maximo Escalation 来完成此任务。在应用修复包或升级 Maximo 时,使用触发器也可能会给您带来麻烦,因此如果您选择这样做,请务必在执行任何此类操作之前备份触发器。

For what you are trying to achieve, I'm not sure using a trigger is the best solution in Maximo - using triggers ignores all MBO business logic. Maybe you could consider customizing MBO classes or using Maximo Escalations to do this taks. Using triggers could also get you in trouble when applying Fix Packs or upgrading Maximo so if you choose to go this way, be sure to backup the triggers before any such action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文