From: | Emi Lu <emilu(at)encs(dot)concordia(dot)ca> |
---|---|
To: | Markus Bertheau <mbertheau(dot)pg(at)googlemail(dot)com> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: in PlPgSQL function, how to use variable in a "select ... |
Date: | 2006-03-17 19:23:57 |
Message-ID: | 441B0CCD.9020704@encs.concordia.ca |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
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 ;
EXECUTE query_value;
RETURN cm_tableName;
END;
$$ language 'plpgsql' IMMUTABLE STRICT;
select test('001');
Error:
ERROR: syntax error at or near "$1" at character 20
QUERY: SELECT col2 FROM $1 WHERE col1 = $2
CONTEXT: PL/pgSQL function "test" line 8 at select into variables
LINE 1: SELECT col2 FROM $1 WHERE col1 = $2
Does it mean I have to use the cursor ?
Thanks,
Ying
>I think it is SELECT INTO cm_tableName col2 FROM ... WHERE ...
>
>2006/3/17, Emi Lu <emilu(at)encs(dot)concordia(dot)ca>:
>
>
>>Hello,
>>
>>In pl/pgsql (postgresql 8.01), how to use variables in select .. into ..
>>command
>>
>>CREATE OR REPLACE FUNCTION test(VARCHAR) RETURNS VARCHAR AS $$
>>DECLARE
>> var1 ALIAS FOR $1;
>> cm_tableName tableA.col1%TYPE;
>> T1 VARCHAR := 'sourceTable';
>> query_value VARCHAR ;
>>BEGIN
>>
>> SELECT col2 INTO cm_tableName FROM T1 WHERE col1 = var1 ;
>> EXECUTE query_value;
>>
>>
>> RETURN cm_tableName;
>> END;
>>$$ language 'plpgsql' IMMUTABLE STRICT;
>>
>>select test('abc');
>>
>>Failed.
>>
>>
>>Also, tried "SELECT col2 INTO cm_tableName FROM || T1 WHERE col1 = ||
>>var1 " and
>>"SELECT col2 INTO cm_tableName FROM || T1 || WHERE col1 = || var1"
>>
>>Failed as well.
>>
>>T1 and var1 both are variables, may I how to use variables in a "select
>>... into " query please?
>>
>>Thanks a lot,
>>Ying
>>
>>
>>
>>
>>
>>---------------------------(end of broadcast)---------------------------
>>TIP 6: explain analyze is your friend
>>
>>
>>
From | Date | Subject | |
---|---|---|---|
Next Message | A. Kretschmer | 2006-03-17 19:50:32 | Re: in PlPgSQL function, how to use variable in a "select ... |
Previous Message | Emi Lu | 2006-03-17 17:52:53 | in PlPgSQL function, how to use variable in a "select ... into .. where " query |