1

我有两张桌子:

`deal` (`value`, `when`, `AccountTo`, `AccountFrom`, `Name`)

`account` (`AccountNumber`, `Value`)

我设置了一个程序:

CREATE PROCEDURE transfer
(IN Value_IN float,
IN When_IN int(4),
IN AccountTo_IN varchar(26),
IN AccountFrom_IN varchar(26),
IN Name_IN varchar(30))
NOT DETERMINISTIC
SQL SECURITY DEFINER
COMMENT ''

INSERT INTO deal
(value, when, AccountTo, AccountFrom, Name)
VALUES (Value_IN, When_IN, AccountTo_IN, AccountFrom_IN, Name_IN);

UPDATE account SET Value=(Value+Value_IN) WHERE AccountNumber=AccountTo_IN;
UPDATE account SET Value=(Value-Value_IN) WHERE AccountNumber=AccountFrom_IN;

数据被放入“交易”但“帐户”表未更新

编辑:

感谢您的回答。它是这样工作的:

CREATE DEFINER=`root`@`localhost` PROCEDURE `transfer`( 
Value_IN FLOAT,
When_IN DATE,
AccountTo_IN VARCHAR(26),
AccountFrom_IN VARCHAR(26),
Name_IN VARCHAR(30))
BEGIN

    INSERT INTO deal 
    (value, when, AccountTo, AccountFrom, Name)
    VALUES (value_IN, When_IN, AccountTo_IN, AccountFrom_IN, Name_IN);

    UPDATE account SET Value = Value+Value_IN) WHERE AccountNumber = AccountTo_IN;
    UPDATE account SET Value = Value-Value_IN) WHERE AccountNumber = AccountFrom_IN;

END;
4

1 回答 1

1

我认为您在 INSERT 之前和最后一次 UPDATE 之后可能需要一个 BEGIN 和一个 END。许多数据库都是这种情况,SQL/Server、MySQL、Sybase、Oracle 都需要 BEGIN/END。

于 2015-10-12T21:48:59.167 回答