Re: EXECUTE 'EXECUTE ...' or how to use dynamic prepared statements ? - Mailing list pgsql-sql

From Dirk Jagdmann
Subject Re: EXECUTE 'EXECUTE ...' or how to use dynamic prepared statements ?
Date
Msg-id 5d0f60990709040238i1bd78235md22663f8bd2f2700@mail.gmail.com
Whole thread Raw
In response to EXECUTE 'EXECUTE ...' or how to use dynamic prepared statements ?  ("Marc Mamin" <M.Mamin@intershop.de>)
Responses Re: EXECUTE 'EXECUTE ...' or how to use dynamic prepared statements ?
List pgsql-sql
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)whererang=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


pgsql-sql by date:

Previous
From: "Marc Mamin"
Date:
Subject: EXECUTE 'EXECUTE ...' or how to use dynamic prepared statements ?
Next
From: "Bart Degryse"
Date:
Subject: Use of delete...returning in function problem