X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 0D62756622 for ; Wed, 16 Mar 2005 14:01:04 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 80882-04 for ; Wed, 16 Mar 2005 14:01:01 +0000 (GMT) Received: from iracema.dnscenter.com.br (iracema.dnscenter.com.br [67.15.80.70]) by svr1.postgresql.org (Postfix) with ESMTP id 0A8B5563FA for ; Wed, 16 Mar 2005 14:00:59 +0000 (GMT) Received: from cpanel by iracema.dnscenter.com.br with local (Exim 4.44) id 1DBZ4z-0007js-TW; Wed, 16 Mar 2005 11:00:58 -0300 Received: from 200.158.187.163 ([200.158.187.163]) by www.presserv.org (Horde) with HTTP for ; Wed, 16 Mar 2005 11:00:57 -0300 Message-ID: <20050316110057.hj7k01plv1ck4g08@www.presserv.org> Date: Wed, 16 Mar 2005 11:00:57 -0300 From: lucas@presserv.org To: George Weaver Cc: pgsql-sql@postgresql.org Subject: Re: Generic Function References: <20050314151555.6kp8nfclzy5cogg0@www.presserv.org> <004b01c52954$dacb5ec0$6400a8c0@Dell4500> In-Reply-To: <004b01c52954$dacb5ec0$6400a8c0@Dell4500> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format="flowed" Content-Disposition: inline Content-Transfer-Encoding: 7bit User-Agent: Internet Messaging Program (IMP) H3 (4.0) X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - iracema.dnscenter.com.br X-AntiAbuse: Original Domain - postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [32001 502] / [47 12] X-AntiAbuse: Sender Address Domain - presserv.org X-Source: X-Source-Args: X-Source-Dir: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.626 tagged_above=0 required=5 tests=AWL, NO_DNS_FOR_FROM, NO_REAL_NAME, RCVD_IN_NJABL_PROXY X-Spam-Level: * X-Archive-Number: 200503/226 X-Sequence-Number: 20705 Oh sorry. I was not clearly. I've wanted to create a function that suport to select a "parameter variable" table. Like: return 'select * from $1'. The Postgresql does not suport this sql function becouse the $1 variable is considerate as a table... the Postgresql return an error like: The table "$1" doesn't exist. Then there is no way to construct this function as SQL function, but I could make it in PL/TCL function and execute normaly look: set search_path to public; create table tb1(vl text); insert into tb1 values ('aaa'); create table tb2(vl text); insert into tb2 values ('bbb'); CREATE or REPLACE FUNCTION select_table(text, text, text) returns text as $$ set schema_name $1; set table_name $2; set field_name $3; set select_query "select $field_name AS select_table from "; set point "."; spi_exec $select_query$schema_name$point$table_name; return $select_table; $$ language 'pltcl'; Then: SELECT select_table('public','tb1','vl'); SELECT select_table('public','tb2','vl'); The spi_exec execute the query as a variable ($select_query$...) and the return of this query (select $field_name AS select_table) will be the variable "select_table" for the pl/tcl function. Then I return this variable (return $select_table). Is it right?! Is there a better way to make it? The Pl/Pgsql can built this function? And the SQL Standard? Thanks... Quoting George Weaver : > > ----- Original Message ----- > From: > To: > Sent: Monday, March 14, 2005 12:15 PM > Subject: [SQL] Generic Function > > >> Hi, >> Can I built a generic function like: >> CREATE FUNCTION f (text) RETURNS TEXT as >> $$ >> return 'select * from $1'; >> $$ ... > > If you show us what you've tried and the results you received we may be able > to help more. ...