X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 526079DCB80 for ; Fri, 17 Mar 2006 16:33:21 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 88124-08 for ; Fri, 17 Mar 2006 16:33:21 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from perseverance.encs.concordia.ca (perseverance-96.encs.concordia.ca [132.205.96.94]) by postgresql.org (Postfix) with ESMTP id F1CA99DC871 for ; Fri, 17 Mar 2006 16:33:18 -0400 (AST) Received: from [132.205.47.205] (IDENT:emilu@curacao.encs.concordia.ca [132.205.47.205]) by perseverance.encs.concordia.ca (8.12.11/8.12.11) with ESMTP id k2HKXKw4027278; Fri, 17 Mar 2006 15:33:20 -0500 Message-ID: <441B1D0E.3000109@encs.concordia.ca> Date: Fri, 17 Mar 2006 15:33:18 -0500 From: Emi Lu Reply-To: emilu@encs.concordia.ca User-Agent: Mozilla Thunderbird 1.0.7 (X11/20050923) X-Accept-Language: en-us, en MIME-Version: 1.0 To: "A. Kretschmer" CC: pgsql-sql@postgresql.org Subject: Re: in PlPgSQL function, how to use variable in a "select ... References: <441AF775.7060006@encs.concordia.ca> <684362e10603170956j20174d07q@mail.gmail.com> <441B0CCD.9020704@encs.concordia.ca> <20060317195031.GA8508@webserv.wug-glas.de> In-Reply-To: <20060317195031.GA8508@webserv.wug-glas.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.43 on perseverance.encs.concordia.ca at 2006/03/17 15:33:20 EST X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.135 required=5 tests=[AWL=0.135, UPPERCASE_25_50=0] X-Spam-Score: 0.135 X-Spam-Level: X-Archive-Number: 200603/254 X-Sequence-Number: 24600 >>Does not work either, the whole function is: >> >>create table t1(col1 varchar(3), col2 varchar(100)); >>insert into t1 values('001', 'Result 1'); >>insert into t1 values('002', 'Result 2'); >>insert into t1 values('003', 'Result 3'); >> >>CREATE OR REPLACE FUNCTION test(VARCHAR) RETURNS VARCHAR AS $$ >>DECLARE >> col1_value ALIAS FOR $1; >>cm_tableName st1_legend.code_map_tablename%TYPE; >>lengendTableName VARCHAR := 't1'; >> query_value VARCHAR ; >>BEGIN >> >> SELECT INTO cm_tableName col2 FROM lengendTableName WHERE col1 = >>col1_value ; >> >> > >This can't work, read the docu: >http://www.postgresql.org/docs/8.1/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN > >You should build a string with your SQL and EXECUTE this string. > > Thank you Andreas. Unfortunately it did not work. maybe I made something wrong? drop table t1; create table t1(col1 varchar(3), col2 varchar(100)); insert into t1 values('001', 'Result 1'); insert into t1 values('002', 'Result 2'); insert into t1 values('003', 'Result 3'); CREATE OR REPLACE FUNCTION test(VARCHAR) RETURNS VARCHAR AS $$ DECLARE col1_value ALIAS FOR $1; cm_tableName st1_legend.code_map_tablename%TYPE; lengendTableName VARCHAR := 't1'; query_value VARCHAR ; BEGIN query_value := 'SELECT col2 FROM lengendTableName WHERE col1 = \'' || col1_value || '\''; EXECUTE query_value INTO cm_tableName; RETURN cm_tableName; END; $$ language 'plpgsql' IMMUTABLE STRICT; select test('001'); Error: ERROR: syntax error at or near "$2" at character 20 QUERY: SELECT $1 INTO $2 CONTEXT: PL/pgSQL function "test" line 9 at execute statement LINE 1: SELECT $1 INTO $2 I am using postgresql 8.0.1, and I am afraid that 8.0 does not support "excecute ... into ...." http://www.postgresql.org/docs/8.0/static/plpgsql-statements.html I will try to use cursor. Thank you very much for all your help anyway. Ying