content : i am writing a function to return a value based on 3 conditions, but i need to write a loop inside it so it can check the conditions for each id passed. can i write t
It is hard for us to reverse engineer business logic from broken code. It is easier to understand clearly stated rules. So this is my interpretation of what you want:
For a given id:
level 1
if it has distinct codes matching only ('TF','TB','TY','TL','TS')
level 2
if it has other codes not matching ('TF','TB','TY','TL','TS')
level 3
if it has null codeslevel 4
(e.g. when NO_DATA_FOUND)create or replace FUNCTION fwt_get_holds(
i_id id.table_im%TYPE
) RETURN VARCHAR2 IS
l_level1 number := 0;
l_level2 number := 0;
l_level3 number := 0;
l_level4 number := 0;
BEGIN
begin
SELECT count(case when sprhold_hldd_code in ('TF','TB','TY','TL','TS') then 1 end ) as lvl_1
, count(case when sprhold_hldd_code LIKE 'T%' and sprhold_hldd_code not in ('TF','TB','TY','TL','TS') then 1 end ) as lvl_2
, count(case when sprhold_hldd_code is null then 1 end ) as lvl_3
into l_level1
, l_level2
, l_level3
FROM sprhold,
stvhldd
WHERE stvhldd_code = sprhold_hldd_code
AND sprhold_to_date >= trunc(sysdate)
AND sprhold_pidm = i_id;
exception
when others then
l_level4 := 1;
end;
if l_level4 != 0 then
return 'level 4'; -- no data found ?
elsif l_level3 != 0 then
return 'level 3'; -- found some nulls
elsif l_level2 != 0 then
return 'level 2'; -- found some non-matching codes
else
return 'level 1'; -- found only matching codes
end if;
END fwt_get_holds;
Quite possibly this is not what you want. If so, I suggest you edit your question to explain your business rules, as I did at the top of this answer.