I am new to PostgreSQL.
I have a function
CREATE OR REPLACE FUNCTION func1 ( a_person_id integer) RETURNS TABLE (granted_group_id varchar(10)) AS $body$
WITH v_granted_group_list AS (
SELECT DISTINCT a.group_id
FROM a_table a
)
SELECT v.group_id FROM v_granted_group_list v;
$body$
LANGUAGE sql;
I want to use the output of this in other function func2.
I tried to do that as in the following code and got an error.
What can we use in place of the v_access_groups array?
CREATE OR REPLACE FUNCTION func2 ( a_person_id IN integer,mhrc_emp_no IN varchar(50))
RETURNS TABLE (granted_group_id varchar(10)) AS $body$
DECLARE
v_access_groups varchar[];
BEGIN
v_access_groups := func1(a_person_id);
---- this gives error
---ERROR: malformed array literal: "LCCHG"
----DETAIL: Array value must start with "{" or dimension information.
--------CONTEXT: PL/pgSQL function func2(integer,character varying) line 14 at SQL statement
-- what can we you in place of v_access_groups array
RETURN v_access_groups;
END;
$body$
LANGUAGE PLPGSQL;
I want to use the result of func2 in yet another function in a select query
CREATE OR REPLACE FUNCTION func3 ( a_blurb_id integer, a_person_id integer,mhrc_emp_no varchar(20)) RETURNS boolean AS $body$
DECLARE
v_acc_count numeric;
v_accessAllowed boolean:=FALSE;
BEGIN
SELECT COUNT(*) INTO v_acc_count
FROM table1 agfa
where agfa.group_id IN (
SELECT TO_CHAR(column_value) AS group_id
FROM TABLE(func2(a_person_id,mhrc_emp_no))
);
RETURN TRUE;
END;
$body$
LANGUAGE PLPGSQL;
How can I achieve this in PostgreSQL?