无法更新表格'出价'在存储函数/触发器中,因为它已被调用此存储函数/触发器的语句使用

时间:2018-04-12 22:13:03

标签: mysql triggers

我有桌子

CREATE TABLE Bids
 (buyer VARCHAR(20),
 vin VARCHAR(20),
 amount int,
 autobid int,
 upperlimit int,
 PRIMARY KEY(buyer, vin, amount),
 FOREIGN KEY (vin) REFERENCES Auctions(vin)

 );

 CREATE TABLE Notifications
 (buyer VARCHAR(20),
 outbidded VARCHAR(20),
 vin VARCHAR(20),
 amount int,
 PRIMARY KEY(vin, buyer, amount)
 );

触发器

 DELIMITER $$
CREATE TRIGGER before_bid_insert 
    BEFORE INSERT ON Bids
    FOR EACH ROW 
BEGIN

    SELECT B.amount, B.buyer INTO @amount, @buyer
    FROM Bids AS B WHERE B.vin = NEW.vin AND B.amount = (SELECT MAX(amount) FROM Bids B WHERE B.vin = NEW.vin AND B.buyer != NEW.buyer);

    IF NEW.amount > @amount  THEN
    INSERT INTO Notifications (buyer, outbidded, vin, amount)
    values (NEW.buyer, @buyer, NEW.vin, NEW.amount);


    END IF ;


END$$
DELIMITER ;

 DELIMITER $$
CREATE TRIGGER automatic_bidding
    AFTER INSERT ON Notifications
    FOR EACH ROW 
BEGIN

    SELECT B.amount, B.buyer, B.autobid, B.upperlimit INTO @amount, @buyer, @autobid, @upperlimit
    FROM Bids AS B WHERE B.vin = NEW.vin AND B.amount = (SELECT MAX(amount) FROM Bids B WHERE B.vin = NEW.vin AND B.buyer != NEW.buyer);

    IF NEW.amount > @amount AND @autobid != 0 AND NEW.amount + @autobid <= @upperlimit THEN

    SET @amount = NEW.amount + @autobid;

    INSERT INTO Bids (buyer, vin, amount, autobid, upperlimit)
    values (@buyer, NEW.vin, @amount, @autobid, @upperlimit);


    END IF ;


END$$
DELIMITER ;

但我一直收到这个错误。第一个触发器应该向通知表添加用户已经出价的通知。然后,自动出价触发器应该对此作出反应,并检查出价的用户是否具有自动设置,然后将设置新的出价。但是,似乎mysql不允许这样做,因为第一个触发器是使用bid表来进行操作?我怎么能将这两个动作分开,以便它们可以相互作用呢?

编辑:我尝试更改通知表以包含出价表中的信息,以便我可以通过通知而不是出价来操作第二个触发器,但它仍然没有帮助。同样的错误,不能正常工作

0 个答案:

没有答案