Thread: how to create this trigger?

how to create this trigger?

From
Ramiro Arenas Ramírez
Date:
I need to create a trigger that increment a value in a column 
of table1 where a row is inserted in table 2

I have tried whit this but it just does nothing.

CREATE FUNCTION increment_value () RETURNS opaque AS 
'DECLARE  code int4;  BEGIN  code := new.code;  UPDATE table1   SET value = value + 1  WHERE id = code;
RETURN NEW;
END;'  LANGUAGE 'plpgsql';

CREATE TRIGGER insert_on_table2 BEFORE INSERT ON table2 FOR EACH ROW EXECUTE PROCEDURE increment_value();


Can you help me? 





RE: how to create this trigger?

From
Michael Davis
Date:
Did you insert a row into table2?  What happens if there is no row in table 
1 where id = code (of the newly inserted row in table2).  Seems to me you 
many need to consider expanding increment_value() to check table1 to see if 
code exists.  If not insert a new value, else update.

-----Original Message-----
From:    Ramiro Arenas Ramirez [SMTP:ramiroa@coordinadora.com.co]
Sent:    Thursday, February 01, 2001 2:48 PM
To:    pgsql-sql@postgresql.org
Subject:    how to create this trigger?

I need to create a trigger that increment a value in a column
of table1 where a row is inserted in table 2

I have tried whit this but it just does nothing.

CREATE FUNCTION increment_value () RETURNS opaque AS
'DECLARE  code int4;  BEGIN  code := new.code;  UPDATE table1  SET value = value + 1  WHERE id = code;
RETURN NEW;
END;'  LANGUAGE 'plpgsql';

CREATE TRIGGER insert_on_table2 BEFORE INSERT ON table2FOR EACH ROW EXECUTE PROCEDURE increment_value();


Can you help me?





Re: how to create this trigger?

From
"Albert REINER"
Date:
Don't know much, but couldn't you let increment_value take an argument
and run it on new.code?

But maybe I am completely off.

Albert.


On Thu, Feb 01, 2001 at 04:48:28PM -0500, Ramiro Arenas Ramírez wrote:
> I need to create a trigger that increment a value in a column 
> of table1 where a row is inserted in table 2
> 
> I have tried whit this but it just does nothing.
> 
> CREATE FUNCTION increment_value () RETURNS opaque AS 
> 'DECLARE
>    code int4;
>    BEGIN
>    code := new.code;
>    UPDATE table1 
>    SET value = value + 1
>    WHERE id = code;
> RETURN NEW;
> END;'  LANGUAGE 'plpgsql';
> 
> CREATE TRIGGER insert_on_table2 BEFORE INSERT ON table2 
>     FOR EACH ROW EXECUTE PROCEDURE increment_value();
> 
> 
> Can you help me? 
> 
> 
> 
> 

-- 

--------------------------------------------------------------------------
Albert Reiner                                   <areiner@tph.tuwien.ac.at>
Deutsch       *       English       *       Esperanto       *       Latine
--------------------------------------------------------------------------