Tags: 1result, alias, create, database, declarethetable, example, fun_totalrecords, function, int8, mysql, oracle, postgresql, returns, sql, table, varchar, variable

table name as variable within Function

On Database » PostgreSQL

1,442 words with 1 Comments; publish: Thu, 22 May 2008 19:03:00 GMT; (25078.13, « »)

Here is an example of what i would like to do...

---

CREATE FUNCTION fun_totalrecords(varchar) RETURNS int8 AS'

DECLARE

theTable ALIAS FOR $1;

result int := 0;

BEGIN

result = COUNT(*) FROM theTable; -- this is where i need help

RETURN result;

END; 'LANGUAGE 'plpgsql';

---

Thanks for the help...

Clark

All Comments

Leave a comment...

  • 1 Comments
    • On Thu, Jul 07, 2005 at 12:22:38PM -0600, Clark Allan wrote:

      > Here is an example of what i would like to do...

      > ---

      > CREATE FUNCTION fun_totalrecords(varchar) RETURNS int8 AS'

      > DECLARE

      > theTable ALIAS FOR $1;

      > result int := 0;

      > BEGIN

      > result = COUNT(*) FROM theTable; -- this is where i need help

      > RETURN result;

      > END; 'LANGUAGE 'plpgsql';

      See "Executing Dynamic Commands" in the PL/pgSQL documentation:

      http://www.postgresql.org/docs/8.0/...S-EXECUTING-DYN

      Michael Fuhr

      http://www.fuhr.org/~mfuhr/

      --(end of broadcast)--

      TIP 3: if posting/reading through Usenet, please send an appropriate

      subscribe-nomail command to majordomo.postgresql.questionfor.info.postgresql.org so that your

      message can get through to the mailing list cleanly

      #1; Thu, 22 May 2008 19:04:00 GMT