On 07/04/2013 08:53 AM, giozh wrote:
> i've write this function that search if inside a specified table there's a
> specified value:
>
> CREATE FUNCTION check_if_if_exist(id INTEGER, table_name character(50),
> table_column character(20) ) RETURNS BOOLEAN AS $$
>
> BEGIN
> RETURN EXECUTE 'SELECT EXISTS(SELECT * FROM table_name WHERE table_column =
> id)';
> END;
>
> $$ LANGUAGE plpgsql
>
> but when i try to call it i always receive an error and the function will
> not call. where is the problem?
Try:
CREATE OR REPLACE FUNCTION utility.check_if_if_exist(id integer,
table_name character, table_column character)
RETURNS boolean
LANGUAGE plpgsql
AS $function$
DECLARE
_exists boolean;
BEGIN
EXECUTE 'SELECT EXISTS(SELECT * FROM '|| table_name || ' WHERE '
|| table_column ||' =
$1)' INTO _exists USING id ;
RETURN _exists;
END;
More information here:
http://www.postgresql.org/docs/9.2/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
--
Adrian Klaver
adrian.klaver@gmail.com