====== pears.DBCleanupOrphanOLEDocument ======
Generated schema reference. Regenerate this page from the SQL unload; keep hand-maintained business notes in the narrative namespace.
===== Original SQL =====
create function "pears"."DBCleanupOrphanOLEDocument"(
/* 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 "OLEDocument"
where((("ownertype" = 'P') and not exists(select * from "person" where "personid" = "OLEDocument"."ownerid"))
or(("ownertype" = 'C') and not exists(select * from "company" where "companyid" = "OLEDocument"."ownerid"))
or(("ownertype" = 'V') and not exists(select * from "vacancy" where "vacancyid" = "OLEDocument"."ownerid"))
or(("ownertype" = 'R') and not exists(select * from "progress" where "progressid" = "OLEDocument"."ownerid"))
or(("ownertype" = 'L') and not exists(select * from "placement" where "placementid" = "OLEDocument"."ownerid")));
return @RC
end if;
delete from "OLEDocument" where("ownertype" = 'P')
and not exists(select * from "person" where "personid" = "OLEDocument"."ownerid");
set @RC = @RC+(select @@rowcount);
delete from "OLEDocument" where("ownertype" = 'C')
and not exists(select * from "company" where "companyid" = "OLEDocument"."ownerid");
set @RC = @RC+(select @@rowcount);
delete from "OLEDocument" where("ownertype" = 'V')
and not exists(select * from "vacancy" where "vacancyid" = "OLEDocument"."ownerid");
set @RC = @RC+(select @@rowcount);
delete from "OLEDocument" where("ownertype" = 'R')
and not exists(select * from "progress" where "progressid" = "OLEDocument"."ownerid");
set @RC = @RC+(select @@rowcount);
delete from "OLEDocument" where("ownertype" = 'L')
and not exists(select * from "placement" where "placementid" = "OLEDocument"."ownerid");
set @RC = @RC+(select @@rowcount);
call "AuditLog"('CLEANUP',null,"string"(@RC,' Unattached OLEDocument Records deleted '),null,null);
return @RC
end