我可以制作这个PL SQL触发器吗?

时间:2017-02-27 09:16:42

标签: sql oracle plsql triggers

我有三张桌子BORROW,PAID和COMPLETED

BORROW表有3个字段 UID(主键) AMOUNT

PAID表有 UID(Forign Key) 每期

COMPLETED表有 UID(Forign Key) AMOUNT

If i borrow $100 from some one 

BORROW TABLE will be

| UID | AMOUNT |
| 1   | 100    |
----------------

PAID TABLE 


| UID | INSTALLMENT |
|-----|------------|
| 1   | 30         |
--------------------
| 1   | 40         |
--------------------
| 1   | 10         |
--------------------
| 1   | 20         |
--------------------

我需要自动将BORROW TABLE的详细信息传输到A COMPLETED TABLE中 SUM(INSTALLMENT)= AMOUNT,其中UID = 1;

有人请帮助我触发上述操作

1 个答案:

答案 0 :(得分:-1)

您可以创建一个存储过程/查询,以便在PAID上的每个INSERT / UPDATE之后调用。它将包含类似的内容:

-- @UID is your UID from PAID when inserted/updated
DECLARE @SUM int
DECLARE @BILL int

SELECT @SUM = SUM(INSTALLMENT)
FROM PAID
WHERE UID = @UID
GROUP BY UID

SELECT @BILL = AMOUNT
FROM BORROW
WHERE UID = @UID

IF @BILL = @SUM
BEGIN
    INSERT INTO COMPLETED (UID, AMOUNT)
    VALUES (@UID, @BILL)
END

您需要根据需要更改此查询,但基本想法就在这里。

编辑:不知道您无法在Oracle的触发器事件上访问您自己的表。看起来在这种特殊情况下不可能触发触发器。