I have a function that checks whether a table exists on PostgreSQL or not, using the following code:
CREATE OR REPLACE FUNCTION public.sp_table_exists(p_in_table_name character varying)
RETURNS boolean AS
$$
DECLARE QUERY_COUNT INTEGER DEFAULT 1;
QUERY_STRING VARCHAR(300);
BEGIN
QUERY_STRING := CONCAT('SELECT RELNAME FROM PG_CLASS WHERE RELNAME = ''',p_in_table_name,'''');
EXECUTE QUERY_STRING;
GET DIAGNOSTICS QUERY_COUNT = ROW_COUNT;
IF QUERY_COUNT > 0 THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
END;
$$ LANGUAGE plpgsql;
I'm trying to use the output of the above function to assign to a boolean value, but PostgreSQL doesn't allow me to do so.
DECLARE DEBUG_ENABLED boolean DEFAULT FALSE;
DEBUG_ENABLED := PERFORM sp_table_exists('temp_table');
OR
DEBUG_ENABLED := SELECT * FROM sp_table_exists('temp_table');
Can you please help me resolve this?