====== pears.DBCleanupOrphanPhone ======
Generated schema reference. Regenerate this page from the SQL unload; keep hand-maintained business notes in the narrative namespace.
===== Original SQL =====
create function "pears"."DBCleanupOrphanPhone"(
/* Application Maintained Function / Procedure - DO NOT EDIT*/
in "countordo" char(1) )
returns integer
begin
declare @RC integer;
set @RC = 0;
if "countordo" = 'C' then
select "count"() into @RC from "phone"
where((("who" = 'P') and not exists(select * from "person" where "personid" = "phone"."whoid"))
or(("who" = 'C') and not exists(select * from "company" where "companyid" = "phone"."whoid"))
or(("who" = 'CP') and not exists(select * from "employment" where "employmentid" = "phone"."whoid")));
return @RC
end if;
delete from "phone" where("who" = 'P')
and not exists(select * from "person" where "personid" = "phone"."whoid");
set @RC = @RC+(select @@rowcount);
delete from "phone" where "who" = 'CP'
and not exists(select * from "employment" where "employmentid" = "phone"."whoid");
set @RC = @RC+(select @@rowcount);
delete from "phone" where("who" = 'C')
and not exists(select * from "company" where "companyid" = "phone"."whoid");
set @RC = @RC+(select @@rowcount);
call "AuditLog"('CLEANUP',null,"string"(@RC,' Unattached Phone Records deleted '),null,null);
return @RC
end