Passing a table name to a function for dynamic queries.... - Mailing list pgsql-general

From Greg Patnude
Subject Passing a table name to a function for dynamic queries....
Date
Msg-id d9ul70$2ca1$1@news.hub.org
Whole thread Raw
Responses Re: Passing a table name to a function for dynamic queries....
List pgsql-general
I know I've seen the answer to this here before but cannot seem to find
it....  Can anyone give me the quick & dirty answer to passing a table name
to a function like this:

CREATE OR REPLACE FUNCTION tbl_toarray(name) RETURNS text
$BODY$

DECLARE ROW RECORD;

BEGIN

    SELECT ARRAY(SELECT ''id:'' || id || '', type_desc:'' || type_desc FROM
$1) INTO ROW;
    RETURN NEXT ROW;

END;

$BODY$
LANGUAGE PLPGSQL;

I'd like to be able to pass the function a table name and have the contents
of the table returned as an array of text....

THIS WORKS:

dmconfig=# SELECT ARRAY(SELECT '[id:' || id || ', type_desc:' || type_desc
|| ']' FROM lu_user_type WHERE active_flag);

                ?column?

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 {"[id:1, type_desc:System]","[id:2, type_desc:Company]","[id:3,
type_desc:Administrator]","[id:4, type_desc:Employee]","[id:5,
type_desc:User (standalone)]","[id:6, type_desc:Guest / Demo]"}
(1 row)


TIA....





Regards,







Greg Patnude - Manager, Dynamic Applications Group

Data Mosaics, Inc.

2406 South Dishman-Mica Road / Suite # 6

Spokane Valley, WA  99206-6429



VOICE: (866) 904-DMSF

FAX: (509) 928-4236





pgsql-general by date:

Previous
From: Sven Willenberger
Date:
Subject: Re: PostgreSQL's vacuumdb fails to allocate memory for
Next
From: "Ketan S Shah"
Date:
Subject: how use pg_dump/pg_restore