Hi,
I am trying to create a simple trigger which inserts a couple of fields
into a table, when data in inserted into another table.

I have two tables, 'addressbook' and 'phonebook'. When I insert data
into the addressbook (whose fields are name, address1, address2,
address3, phonenum), I want it to add 'name' and 'phonenum' field values
into 'phonebook' table.

The following is the trigger i wrote for the above..

---- Begin of Code ---

DROP TRIGGER phonebook on addressbook;

CREATE OR REPLACE FUNCTION add_to_phonebook() RETURNS TRIGGER AS $phonebook$
        DECLARE
                new_name varchar;
                new_phone varchar;
        
        BEGIN
                IF(TG_OP='INSERT') THEN
                        INSERT INTO phonebook(name,phonenum)                    
VALUES(NEW.name,NEW.phonenum);
                END IF;
                RETURN NEW;
        END;

$phonebook$ LANGUAGE plpgsql;

CREATE TRIGGER phonebook AFTER INSERT ON addressbook FOR EACH ROW
EXECUTE PROCEDURE add_to_phonebook();

----- End of code ---
I created the two tables as follows

'addressbook'
 id serial | name varchar(100) | address1 varchar(100) | address2
varchar(100) | address3 varchar(100) | phonenum varchar(15)

'phonebook'
 id serial | name varchar(100) | phonenum varchar(100)

Now, when I try to insert data into the addressbook and the trigger
runs, I get into an ever ending process accompanied by following in my
terminal where I run it:
""SQL statement "INSERT INTO phonebook(name,phonenum) VALUES( $1 , $2 )"
PL/pgSQL function "add_to_phonebook" line 7 at SQL statement""

When this happens, my processor runs at 100% and when I press Ctrl+C, it
continues for few seconds and I get my terminal gets filled with the
above stated message. Also that, the original 'insert into' operation
over the 'addressbook' table doesn't happen.

Where am I wrong in the trigger function ?

--
With Regards

Parthan (TechnoFreak)

.   A Proud GNU/Linux User and Ubuntero
.0.
..0 [Web] https://wiki.ubuntu.com/Parthan
000 [Blog]http://technofreakatchennai.wordpress.com


---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Reply via email to