DECLARE @contractproductid INT DECLARE @customernumberid INT DECLARE @productid INT DECLARE @kansdato VARCHAR(8) DECLARE @today VARCHAR(8) SET @today = CONVERT(VARCHAR(8), GETDATE(), 112) DECLARE DeactivateMobile CURSOR fast_forward FOR SELECT DISTINCT cp2.contractproductid, cn.customernumberid, cp2.productid, cp3.property7 FROM customernumber cn (nolock) join contractproductcustomernumber cpcn (nolock) ON cpcn.customernumberid = cn.customernumberid join contractproduct cp (nolock) ON cp.contractproductid = cpcn.contractproductid join contractproduct cp2 (nolock) ON cp2.contractid = cp.contractid join contractproduct cp3 (nolock) ON cp3.contractid = cp.contractid WHERE cn.numbermain in ( select cm.number from contractproduct cp (nolock) join contractproductcustomernumber cpcm (nolock) on cpcm.contractproductid = cp.contractproductid join customernumber cm (nolock) on cm.customernumberid = cpcm.customernumberid where 1 = 1 and productid = 11001 and contractproductstatusid = 1012 and activationdate is not null and deactivationdate is null and supplierid = 200005 ) and cp3.productid = 11042 OPEN DeactivateMobile FETCH NEXT FROM DeactivateMobile INTO @contractproductid, @customernumberid, @productid, @kansdato WHILE @@FETCH_STATUS = 0 BEGIN UPDATE customernumber SET flagswitchactive = 0, customernumberstatus = 26 WHERE customernumberid = @customernumberid UPDATE contractproduct SET deactivationdate = @today, activationdate = @today, flagbillingactive = 0, contractproductstatusid = 1013 WHERE contractproductid = @contractproductid and isnull(deactivationdate,'')='' and isnull(activationdate,'')='' UPDATE contractproduct SET deactivationdate = activationdate, flagbillingactive = 0, contractproductstatusid = 1013 WHERE contractproductid = @contractproductid and activationdate > @kansdato UPDATE contractproduct SET deactivationdate = @kansdato, flagbillingactive = 0, contractproductstatusid = 1013 WHERE contractproductid = @contractproductid and isnull(deactivationdate,'')='' UPDATE contractproduct SET contractproductstatusid = 225 WHERE contractproductid = @contractproductid and productid = 11042 UPDATE customernumberbilling SET datebillingstop = @kansdato WHERE flagcurrent = 1 and customernumberid = @customernumberid and ISNULL(datebillingstop, '') = '' FETCH NEXT FROM DeactivateMobile INTO @contractproductid, @customernumberid, @productid, @kansdato END CLOSE DeactivateMobile DEALLOCATE DeactivateMobile