开发者

Trigger with postgresql

First off here is my code:

CREATE FUNCTION unknown_Model ()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
--checks if the produc开发者_如何学Ct 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜