引发异常时出现PL / SQL运行时错误

时间:2016-03-10 19:52:16

标签: plsql

我已经编写了几个PL / SQL程序,它们运行正常,除非我尝试测试它们以便故意提出异常。可能是声明或引发异常的语法?我用异常语法找不到我的错误

当它在没有触发异常的情况下运行时没有任何问题,但是当它遇到我的异常时,而不是输出我想要的内容时,它会说“PLS-00103:遇到符号”文件结束时“期待以下之一:等等......“

CREATE OR REPLACE PROCEDURE SPLIT_STOCK (p_stock_id IN NUMBER,p_split_factor IN NUMBER)
AS
l_multiplied_shares  NUMBER;
l_new_shares NUMBER;
l_current_shares NUMBER;
l_number_of_shareholders NUMBER;
l_company_id NUMBER;
l_shares_authorized NUMBER;
SPLIT_FACTOR_LESS_THAN_1_EXC EXCEPTION;
SHARES_EXCEEDED_EXC EXCEPTION;
BEGIN
IF p_split_factor <=1 THEN  RAISE SPLIT_FACTOR_LESS_THAN_1_EXC ;  END IF; --Share split factor has to be > 1.

SELECT sum(CShSh.shares) * p_split_factor into l_multiplied_shares from Current_Shareholder_Shares CShSh where CShSh.stock_id = p_stock_id;  --Getting the new total shares after the split
select authorized into l_shares_authorized from shares_authorized sa where sa.stock_id = p_stock_id; --getting number of authorized shares into local variable
select sum(shares) INTO l_current_shares from Current_Shareholder_Shares CShSh where CShSh.stock_id = p_stock_id; --getting the current total shares for this stock
if l_multiplied_shares > l_current_shares THEN RAISE SHARES_EXCEEDED_EXC; END IF; --Did we exceed allowed shares? Raise exception if so.
l_new_shares := l_multiplied_shares - l_current_shares; --This is the new amount of shares to be split evenly among current Shareholders.
Select count(shareholder_id) into l_number_of_shareholders from Current_Shareholder_Shares CShSh where CShSh.stock_id = p_stock_id; --This is the number of shareholders, we need to divide by this number to issue everyone their share of the split shares.
select com.company_id into l_company_id from company com where com.stock_id = p_stock_id; -- getting the company id of the company that is doing the split.

INSERT INTO trade -- (trade_id, stock_id, transaction_time,shares,stock_ex_id,price_total,buyer_id,seller_id,buy_broker_id,sell_broker_id)

               SELECT  trade_id_seq.nextval, p_stock_id, SYSDATE, (l_new_shares/l_number_of_shareholders), NULL, NULL, CShSh.shareholder_id , l_company_id, 
               NULL, NULL
               FROM Current_Shareholder_Shares CShSh;
EXCEPTION
WHEN SPLIT_FACTOR_LESS_THAN_1_EXC THEN dbms_output.put_line ('Please enter a split factor greater than 1. Decimals are also allowed.');
WHEN SHARES_EXCEEDED_EXC THEN dbms_output.put_line ('Number of Authorized shares has been exceeded. split factor needs to be smaller.');
END
;
/



EXEC SPLIT_STOCK(6,100001); -- this should throw my specific defined exception
EXEC SPLIT_STOCK(6,0.5); -- this should throw my specific defined exception

1 个答案:

答案 0 :(得分:0)

SQL * Plus会将行EXEC text转换为BEGIN text END;,如果text未以text结尾,则会在EXEC SPLIT_STOCK(6,100001); -- this should throw my specific defined exception 之后添加分号。

所以,在你的情况下,行

BEGIN SPLIT_STOCK(6,100001); -- this should throw my specific defined exception; END;

变为

END;

Stack Overflow中的语法突出显示应该为您提供有关此处发生的事情的提示:SQL> EXEC SPLIT_STOCK(6,100001); -- this should throw my specific defined exception BEGIN SPLIT_STOCK(6,100001); -- this should throw my specific defined exception; END; * ERROR at line 1: ORA-06550: line 2, column 0: PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: 已被注释掉。看起来很奇怪,问题实际上就是评论!

您可以在收到的错误消息的第二行看到发送到数据库的SQL * Plus行:

EXEC

因为您正在使用/,所以SQL * Plus不会等待单行EXEC告诉它输入已经结束。它可以立即将该行发送到数据库,但当然数据库将返回错误,因为它无法找到块的结尾。

因此,最简单的解决方法是删除/* ... */行的评论。或者,使用EXEC SPLIT_STOCK(6,100001) /* this should throw my specific defined exception */ 评论,但请注意在评论前使用分号:

Properties.Resources.Favori