USE LoadTest --@DaysToKeep - indicates the number of previous days records to keep. For example "30" indicates that I want to delete test results older than 30 days DECLARE @DaysToKeep int Set @DaysToKeep = 30 DECLARE @LoadTestRunId int DECLARE OldLoadTestsCursor CURSOR FOR SELECT LoadTestRunId FROM LoadTestRun WHERE datediff(dd, StartTime, getdate()) > @DaysToKeep OPEN OldLoadTestsCursor FETCH NEXT FROM OldLoadTestsCursor INTO @LoadTestRunId WHILE @@FETCH_STATUS = 0 BEGIN EXEC Prc_DeleteLoadTestRun @LoadTestRunId FETCH NEXT FROM OldLoadTestsCursor INTO @LoadTestRunId END CLOSE OldLoadTestsCursor DEALLOCATE OldLoadTestsCursor