X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 9995F476141 for ; Thu, 8 May 2003 12:39:28 -0400 (EDT) Received: from ccis01.baylor.edu (ccis01.baylor.edu [129.62.1.18]) by postgresql.org (Postfix) with ESMTP id A16A947592C for ; Thu, 8 May 2003 12:39:26 -0400 (EDT) Received: from baylor.edu ([147.80.228.49]) by ccis01.baylor.edu (8.9.3p2/8.9.1) with ESMTP id LAA1519647 for ; Thu, 8 May 2003 11:39:32 -0500 (CDT) Date: Thu, 8 May 2003 11:41:04 -0500 Subject: Re: insert rowtype Content-Type: text/plain; charset=US-ASCII; format=flowed Mime-Version: 1.0 (Apple Message framework v552) From: Bryan Klimt To: pgsql-sql@postgresql.org Content-Transfer-Encoding: 7bit In-Reply-To: <22B5FF60-815D-11D7-B60D-000A95774E48@baylor.edu> Message-Id: X-Mailer: Apple Mail (2.552) X-Spam-Status: No, hits=-12.1 required=5.0 tests=BAYES_10,IN_REP_TO,USER_AGENT_APPLEMAIL autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200305/172 X-Sequence-Number: 13337 So, to answer my own question... I wanted to duplicate a row, but change a few data items in it. Here is an example table to demonstrate: # select oid,* from test; oid | id | tmstamp | description -------+----+---------------------+------------- 20396 | 1 | 0000-00-00-00-00-00 | hello 20397 | 2 | 0000-00-00-00-00-00 | goodbye 20398 | 2 | 0000-00-00-00-00-01 | goodbye I wanted to create a new row like this: oid | id | tmstamp | description -------+----+---------------------+------------- 20396 | 1 | 0000-00-00-00-00-00 | hello 20397 | 2 | 0000-00-00-00-00-00 | goodbye 20398 | 2 | 0000-00-00-00-00-01 | goodbye 20415 | 2 | 1111-00-00-00-00-02 | goodbye But with my real table there are like 50 columns, and i didn't want to retype all their names. So, I created this function: create or replace function updatetest(integer,varchar(32)) returns integer as ' declare -- the oid of the new row newoid integer; begin -- duplicate the row insert into test select * from test t where t.tmstamp=( select max(tt.tmstamp) from test tt where tt.id=t.id ) and t.id=$1; -- get the oid of the new duplicate get diagnostics newoid = result_oid; -- update the columns you want to change update test set tmstamp=$2 where oid=newoid; -- return the new oid return newoid; end; ' language plpgsql; # select updatetest(2,'1111-00-00-00-00-02'); updatetest ------------ 20415 I know it looks kinda obvious but it took me forever to figure it out. Specifically, "get diagnostics newoid = result_oid;" is not very obvious to plpgsql newbies. -Bryan