altocompta/sql/trunk/05_account_parent.sql
2010-04-12 15:01:37 +00:00

38 lines
1 KiB
PL/PgSQL

-- Function: comptaproc.account_parent(account_type)
-- DROP FUNCTION comptaproc.account_parent(account_type);
CREATE OR REPLACE FUNCTION comptaproc.account_parent(p_account account_type)
RETURNS account_type AS
$BODY$
declare
sSubParent tmp_pcmn.pcm_val_parent%type;
sResult tmp_pcmn.pcm_val_parent%type;
nCount integer;
begin
if p_account is NULL then
return NULL;
end if;
sSubParent:=p_account;
while true loop
select count(*) into nCount
from tmp_pcmn
where
pcm_val = sSubParent;
if nCount != 0 then
sResult:= sSubParent;
exit;
end if;
sSubParent:= substr(sSubParent,1,length(sSubParent)-1);
if length(sSubParent) <= 0 then
raise exception 'Impossible de trouver le compte parent pour %',p_account;
end if;
raise notice 'sSubParent % % ',sSubParent,length(sSubParent);
end loop;
raise notice 'account_parent : Parent is %',sSubParent;
return sSubParent;
end;
$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100;
ALTER FUNCTION comptaproc.account_parent(account_type) OWNER TO trunk;