X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id C051E9DC854 for ; Thu, 16 Mar 2006 11:41:07 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 33064-08 for ; Thu, 16 Mar 2006 11:41:11 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from ciao.gmane.org (main.gmane.org [80.91.229.2]) by postgresql.org (Postfix) with ESMTP id E54089DC800 for ; Thu, 16 Mar 2006 11:41:04 -0400 (AST) Received: from root by ciao.gmane.org with local (Exim 4.43) id 1FJual-00069c-DQ for pgsql-sql@postgresql.org; Thu, 16 Mar 2006 16:40:49 +0100 Received: from toronto-hse-ppp4121442.sympatico.ca ([70.50.244.232]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 16 Mar 2006 16:40:47 +0100 Received: from lacouf by toronto-hse-ppp4121442.sympatico.ca with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 16 Mar 2006 16:40:47 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: lacou Subject: help with function Date: Thu, 16 Mar 2006 10:06:16 -0500 Lines: 47 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: toronto-hse-ppp4121442.sympatico.ca User-Agent: Thunderbird 1.5 (Windows/20051201) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200603/231 X-Sequence-Number: 24577 Hello, I have 2 tables where each table has a column named "comments" and the tables are related as a one to many. I want to concatenate all the comments of the many side to the one side so I wrote the following plpgsql function to do so.
CREATE OR REPLACE FUNCTION fixcomments()
   RETURNS int4 AS
$BODY$
DECLARE
     mviews RECORD;
     i int4;
BEGIN

     FOR mviews IN SELECT * FROM saleorder WHERE comments is not null 
and comments <> '' LOOP

         -- Now "mviews" has one record from saleorder

         EXECUTE 'UPDATE sale SET comments = ' || 
quote_ident(sale.comments) || quote_ident(mviews.comments)
		|| ' WHERE sale.id = ' || quote_ident(mviews.sale_id);
     i := i + 1;
     END LOOP;

     RETURN i;
END;
$BODY$
   LANGUAGE 'plpgsql' VOLATILE;

I have the following error when I run this code:
ERROR:  missing FROM-clause entry for table "sale"
CONTEXT:  SQL statement "SELECT  'UPDATE sale SET comments = ' || 
quote_ident(sale.comments) || quote_ident( $1 ) || ' WHERE sale.id = ' 
|| quote_ident( $2 )"
PL/pgSQL function "fixcomments" line 11 at execute statement
Doesn anybody know what I am doing wrong here ? Lacou.