Re: [GENERAL] PL/pgSQL help - Mailing list pgsql-general

From tolik@icomm.ru (Anatoly K. Lasareff)
Subject Re: [GENERAL] PL/pgSQL help
Date
Msg-id 876755mryl.fsf@tolikus.hq.aaanet.ru
Whole thread Raw
In response to PL/pgSQL help  (Mike Haberman <mikeh@ncsa.uiuc.edu>)
Responses Re: [GENERAL] PL/pgSQL help  (Dustin Sallings <dustin@spy.net>)
List pgsql-general
>>>>> "MH" == Mike Haberman <mikeh@ncsa.uiuc.edu> writes:

 MH> I'm a bit new to plpgsql, so this may be an easy question,
 MH> I've got a function (see below) that inserts into 3 different
 MH> tables.  Each table has a SERIAL type for it's primary key.

 MH> Question 1:
 MH> I want to group all 3 inserts as a transacation.

 MH> but when I put BEGIN WORK and COMMIT I get the error (at run
time):

Really this is compile time for your function: its text compiles when
it is first time called.


 MH> NOTICE:  plpgsql: ERROR during compile of easy_add near line 21
 MH> ERROR:  parse error at or near ""

 MH> this is the line with COMMIT on it;

 MH> What am i doing wrong?
 MH> Also, do I also need to specify a ROLLBACK if any of the inserts
fail?

Any transaction operators, such as 'commit', 'rollback', etc not
allowed in 'plpgsql' functions. Only function _call_ as a unit can be
into transaction block.



 MH> Question 2:
 MH> is there a way to get the value of the newly assigned primary key
 MH> after an insert?  (rather then following the insert with a select)

 MH> e.g. (this would be nice if it worked (networkID is the PKey))
 MH> INSERT into Network (parentID, networkName) values (pid, mname);
 MH> netid := new.networkID;

Yes. 'serial' type implements as 'int' type for field and sequence,
which mane is <tablename>_<fieldname>_seq. So you can do this:

INSERT into Network (parentID, networkName) values (pid, mname);
netid := Network_networkID_seq.last_value;


--
Anatoly K. Lasareff              Email:       tolik@icomm.ru
Senior programmer

pgsql-general by date:

Previous
From: taipan@shofiah.kusza.edu.my
Date:
Subject: reatedb: database creation failed on mydb
Next
From: The Hermit Hacker
Date:
Subject: Re: [GENERAL] Parallelizing PostgreSQL for Cluster