I have wrote the following function. But at the end I'm not able when count is empty to detect it and put in the result variable returning 0; I don't know what is wrong, maybe count(*) should be put by an external SELECT, no sure.
Running for example, random input:
SELECT is_department_active('44','222') from dual
I got is_department_active (null) but expected 0.
CREATE OR REPLACE FUNCTION is_department_active (
department_id VARCHAR2,
doc_id VARCHAR2
) RETURN NUMBER IS
result NUMBER(1);
cnt number(3);
BEGIN
result:=0;
SELECT
dep_active, count(*)
INTO result,cnt
FROM
(
SELECT
grade,
dep_active, 3 "priority number"
FROM
vrsc.inst_department
WHERE
af_feature_id = department_id
AND grade = 'DOC'
AND grade_field = doc_id
UNION
SELECT grade,
dep_active,
1 "priority number"
FROM
vrsc.inst_department
WHERE
af_feature_id = department_id
AND grade = 'CUSTOMER'
AND grade_field IN (
SELECT
doc_cust_id
FROM
customer_docs
WHERE
doc_id = doc_id)
UNION
SELECT
grade,
dep_active,
0 "priority number"
FROM
vrsc.inst_department
WHERE
af_feature_id = department_id
AND grade = 'DEFAULT'
AND GRADE_FIELD = 'DEFAULT'
UNION
SELECT
grade, dep_active,
2 "priority number"
FROM
vrsc.groups_docs left
JOIN vrsc.inst_department ON grade = 'GROUP'
AND grade_field = gs_group_id
AND grade_sec_field = gs_doc_code
WHERE
gs_doc_id = doc_id
AND af_feature_id = feature_id
ORDER BY
"priority number" DESC
)
WHERE
ROWNUM = 1 group by dep_active;
if cnt=0 then
result:=0;
end if;
RETURN result;
END;
I would like to take the first row wit priority 3, but if this query is null, empty then I can't return 0 as DEFAULT.
