关于Oracle 触发器向各位大神请教!

[复制链接]
查看11 | 回复9 | 2017-8-11 15:38:46 | 显示全部楼层 |阅读模式
关于oracle创建触发器:
1. 首先我要查询某台设备有没有工单(多表连接查询)
select mo.extra_mo_info9, count(wo.wo_key) count
from work_order wo, maintenance_object mo
where mo.mo_key = wo.mo_key
and wo.job_type = 'U'
and mo.extra_mo_info9' '
group by mo.extra_mo_info9
查询的结果如下:
extra_mo_info9count
2000-DF10
1
1000-BT01
2
然后我要根据上面的结果更新设备表(Maintenance_object)中的信息
比如将设备编号(mo_key)为 2000-DF10的设备状态字段(status)变为 “停机”

回复

使用道具 举报

千问 | 2017-8-11 15:38:46 | 显示全部楼层
在设备表中的结构是
mo_key, status
如果上面的表中查询出的工单数量大于0 然后,将Mo_key 对应的status变为停机就可以了。
回复

使用道具 举报

千问 | 2017-8-11 15:38:46 | 显示全部楼层
这种情况一般用视图来实现.
回复

使用道具 举报

千问 | 2017-8-11 15:38:46 | 显示全部楼层
我创建了视图,但是创建触发器报错。
回复

使用道具 举报

千问 | 2017-8-11 15:38:46 | 显示全部楼层
Would you please explain why we need trigger here? What's your requirement indeed?
回复

使用道具 举报

千问 | 2017-8-11 15:38:46 | 显示全部楼层
Plus, please refer to the following post before you asking question, it will help both yourself and the people who willing to serve an answer.
http://www.itpub.net/thread-1348543-1-1.html

回复

使用道具 举报

千问 | 2017-8-11 15:38:46 | 显示全部楼层
有两张表
一张表(work_order)记录的数据如下:
设备编码
工单号
工单类型
A0001
WO-0001
U
A0002
WO-0002
U
A0001
WO-0003
P
A0003
WO-0004
U
A0005
WO-0005
P
A0001
WO-0007
U
上述表以设备编码进行统计工单类型为“U"的工单号的数量
select 设备编码,count(工单号) 工单数量 from work_order
where 工单类型=‘U’
group by 设备编码
得到的结果为
设备编码 工单数量
A00001 2
A00021
A00031
A00050
以上结果,如果工单数量>0 则需要更新另外一张表(Maintenance_object)中的设备状态status字段内容
另外一张表更新前的数据及结构如下:
设备编码 设备状态
A0001 在用
A0002 在用
A0003 在用
A0004 在用
A0005 在用
我需要用触发器的功能,将上面表中的设备状态进行变更(如设备对应的工单大于0则设备状态变为"停机",否则为"在用"
设备编码 设备状态
A0001 停机
A0002 停机
A0003 停机
A0004 在用
A0005 在用
回复

使用道具 举报

千问 | 2017-8-11 15:38:46 | 显示全部楼层
YuBinTAMU 发表于 2018-7-12 22:00
Plus, please refer to the following post before you asking question, it will help both yourself and...

感谢您的提醒,我重新写了一下要求,不知道我说明白没有,谢谢啊!
回复

使用道具 举报

千问 | 2017-8-11 15:38:46 | 显示全部楼层
本帖最后由 Bert_xia 于 2018-7-12 22:39 编辑
有两张表
一张表(work_order)记录的数据如下:
设备编码
工单号工单类型
A0001
WO-0001
U
A0002
WO-0002
U
A0001
WO-0003
P
A0003
WO-0004
U
A0005
WO-0005
P
A0001
WO-0007
U
上述表以设备编码进行统计工单类型为“U"的工单号的数量
select 设备编码,count(工单号) 工单数量 from work_order
where 工单类型=‘U’
group by 设备编码
得到的结果为
设备编码 工单数量
A00001 2
A00021
A00031
A00050
以上结果,如果工单数量>0 则需要更新另外一张表(Maintenance_object)中的设备状态status字段内容
另外一张表更新前的数据及结构如下:
设备编码 设备状态
A0001 在用
A0002 在用
A0003 在用
A0004 在用
A0005 在用
我需要用触发器的功能,将上面表中的设备状态进行变更(如设备对应的工单大于0则设备状态变为"停机",否则为"在用"
设备编码 设备状态
A0001 停机
A0002 停机
A0003 停机
A0004 在用
A0005 在用
说明:第一张工单表的work_order中的信息会随时更新,增加和删除,可以用work_order 的insert事件作为触发条件。谢谢!

回复

使用道具 举报

千问 | 2017-8-11 15:38:46 | 显示全部楼层
Thanks for your re-post, it's much better now.
Below please find a solution using materialized view. It will fulfill your request with low DML frequency to table work_order.
(1) Set up the test env:
create table work_order(dev_no varchar2(5) , ticket_no varchar2(10), ticket_type varchar2(1));
insert into work_order values ('A0001','WO-0001','U');
insert into work_order values ('A0002','WO-0002','U');
insert into work_order values ('A0001','WO-0003','P');
insert into work_order values ('A0003','WO-0004','U');
insert into work_order values ('A0005','WO-0005','P');
insert into work_order values ('A0004','WO-0006','P');
insert into work_order values ('A0001','WO-0007','U');
commit;
alter table work_order add constraint work_order_pk primary key( dev_no, ticket_no );
create table Maintenance_object ( dev_no varchar2(5), dev_status varchar2(1) default 'W' );
-- 'W'working status
-- 'M'Maitaining status , stopped
复制代码
(2) Create the Materialized view:
create materialized view maintenance_object
on prebuilt table
refreshon demand
as
select dev_no
, decode( sum( decode(ticket_type, 'U',1,0)), 0, 'W','M') dev_status
from work_order
group by dev_no ;
复制代码
(3) Any time you want to refresh the data in maintenance_object, just issue the following command:
SQL>exec dbms_mview.refresh('maintenance_object', 'c');
PL/SQL procedure successfully completed.
SQL>select * from maintenance_object;
DEV_N D
----- -
A0001 M
A0002 M
A0003 M
A0004 W
A0005 W
SQL>
复制代码
Please be noticed that it's not a good solution if the table work_order has a high volume with frequency DML.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行