2014-04-16 170 views
-1

表DESC:創建觸發器的查詢ORACLE SQL

Customer (Custid, Custname, Addr, phno,panno) 
Loan (Loanid, Amount, Interest, Custid) 
Account (Accd, Accbal, Custid) 

查詢:

Create a Trigger which checks whether the Accbal is atleast 25% of amount to be inserted in Loan. 

下了扳機代碼是:

create or replace trigger claim before 
insert on loan for each row 
declare 
    ex exception; 
    bal number; 
begin 
    select accbal into bal from account where :new.custid=account.custid; 
    if(bal < 0.25*:new.amount)then 
    raise ex; 
    end if; 
EXCEPTION 
when ex then 
    raise_application_error(-20001,'Balance is not sufficient'); 
end; 
/

我的觸發創建。但是,在貸款中插入值(意圖獲取錯誤)時,錯誤不會被提升。任何更正將在我的代碼中進行?

假設這些都是值:

LOANID  AMOUNT INTEREST CUSTID 
------- ---------- ---------- ------ 
    1001  100000  2000 C01 
    1002  200000 4081.41 C02 
    1003  50000  1000 C03 

    ACCD  ACCBAL CUSTID 
---------- ------ ------ 
    2001  5000 C01 
    2002  105000 C02 
    2003  24000 C03 

如果我要插入此:

insert into loan values(1004,100000,2000,'C02'); 

它必須產生一個錯誤作爲新量的四分之一「100000」 = 25000 [ 0.25 *:new.amount]小於accbal,即對應的角點「C02」爲105000。

但是這個觸發代碼沒有發生。相反,它直接插入表中的值。

+0

我想提出一個錯誤。當bal <0.25 *金額時,此金額爲新插入的值。 – Goks

+0

您對此測試的數據是什麼 - 請在表格中添加一些示例數據以及您要發佈的確切聲明。而且「custid」只有一個「帳戶」記錄嗎?似乎觸發器正在假設。 –

+0

對不起。我現在會改變。 – Goks

回答

0

對於帳戶餘額爲105000的客戶,您將插入一個新金額爲100000的新負載。規則是檢查餘額是否至少是貸款金額的25%。你的貸款價值的25%是25000,餘額遠高於此。

爲了您的觸發,提高異常的貸款金額將需要平衡的四倍以上:

insert into loan values (1004, 100000, 2000, 'C02'); 

1 rows inserted. 

insert into loan values (1005, (100005 * 4), 2000, 'C02'); 

1 rows inserted. 

insert into xloan values (1006, (105000 * 4) + 1, 2000, 'C02') 

Error report - 
SQL Error: ORA-20001: Balance is not sufficient 
ORA-06512: at "MYSCHEMA.CLAIM", line 11 
ORA-04088: error during execution of trigger 'MYSCHEMA.CLAIM' 

所以,你只是有邏輯,當你創建數據以錯誤的方式和插入語句。

可以簡化觸發了一下,順便說一句,像你一樣,你發現這個問題並不需要提高,趕上ex例外,你可以儘快提高自己的錯誤:

create or replace trigger claim 
before insert on loan 
for each row 
declare 
    bal number; 
begin 
    select accbal into bal from account where :new.custid=account.custid; 
    if bal < (0.25*:new.amount) then 
    raise_application_error(-20001,'Balance is not sufficient'); 
    end if; 
end; 
/
+0

非常感謝:)...與數據混淆:P ... – Goks

+0

U明確表達了這一點,謝謝.. :) – Goks