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.property4 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 = 11037 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 activationdate = @kansdato --WHERE contractproductid = @contractproductid --AND isnull(activationdate,'')='' UPDATE contractproduct SET deactivationdate = @kansdato, flagbillingactive = 0, contractproductstatusid = 1013 WHERE contractproductid = @contractproductid AND isnull(deactivationdate,'')='' 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