USE StationService
DECLARE @Deleted_Rows INT;
SET @Deleted_Rows = 1;
WHILE (@Deleted_Rows > 0)
  BEGIN   
     DELETE TOP (100000) Unitlog Where Time < getdate()-30
  SET @Deleted_Rows = @@ROWCOUNT;
END

SET @Deleted_Rows = 1;
WHILE (@Deleted_Rows > 0)
  BEGIN  
     DELETE TOP (100000) Totallog Where Time < getdate()-30
  SET @Deleted_Rows = @@ROWCOUNT;
END

SET @Deleted_Rows = 1;
WHILE (@Deleted_Rows > 0)
  BEGIN
     DELETE TOP (100000) Tanklog Where Time < getdate()-365
  SET @Deleted_Rows = @@ROWCOUNT;
END

SET @Deleted_Rows = 1;
WHILE (@Deleted_Rows > 0)
  BEGIN
     DELETE TOP (100000) Tanklog2 Where Time < getdate()-95
  SET @Deleted_Rows = @@ROWCOUNT;
END
begin
ALTER INDEX All ON dbo.Unitlog rebuild
ALTER INDEX All ON dbo.Totallog rebuild
ALTER INDEX All ON dbo.Tanklog rebuild
ALTER INDEX All ON dbo.Tanklog2 rebuild
end