2010-11-08 7 views
0

Tout d'abord voici mon code:Trigger avec postgresql

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(); 

Je continue à obtenir l'erreur « contrôle a atteint la fin de la procédure de déclenchement sans retour ». J'ai regardé d'autres exemples sur internet et ils sont très similaires aux miens. Une idée de pourquoi il ne voit pas ma déclaration de retour?

Merci

Répondre

1

Essayez

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 ; 

à savoir déplacer la déclaration de langue après que le corps de la procédure. AFAIK cette ne devrait pas importe, mais ... eh bien, essayez et voyez si cela aide.

Partagez et appréciez.

+0

Merci pour l'aide. Il s'avère que les hypothèses m'ont encore brûlé. Je suppose que plpgsql était déjà installé. Une fois que j'ai mis en place mon code a fonctionné. – Pinsickle