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 3DC07D1B169 for ; Mon, 21 Jun 2004 15:51:16 -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 70149-06 for ; Mon, 21 Jun 2004 18:51:08 +0000 (GMT) Received: from mail231.csoft.net (resin.csoft.net [63.111.22.86]) by svr1.postgresql.org (Postfix) with SMTP id D0828D1B737 for ; Mon, 21 Jun 2004 15:51:05 -0300 (ADT) Received: (qmail 16619 invoked from network); 21 Jun 2004 18:51:04 -0000 Received: from unknown (HELO chezphil.org) (63.111.27.87) by mail231.csoft.net with SMTP; 21 Jun 2004 18:51:04 -0000 Content-Disposition: inline Content-Transfer-Encoding: binary Content-Type: text/plain MIME-Version: 1.0 X-Mailer: MIME::Lite 3.01 (F2.72; A1.17; B2.21; Q2.21) From: "Phil Endecott" To: Tom Lane , "Riccardo G. Facchini" , pgsql-sql@postgresql.org Subject: Re: plpgsql - Insert from a record variable? Date: Mon, 21 Jun 2004 14:51:04 -0400 X-Mailer: Phil's WebMail Message-Id: <2179249@chezphil.org> In-Reply-To: <4357626@chezphil.org> References: <20040619060330.83941.qmail@web13901.mail.yahoo.com> <17840.1087670348@sss.pgh.pa.us> <4357626@chezphil.org> 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/199 X-Sequence-Number: 18144 I wrote: > perhaps rather than inspecting the record variable to see what fields > it contains, I can look at the table to see what columns it contains This is my effort. It doesn't work, because I can only EXECUTE SQL statements, not PLPGSQL statements. I need to EXECUTE an assignment statement to accumulate the string of column values. I have a feeling that I can EXECUTE a CREATE FUNCTION statement, and then call the function, but this seems over-the-top. I just want to insert a record into a table! Any better ideas? -- Simply insert record r into table t. -- Doesn't work, because EXECUTE takes an SQL command, not -- a plpgsql statement. create function insert_record ( record, text ) as ' -- probably ought to pass schema as well as table name, since -- information_schema.columns query doesn't use search_path. declare r as alias for $1; t as alias for $2; cr information_schema.columns%rowtype; first boolean; column_names text; column_values text; begin first := true; for cr in select * from information_schema.columns where table_name=t loop if not first then column_names := column_names || '', ''; column_values := column_values || '', ''; first := false; end if; column_names := column_names || quote_ident(cr.column_name); !! execute ''column_values := !! column_values || quote_literal(r.'' || cr.column_name || '')''; end loop; execute ''insert into '' || t || ''('' || column_names || '') values ('' || column_values || '')''; end; ' language plpgsql; --Phil.