DECLARE @i INT =1 WHILE(@i>0) BEGIN DELETE TOP (1000) FROM CompanyUserPermission WHERE PermissionTypeId = 1 AND CompanyUserId IN (SELECT LoginId FROM CloginDB WITH(NOLOCK) WHERE LoginId In (SELECT CompanyUserId FROM CompanyUserPermission WITH(NOLOCK) WHERE PermissionTypeId = 1 GROUP BY CompanyUserId) And LastloginDateN < Convert(varchar,DATEADD(DAY, -3, GetDate()), 112) ) SET @i=@@ROWCOUNT END