这是我的功能:
create or replace function get_claim_data(
inclaimid char,
ingrouplist char,
inpatientname char
)
returns table (claimid char, patientname char, groupid char)
language plpgsql as $$
begin
return query
select claimid, patientname, groupid
from claims
where claimid = inclaimid;
-- If rows do not match then match Groupid
return query
select claimid, patientname, groupid
from claims
where groupid in ingrouplist;
-- If rows do not match then match PatientName
return query
select claimid, patientname, groupid
from claims
where patientname like inpatientname;
-- If rows do not match then defaulted to this row
return query
select 'NOCLAIM' as claimid, 'NOPATIENT' as patientname, 'OURGROUP' as groupid
from claims
where patientname <> 'JAGRUT'
limit 1;
end $$;
答案 0 :(得分:0)
你将不得不使用游标,大致就像
CREATE FUNCTION get_claim_data(
p_inclaimid text,
p_ingrouplist text,
p_inpatientname text
) RETURNS TABLE (p_claimid text, p_patientname text, p_groupid text)
LANGUAGE plpgsql AS $$
DECLARE
row_found boolean;
r record;
BEGIN
row_found := FALSE;
FOR (p_claimid, p_patientname, p_groupid) IN
SELECT claimid, patientname, groupid
FROM claims
WHERE claimid = p_inclaimid
LOOP
RETURN NEXT;
row_found := TRUE;
END LOOP;
IF row_found THEN RETURN END IF;
row_found := FALSE;
FOR (p_claimid, p_patientname, p_groupid) IN
SELECT claimid, patientname, groupid
FROM claims
WHERE groupid IN p_ingrouplist
LOOP
RETURN NEXT;
row_found := TRUE;
END LOOP;
IF row_found THEN RETURN END IF;
...
$$;
答案 1 :(得分:0)
您可以在每次查询后检查行数:
create or replace function get_claim_data(
inclaimid char,
ingrouplist char,
inpatientname char
)
returns table (claimid char, patientname char, groupid char)
language plpgsql as $$
declare
l_rows bigint;
begin
return query
select claimid, patientname, groupid
from claims
where claimid = inclaimid;
get diagnostics l_rows = row_count;
if l_rows = 0 then
-- If rows do not match then match Groupid
return query
select claimid, patientname, groupid
from claims
where groupid in ingrouplist;
get diagnostics l_rows = row_count;
end if;
-- If rows do not match then match PatientName
if l_rows = 0 then
return query
select claimid, patientname, groupid
from claims
where patientname like inpatientname;
get diagnostics l_rows = row_count;
end if;
-- If rows do not match then defaulted to this row
if l_rows = 0 then
return query
select 'NOCLAIM' as claimid, 'NOPATIENT' as patientname, 'OURGROUP' as groupid
from claims
where patientname <> 'JAGRUT'
limit 1;
end if;
end $$;
请注意,char
数据类型的使用是强discouraged