Skip site navigation (1) Skip section navigation (2)

For In Execute

From: Jason Davis <jdavis(at)tassie(dot)net(dot)au>
To: pgsql-novice(at)postgresql(dot)org
Subject: For In Execute
Date: 2001-08-21 13:05:39
Message-ID: 200108211304.XAA23003@zeus.tassie.net.au (view raw or flat)
Thread:
Lists: pgsql-novice
Hi all

I'm having some dramas getting a basic dynamic function to go, similar to
the one from Phillip Allen's posting from the other day...I'm attempting to
get the function to return the count(*) for a table name supplied to it:

CREATE FUNCTION test_func(text) RETURNS text AS '
DECLARE
  rec           record;
  retval        int;
  qry           text;
BEGIN
  qry:= ''SELECT COUNT(*) AS numrows FROM '' || $1 || '';'';
  FOR rec IN EXECUTE qry LOOP
    retval:= rec.numrows;
  END LOOP;
  RETURN retval;
END;
' LANGUAGE 'plpgsql';

The double quotes in the assignment to the qry variable are two single
quotes, if this is unclear. I get the following:

test=# select test_func('test_table');
NOTICE:  plpgsql: ERROR during compile of test_func near line 7
ERROR:  parse error at or near "EXECUTE"

After searching all the techdocs and the excellent Practical Postgresql and
other resources, I am still none the wiser...can anyone lend me a few brain
cells? I'm running 7.0.3.

cheers
Jason Davis


Responses

pgsql-novice by date

Next:From: Tom LaneDate: 2001-08-21 14:38:06
Subject: Re: For In Execute
Previous:From: D. DucciniDate: 2001-08-21 02:52:10
Subject: Re: installing on sunos 5.7

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group