0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help. Turns out assumptions burned me again. I assume that plpgsql was already set up. Once I set it up my code worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.