Print ''
Print '----------------------------------------------------'
Print ' Cleanup ProcessData '
Print ' FirstRevision: V2.1.1   17.12.2012'
-- Print ' CurrRevision : V'
Print ' Autor        : J.Feuerstein'
Print ' '
Print ' Database Name: #DB_N#'
Print '----------------------------------------------------'
GO

use [#DB_N#]

Print ''
Print '#Step: Cleanup all NON Last ProcessData Results'
DELETE
FROM
  dbo.JobControl_ObjectsProcessData_tbl
WHERE
  dbo.JobControl_ObjectsProcessData_tbl.Row_isLastRes <> 1

Print ''
Print '#Step: Cleanup Non Referenced ProcessData Results (for example Process Data of Object Data has been deleted)'
DELETE
FROM
  dbo.JobControl_ObjectsProcessData_tbl
WHERE
  dbo.JobControl_ObjectsProcessData_tbl.ID IN
  (SELECT dbo.JobControl_ObjectsProcessData_tbl.ID FROM dbo.JobControl_ObjectsProcessData_tbl
   LEFT OUTER JOIN dbo.Objects_tbl ON (dbo.JobControl_ObjectsProcessData_tbl.Objects_tbl_ID = dbo.Objects_tbl.Id)
   WHERE dbo.Objects_tbl.Id IS NULL)

GO

Print ''
Print '---------------------------------------------------------'
Print ' Script executed'
Print '---------------------------------------------------------'
GO
