USE SEEMSDB SET NOCOUNT ON PRINT CHAR(13) + CHAR(10) + 'Disabling "FK_HardDisksInfo_Computers" Constraint from [HardDisksInfo]' ALTER TABLE HardDisksInfo NOCHECK CONSTRAINT FK_HardDisksInfo_Computers PRINT CHAR(13) + CHAR(10) + 'Disabling "FK_UserReport_Computers" Constraint from [Users]' ALTER TABLE Users NOCHECK CONSTRAINT FK_UserReport_Computers PRINT CHAR(13) + CHAR(10) + 'Disabling "FK_CompCommands_Computers" Constraint from [CompCommands]' ALTER TABLE CompCommands NOCHECK CONSTRAINT FK_CompCommands_Computers PRINT CHAR(13) + CHAR(10) + 'Purging records older than 90 days from table [HardDiskEncrEvents]' DELETE HardDiskEncrEvents From Computers t1 JOIN HardDisksInfo t2 ON t1.CompID = t2.CompID JOIN HardDiskEncrEvents t3 on t2.DiskID = t3.DiskID WHERE (t1.LastCheckIn < GETDATE() - 90) PRINT '(' + CONVERT(varchar,@@rowcount) + ' row(s) affected)' PRINT CHAR(13) + CHAR(10) + 'Purging records older than 90 days from table [DiskUserInfo]' DELETE DiskUserInfo From Computers t1 JOIN HardDisksInfo t2 ON t1.CompID = t2.CompID JOIN DiskUserInfo t3 on t2.DiskID = t3.DiskID WHERE (t1.LastCheckIn < GETDATE() - 90) PRINT '(' + CONVERT(varchar,@@rowcount) + ' row(s) affected)' PRINT CHAR(13) + CHAR(10) + 'Purging records older than 90 days from table [HardDisksInfo]' DELETE HardDisksInfo From Computers t1 JOIN HardDisksInfo t2 ON t1.CompID = t2.CompID WHERE (t1.LastCheckIn < GETDATE() - 90) PRINT '(' + CONVERT(varchar,@@rowcount) + ' row(s) affected)' PRINT CHAR(13) + CHAR(10) + 'Purging records older than 90 days from table [CompClientEvents]' DELETE CompClientEvents From Computers t1 JOIN CompClientEvents t2 ON t1.CompID = t2.CompID WHERE (t1.LastCheckIn < GETDATE() - 90) PRINT '(' + CONVERT(varchar,@@rowcount) + ' row(s) affected)' PRINT CHAR(13) + CHAR(10) + 'Purging records older than 90 days from table [Users]' DELETE Users From Computers t1 JOIN Users t2 ON t1.CompID = t2.CompID WHERE (t1.LastCheckIn < GETDATE() - 90) PRINT '(' + CONVERT(varchar,@@rowcount) + ' row(s) affected)' PRINT CHAR(13) + CHAR(10) + 'Purging records older than 90 days from table [Computers]' DELETE From Computers WHERE (LastCheckIn < GETDATE() - 90) PRINT '(' + CONVERT(varchar,@@rowcount) + ' row(s) affected)' PRINT CHAR(13) + CHAR(10) + 'Purging records marked deleted from table [HardDiskEncrEvents]' DELETE HardDiskEncrEvents From Computers t1 JOIN HardDisksInfo t2 ON t1.CompID = t2.CompID JOIN HardDiskEncrEvents t3 on t2.DiskID = t3.DiskID WHERE t1.Visible=0 PRINT '(' + CONVERT(varchar,@@rowcount) + ' row(s) affected)' PRINT CHAR(13) + CHAR(10) + 'Purging records marked deleted from table [DiskUserInfo]' DELETE DiskUserInfo From Computers t1 JOIN HardDisksInfo t2 ON t1.CompID = t2.CompID JOIN DiskUserInfo t3 on t2.DiskID = t3.DiskID WHERE t1.Visible=0 PRINT '(' + CONVERT(varchar,@@rowcount) + ' row(s) affected)' PRINT CHAR(13) + CHAR(10) + 'Purging records marked deleted from table [HardDisksInfo]' DELETE HardDisksInfo From Computers t1 JOIN HardDisksInfo t2 ON t1.CompID = t2.CompID WHERE t1.Visible=0 PRINT '(' + CONVERT(varchar,@@rowcount) + ' row(s) affected)' PRINT CHAR(13) + CHAR(10) + 'Purging records marked deleted from table [CompClientEvents]' DELETE CompClientEvents From Computers t1 JOIN CompClientEvents t2 ON t1.CompID = t2.CompID WHERE t1.Visible=0 PRINT '(' + CONVERT(varchar,@@rowcount) + ' row(s) affected)' PRINT CHAR(13) + CHAR(10) + 'Purging records marked deleted from table [Users]' DELETE Users From Computers t1 JOIN Users t2 ON t1.CompID = t2.CompID WHERE t1.Visible=0 PRINT '(' + CONVERT(varchar,@@rowcount) + ' row(s) affected)' PRINT CHAR(13) + CHAR(10) + 'Purging records marked deleted from table [Computers]' DELETE From Computers WHERE Visible=0 PRINT '(' + CONVERT(varchar,@@rowcount) + ' row(s) affected)' PRINT CHAR(13) + CHAR(10) + 'Enabling "FK_CompCommands_Computers" Constraint from [CompCommands]' ALTER TABLE CompCommands CHECK CONSTRAINT FK_CompCommands_Computers PRINT CHAR(13) + CHAR(10) + 'Enabling "FK_UserReport_Computers" Constraint from [Users]' ALTER TABLE Users CHECK CONSTRAINT FK_UserReport_Computers PRINT CHAR(13) + CHAR(10) + 'Enabling "FK_HardDisksInfo_Computers" Constraint from [HardDisksInfo]' ALTER TABLE HardDisksInfo CHECK CONSTRAINT FK_HardDisksInfo_Computers