该过程中发生了一些语法错误

时间:2014-10-20 12:43:37

标签: sql oracle plsql

我无法弄清楚错误是什么。这就是日志所说的:

遇到符号" DECLARE"当期待以下之一时:begin function pragma procedure子类型当前游标删除存在

以下是代码:

create or replace procedure sp1_transfer
(custid IN int,
 fromaccount int,
 toaccount IN int,
 amount IN number,
 result out varchar2)
 as
 countto int;
 tocustid int;
 fromcustid int;
 frombal number(12,2);
 countfrom int;
DECLARE exit handler for sqlexception
ROLLBACK;
BEGIN
SET result = 'DB Failure';
select count(*) INTO @countfrom from accounts where accno = fromaccount;
IF countfrom < 1 THEN
SET result = "Account Not found";
END IF;
select count(*) INTO countto from accounts where accno = toaccount;
IF countto < 1 THEN
SET result = "Account Not found";
END IF;
select customers.custid into tocustid from customers, accounts where customers.custid = accounts.custid and accounts.accno=toaccount;
select customers.custid into fromcustid from customers, accounts where customers.custid= accounts.custid and accounts.accno=fromaccount;
IF tocustid = tocustid THEN 
select balance INTO frombal from accounts where accno = fromaccount;
if frombal > amount THEN 
START TRANSACTION;
update accounts set balance = balance-amount where accno = fromaccount;
update accounts set balance = balance+amount where accno = toaccount;
insert into transactions (tdate, accno, description, amount) values (sysdate(), fromaccount, 'Transfer', -amount);
insert into transactions (tdate, accno, description, amount) values (sysdate(), toaccount, 'Deposit', amount);
SET result = 'SUCCESS';
COMMIT;
ELSE
SET result = 'NSF';
END IF;
ELSE
SET result = "Cross Transfer";
END IF;

2 个答案:

答案 0 :(得分:0)

这意味着您在开始此过程之前尝试声明某些内容。

所以你的陈述如下

DECLARE exit handler for sqlexception

应该在BEGIN之后,因为程序以BEGIN开头,其形式如下:

create or replace procedure
....
BEGIN
.....

答案 1 :(得分:0)

您的存储过程是PL / SQL和(我假设通过@符号判断)T-SQL的奇怪混合。首先,你应该在开始编码之前学习PL / SQL(例如来自the best learning resource there ever was)。

您正确编写的存储过程可能如下所示:

create or replace procedure sp1_transfer
    (custid IN int,
    fromaccount int,
    toaccount IN int,
    amount IN number,
    result out varchar2)
as
    countto int;
    tocustid int;
    fromcustid int;
    frombal number(12,2);
    countfrom int;
BEGIN
    result := 'DB Failure';

    select count(*) INTO countfrom from accounts where accno = fromaccount;

    IF countfrom < 1 THEN
        result := 'Account Not found';
    END IF;

    select count(*) INTO countto from accounts where accno = toaccount;

    IF countto < 1 THEN
        result := 'Account Not found';
    END IF;

    select customers.custid into tocustid from customers, accounts where customers.custid = accounts.custid and accounts.accno=toaccount;
    select customers.custid into fromcustid from customers, accounts where customers.custid= accounts.custid and accounts.accno=fromaccount;

    IF tocustid = tocustid THEN 
        select balance INTO frombal from accounts where accno = fromaccount;
        if frombal > amount THEN 
            update accounts set balance = balance-amount where accno = fromaccount;
            update accounts set balance = balance+amount where accno = toaccount;
            insert into transactions (tdate, accno, description, amount) values (sysdate(), fromaccount, 'Transfer', -amount);
            insert into transactions (tdate, accno, description, amount) values (sysdate(), toaccount, 'Deposit', amount);
            result := 'SUCCESS';
            COMMIT;
        ELSE
            result := 'NSF';
        END IF;
    ELSE
        result := 'Cross Transfer';
    END IF;
EXCEPTION
    WHEN OTHERS THEN
        ROLLABCK;
END;
/

......即便如此,如果我是你的开发主管,我会立刻解雇你做提交和select count(*) ... - s以及不重新提升/不记录异常。但这已经完全不同了。