can i use a string in where clause such as "id in (str)"

Started by Yavuz Kavusalmost 20 years ago2 messagesgeneral
Jump to latest
#1Yavuz Kavus
yavuzkavus@gmail.com

hi every body.
i have a pl/pgsql function (i want to form it to run correctly) :

CREATE OR REPLACE FUNCTION "public"."ftest" () RETURNS boolean AS
$body$
declare
f boolean;
str varchar(100);
begin
str := '1,5,10';
if exists( select 1 from tb_categories where cat_id in ( str ) )
then
return true;
else
return false;
end if;
end;
$body$
LANGUAGE 'plpgsql' VOLATILE RETURNS NULL ON NULL INPUT SECURITY INVOKER;

there is a "10" value for cat_id(int4) column which should make function
return true.

but, str is seen as a string value as whole, so it doesnt return true.

i achived this by using execute command on string.
but i dont want to use execute, because there is no prepared plan for it(i
am not sure, but it may slow down procedure).

how can i achive this, are there any solutions?

thanks for any help.

#2A. Kretschmer
andreas.kretschmer@schollglas.com
In reply to: Yavuz Kavus (#1)
Re: can i use a string in where clause such as "id in (str)"

am 12.06.2006, um 0:05:07 +0300 mailte Yavuz Kavus folgendes:

hi every body.
i have a pl/pgsql function (i want to form it to run correctly) :

CREATE OR REPLACE FUNCTION "public"."ftest" () RETURNS boolean AS
$body$
declare
f boolean;
str varchar(100);
begin
str := '1,5,10';
if exists( select 1 from tb_categories where cat_id in ( str ) )
...

how can i achive this, are there any solutions?

Please read our documentation about executing dynamic querys:
http://www.postgresql.org/docs/8.1/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

Hint: you should build a string with your complete SQL command and
EXECUTE this string.

HTH, Andreas
--
Andreas Kretschmer (Kontakt: siehe Header)
Heynitz: 035242/47215, D1: 0160/7141639
GnuPG-ID 0x3FFF606C http://wwwkeys.de.pgp.net
=== Schollglas Unternehmensgruppe ===