Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id C11059F9A7E for ; Tue, 4 Sep 2007 06:38:33 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 00203-06 for ; Tue, 4 Sep 2007 06:38:25 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from py-out-1112.google.com (py-out-1112.google.com [64.233.166.181]) by postgresql.org (Postfix) with ESMTP id AF62E9F9A3F for ; Tue, 4 Sep 2007 06:38:30 -0300 (ADT) Received: by py-out-1112.google.com with SMTP id u77so5936199pyb for ; Tue, 04 Sep 2007 02:38:29 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=Sc+i94MxLvLmubR1guwkvyS4JwhCUPsZwxd0/NkS3u3okP3KzINnwuRlmFsm+c04NVDgzaqaK7n9mEwuM2tlQ83cijKBVBjLlIDFB8ERBCfV+oB423vR9p64XCJADY4KDDy73ZHI4mDCxLDZJe5OZvU0fTzDt+TtsU7HabwmVQQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=ONIuqKthd/if6yWdlctNqhAcdPq73em98klicOgx6frgATSRnzMZZJb34u3ywu+tAmZD2/PSx7vgyLz5p72NuIlw9xqMddvu+P6fMM5chMJ5JwRhtx167D2rbMwpMR1W2s1gusFCpzO/bt0Lz+/K4ogkwc6h7ofrQt6ejsp2KRU= Received: by 10.65.54.9 with SMTP id g9mr4228947qbk.1188898709103; Tue, 04 Sep 2007 02:38:29 -0700 (PDT) Received: by 10.65.241.3 with HTTP; Tue, 4 Sep 2007 02:38:29 -0700 (PDT) Message-ID: <5d0f60990709040238i1bd78235md22663f8bd2f2700@mail.gmail.com> Date: Tue, 4 Sep 2007 11:38:29 +0200 From: "Dirk Jagdmann" To: "Marc Mamin" Subject: Re: EXECUTE 'EXECUTE ...' or how to use dynamic prepared statements ? Cc: pgsql-sql@postgresql.org In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200709/27 X-Sequence-Number: 29240 Hello Marc, at first I tried to solve your update of the tables. The example you gave should be done with an update statement like the following: update test_table set mygroup=(select t.mygroup from test_table as t where t.family = test_table.family and t.rang = test_table.rang+1) where rang=0; If you have to write a function which receives the tablename as an argument it would look like: CREATE OR REPLACE FUNCTION test_function(tablename text) RETURNS integer AS $BODY$ BEGIN EXECUTE 'update ' || tablename || ' set mygroup=(select t.mygroup from ' || tablename || ' as t where t.family = test_table.family and t.rang = test_table.rang+1) where rang=0;' RETURN 0; END; $BODY$ LANGUAGE 'plpgsql' VOLATILE; Generally you should avoid using explicit for/loop constructs in your stored procedures if the action can be solved by a single SQL statement, because the optimizer can make a better execution plan. -- ---> Dirk Jagdmann ----> http://cubic.org/~doj -----> http://llg.cubic.org