使用 postgresql 触发

发布于 2024-09-30 16:57:12 字数 602 浏览 2 评论 0原文

首先是我的代码:

CREATE FUNCTION unknown_Model ()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
--checks if the product table has information pertaining to the new PC insertion
BEGIN

    IF (SELECT COUNT(Product.model) 
          FROM Product 
          WHERE Product.model = NEW.model) = 0 THEN 
       INSERT INTO Product VALUES ('X', NEW.model, 'PC');
    END IF;

    RETURN NEW;
END
$$;

CREATE TRIGGER unknownModel
BEFORE INSERT OR UPDATE ON PC
FOR EACH ROW EXECUTE PROCEDURE unknown_Model();

我不断收到错误“控制到达触发过程结束而没有返回”。我看过互联网上的其他例子,它们与我的非常相似。知道为什么它看不到我的退货声明吗?

谢谢

First off here is my code:

CREATE FUNCTION unknown_Model ()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $
--checks if the product table has information pertaining to the new PC insertion
BEGIN

    IF (SELECT COUNT(Product.model) 
          FROM Product 
          WHERE Product.model = NEW.model) = 0 THEN 
       INSERT INTO Product VALUES ('X', NEW.model, 'PC');
    END IF;

    RETURN NEW;
END
$;

CREATE TRIGGER unknownModel
BEFORE INSERT OR UPDATE ON PC
FOR EACH ROW EXECUTE PROCEDURE unknown_Model();

I keep getting the error "control reached end of trigger procedure without RETURN". I've looked at other examples on the internet and they are very similar to mine. Any idea why it is not seeing my return statement?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦罢 2024-10-07 16:57:12

尝试

CREATE FUNCTION unknown_Model () 
RETURNS TRIGGER 
AS $ 
--checks if the product table has information pertaining to the new PC insertion 
BEGIN 

    IF (SELECT COUNT(Product.model)  
          FROM Product  
          WHERE Product.model = NEW.model) = 0 THEN  
       INSERT INTO Product VALUES ('X', NEW.model, 'PC'); 
    END IF; 

    RETURN NEW; 
END 
$ LANGUAGE plpgsql ;

将语言声明移到过程主体之后。据我所知,这不应该很重要,但是......好吧,尝试一下,看看是否有帮助。

分享并享受。

Try

CREATE FUNCTION unknown_Model () 
RETURNS TRIGGER 
AS $ 
--checks if the product table has information pertaining to the new PC insertion 
BEGIN 

    IF (SELECT COUNT(Product.model)  
          FROM Product  
          WHERE Product.model = NEW.model) = 0 THEN  
       INSERT INTO Product VALUES ('X', NEW.model, 'PC'); 
    END IF; 

    RETURN NEW; 
END 
$ LANGUAGE plpgsql ;

i.e. move the language declaration after the procedure body. AFAIK this shouldn't matter, but...well, try it and see if it helps.

Share and enjoy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文