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





---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
      choose an index scan if your joining column's datatypes do not
      match

Reply via email to