- 论坛徽章:
- 0
|
下面是unix 下db2建立trigger的实例,哪位大侠能给一个as400下的完整例子?多谢了。
DROP TRIGGER WIPPARTSHIS_TIGGER@
CREATE TRIGGER WIPPARTSHIS_TIGGER
after insert on wippartshis
referencing new as n
for each row mode db2sql
--when (
--(-select count(*) from wippartsstock where store_acct=n.storeid and part_nbr=n.partid)>;0
--and
--(select count(*) from wipeqpkit where stepid=n.stepid and eqpid=n.eqpid and parttype=n.parttype)>;0
-- )
begin atomic
declare stmt varchar(100) default '';
declare vcount_stock INTEGER default 0;
declare vcount_eqpkit INTEGER default 0;
declare m_cur_qty_good decimal(5) default 0;
declare m_cur_qty_recng decimal(5) default 0;
declare m_cur_qty_taskng decimal(5) default 0;
declare m_use_qty_good decimal(5) default 0;
declare m_use_qty_recng decimal(5) default 0;
declare m_use_qty_taskng decimal(5) default 0;
declare m_new_qty decimal(5) default 0;
declare m_new_qtyng decimal(5) default 0;
declare m_new_qtyngh decimal(5) default 0;
declare m_flag INTEGER default 0;
declare m_curqty decimal(5) default 0;
if (select count(*) from wippartsstock where store_acct=n.storeid and part_nbr=n.partid)<=0 then
SIGNAL SQLSTATE '70018'('WipPartsStock no Record!!'); --物料庫存主檔沒有符合記錄
elseif (select count(*) from wipeqpkit where stepid=n.stepid and eqpid=n.eqpid and parttype=n.parttype)<=0 then
SIGNAL SQLSTATE '70019'('WipEqpKit no Record!!'); --機台備料主檔沒有符合記錄
end if ;
--set stmt=n.transid;
if n.qty is null then
set m_new_qty = 0;
else
set m_new_qty = decimal(n.qty);
end if;
if n.qtyng is null then
set m_new_qtyng = 0;
else
set m_new_qtyng = decimal(n.qtyng);
end if ;
if n.qtyngh is null then
set m_new_qtyngh = 0;
else
set m_new_qtyngh = decimal(n.qtyngh);
end if ;
------------------------
--取 wippartstock 的數量
------------------------
set m_cur_qty_good = (SELECT decimal(cur_qty_good) FROM wippartsstock WHERE store_acct = n.storeid AND part_nbr = n.partid);
set m_cur_qty_recng = (SELECT decimal(cur_qty_recng) FROM wippartsstock WHERE store_acct = n.storeid AND part_nbr = n.partid);
set m_cur_qty_taskng = (SELECT decimal(cur_qty_taskng) FROM wippartsstock WHERE store_acct = n.storeid AND part_nbr = n.partid);
set m_use_qty_good = (SELECT decimal(use_qty_good) FROM wippartsstock WHERE store_acct = n.storeid AND part_nbr = n.partid);
set m_use_qty_recng = (SELECT decimal(use_qty_recng) FROM wippartsstock WHERE store_acct = n.storeid AND part_nbr = n.partid);
set m_use_qty_taskng = (SELECT decimal(use_qty_taskng ) FROM wippartsstock WHERE store_acct = n.storeid AND part_nbr = n.partid);
------------------------
--取 wipeqpkit 的數量
------------------------
set m_curqty = (SELECT decimal(curqty) FROM wipeqpkit WHERE stepid = n.stepid AND eqpid = n.eqpid AND parttype = n.parttype);
set m_flag = 0;
if n.asm_flag = '+' then
IF m_cur_qty_good - m_new_qty < 0 THEN
SIGNAL SQLSTATE '70011'('cur_qty_good is not enough!!'); --目前良品數量庫存不足
set m_flag = 1;
END IF;
IF m_cur_qty_recng - m_new_qtyng < 0 THEN
SIGNAL SQLSTATE '70012'('cur_qty_recng is not enough!!'); --目前自責不良品數量庫存不足
set m_flag = 1;
END IF;
IF m_cur_qty_taskng-m_new_qtyngh < 0 THEN
SIGNAL SQLSTATE '70013'('cur_qty_taskng is not enough!!'); --目前他責不良品數量庫存不足
set m_flag = 1;
END IF;
IF m_curqty - m_new_qty < 0 then
SIGNAL SQLSTATE '70014'('wipeqpkit is not enough!!'); --機台備料數量不足
set m_flag = 1;
END IF;
if m_flag = 0 then
UPDATE wippartsstock set cur_qty_good = char(decimal(cur_qty_good) - m_new_qty)
,cur_qty_recng = char(decimal(cur_qty_recng) - m_new_qtyng)
,cur_qty_taskng = char(decimal(cur_qty_taskng) - m_new_qtyngh)
,use_qty_good = char(decimal(use_qty_good) + m_new_qty)
,use_qty_recng = char(decimal(use_qty_recng) + m_new_qtyng)
,use_qty_taskng = char(decimal(use_qty_taskng) + m_new_qtyngh)
where store_acct = n.storeid and part_nbr = n.partid;
if m_new_qty >; 0 then
UPDATE wipeqpkit set curqty = char(decimal(curqty) - m_new_qty)
WHERE stepid = n.stepid AND eqpid = n.eqpid AND parttype = n.parttype;
end if ;
else
set m_flag = 0;
IF m_use_qty_good - m_new_qty < 0 THEN
SIGNAL SQLSTATE '70015'('use_qty_good is not enough!!'); --良品使用數量不足
set m_flag = 1;
END IF;
IF m_use_qty_recng-m_new_qtyng < 0 THEN
SIGNAL SQLSTATE '70016'('use_qty_recng is not enough!!'); --自責不良品使用數量不足
set m_flag = 1;
END IF;
IF m_use_qty_taskng-m_new_qtyngh < 0 THEN
SIGNAL SQLSTATE '70017'('use_qty_taskng is not enough!!'); --他責不良品使用數量不足
set m_flag = 1;
END IF;
if m_flag=0 then
UPDATE wippartsstock set cur_qty_good = char(decimal(cur_qty_good) + m_new_qty)
,cur_qty_recng = char(decimal(cur_qty_recng) + m_new_qtyng)
,cur_qty_taskng = char(decimal(cur_qty_taskng) + m_new_qtyngh)
,use_qty_good = char(decimal(use_qty_good) - m_new_qty)
,use_qty_recng = char(decimal(use_qty_recng) - m_new_qtyng)
,use_qty_taskng = char(decimal(use_qty_taskng) - m_new_qtyngh)
where store_acct = n.storeid and part_nbr = n.partid;
end if;
end if ;
end if ;
insert into logerror (transtime)
values (current timestamp);
end @ |
|