dbTalk Databases Forums  

PL/pgsql - getting row from record

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss PL/pgsql - getting row from record in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Matt
 
Posts: n/a

Default PL/pgsql - getting row from record - 11-16-2004 , 06:21 AM






Hi,

I'm sure this has been asked before, but my searches haven't come up
with an example.

I'm writing a generic trigger function called from different tables in
plpgsql that needs to get the value of the primary key column from OLD.

I can find out the pkey column name by querying the system tables, but
the only way I've found of getting the value of that column is by
creating a temporary table and using FOR ... EXECUTE to get the value
back out. My code for doing that is below, but it seems mighty ugly.

So, two questions:
1. Is there a better way of getting arbitrary columns from a record?
2. If not, will I run into problems with the name of the temporary table
if two processes call this function at the same time, or are temp names
unique to the particular session?

CREATE TEMPORARY TABLE get_colval_from_record_tmp
AS SELECT OLD.*;
FOR srow IN EXECUTE
''SELECT '' || col || '' AS scol
FROM get_colval_from_record_tmp''
LOOP
colval := srow.scol;
END LOOP;
DROP TABLE get_colval_from_record_tmp;

Thanks for the help,

Matt

BTW: I noticed on http://developer.postgresql.org/todo.php there's an
item "Allow PL/pgSQL to name columns by ordinal position, e.g. rec.(3)".
That'd do me fine, but I guess I'd better ask on the dev list as to when
this might happen



---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly


Reply With Quote
  #2  
Old   
Richard Huxton
 
Posts: n/a

Default Re: PL/pgsql - getting row from record - 11-16-2004 , 07:35 AM






Matt wrote:
Quote:
So, two questions:
1. Is there a better way of getting arbitrary columns from a record?
Not really.

Quote:
2. If not, will I run into problems with the name of the temporary table
if two processes call this function at the same time, or are temp names
unique to the particular session?
Temporary tables are session-specific.

Really, you'd be better off using one of the more interpreted languages
for this specific task though.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org



Reply With Quote
  #3  
Old   
Matt
 
Posts: n/a

Default Re: PL/pgsql - getting row from record - 11-16-2004 , 08:07 AM



Thanks for the quick response!

Quote:
Really, you'd be better off using one of the more interpreted languages
for this specific task though.
Was afraid someone would say that. Time to polish up those tarnished
perl skills

Matt


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.