X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id DF037D1B169 for ; Sat, 19 Jun 2004 03:03:40 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 95376-04 for ; Sat, 19 Jun 2004 06:03:32 +0000 (GMT) Received: from web13901.mail.yahoo.com (web13901.mail.yahoo.com [216.136.175.27]) by svr1.postgresql.org (Postfix) with SMTP id 18641D1B16A for ; Sat, 19 Jun 2004 03:03:30 -0300 (ADT) Message-ID: <20040619060330.83941.qmail@web13901.mail.yahoo.com> Received: from [217.76.159.49] by web13901.mail.yahoo.com via HTTP; Fri, 18 Jun 2004 23:03:30 PDT Date: Fri, 18 Jun 2004 23:03:30 -0700 (PDT) From: "Riccardo G. Facchini" Subject: Re: plpgsql - Insert from a record variable? To: Phil Endecott , pgsql-sql@postgresql.org In-Reply-To: <4899937@chezphil.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200406/161 X-Sequence-Number: 18106 --- Phil Endecott <__> wrote: > Dear Postgresql experts, > > I'm writing a simple plpgsql function that selects a row from a > table, modifies it slightly, and inserts the modified version. This > sounds simple, but I can't find how to do the insert in a simple > generic fashion: although SELECT has a form that puts the result in a > record variable, INSERT doesn't seem to have anything similar. > > What I'd like to be able to write is something like this: > > DECLARE > R RECORD; > BEGIN > SELECT * INTO R FROM TABLE WHERE id=n; > R.something := x; > INSERT INTO TABLE R; > END I'm not sure if it will work, but have you tried either two of these forms? -- declare r record; begin select * into r from table where id = n; r.something := x; insert into table select r; end; -- declare r record; begin select * into r from table where id = n; r.something := x; insert into table select r.*; end; > > But as far as I can see the only syntax that is allowed for INSERT is > the normal INSERT INTO TABLE (x,y,z) VALUES (a,b,c). To achieve what > I want to do I'd need to iterate over the fields of the record (how?) > and build up the string of the INSERT query. > > It seems odd that SELECT can use a record variable but INSERT can't, > so I wonder if I have missed something. Any suggestions? > > Thanks in advance for any help anyone can offer. > > Regards, > > --Phil. > > ---------------------------(end of > broadcast)--------------------------- > TIP 7: don't forget to increase your free space map settings regards, ===== Riccardo G. Facchini