12

我的触发器有问题。在插入新行时,它将检查文章是否尚未售出。我可以在软件中做到这一点,但我认为在数据库中这样做会更好。

-- Create function
CREATE OR REPLACE FUNCTION checkSold() RETURNS TRIGGER AS $checkSold$
    BEGIN
        SELECT offer_id FROM offer WHERE offer_id = NEW.offer_id AND date_sale IS NULL;

    IF NOT FOUND THEN
        RAISE EXCEPTION 'The Offer is Sold!';
    END IF;
    RETURN NEW;
END;
$checkSold$ LANGUAGE plpgsql;


-- Create trigger
Drop TRIGGER checkSold ON tag_map;
CREATE TRIGGER checkSold BEFORE INSERT ON tag_map FOR EACH ROW EXECUTE PROCEDURE checkSold();

INSERT INTO tag_map (tag_id,offer_id) VALUES (824,80);

这是插入后的错误。

[WARNING  ] INSERT INTO tag_map (tag_id,offer_id) VALUES (824,80)
            ERROR:  query has no destination for result data
            HINT:  If you want to discard the results of a SELECT, use PERFORM instead.
            CONTEXT:  PL/pgSQL function "checksold" line 2 at SQL statement

触发器有什么问题?没有它,效果很好。

4

1 回答 1

22

代替

SELECT offer_id FROM offer WHERE offer_id = NEW.offer_id AND date_sale IS NULL;

PERFORM offer_id FROM offer WHERE offer_id = NEW.offer_id AND date_sale IS NULL;

按照建议。

手册中的更多信息(“38.5.2。执行没有结果的命令”)。

于 2010-04-30T11:16:56.077 回答