create table #Temp
( 
    StartDate DateTime, 
    EndDate DatetIme,
    EntityId bigint
)

declare @date as datetime = '2018-1-1'
;with Numbers (Number) as
   (select row_number() over (order by object_id)
   from sys.all_objects)
   insert into #Temp
select  dateadd(month, number, @date), eomonth(dateadd(month, number, @date)), e.EntityID
from Numbers
CROSS JOIN tbEntity e WITH (NOLOCK)
where number <= datediff(month, @date, getdate())
and e.EntityTypeID = 4 and e.Status = 'open'

 
 DECLARE @StartDate datetime
 DECLARE @EndDate datetime
 DECLARE @EntityId bigint
 
 DECLARE cur CURSOR FOR select StartDate, EndDate, EntityId from #Temp
 OPEN cur
 
 FETCH NEXT FROM cur INTO @StartDate, @EndDate, @EntityId
 
WHILE @@FETCH_STATUS = 0 
 
BEGIN  

UPDATE tbTransactionSummary  
           SET tbTransactionSummary.OtherInfo3 = CustomerInformation.TaxExemptAccountName
          FROM tbTransactionSummary 
                JOIN CustomerInformation ON tbTransactionSummary.CustomerInformationID = CustomerInformation.CustomerInformationID 
                                            AND CustomerInformation.EntityID = 0 and (TaxExemptAccountName <> '' and TaxExemptAccountName is not null)
          WHERE tbTransactionsummary.EntityID = @EntityId and
           tbTransactionSummary.businessday between @StartDate and @EndDate  
          and  tbTransactionSummary.TaxExempt = 1                             


FETCH NEXT FROM cur INTO @StartDate, @EndDate, @EntityId

END

CLOSE cur    
DEALLOCATE cur


If(OBJECT_ID('tempdb..#temp') Is Not Null)
Begin
    Drop Table #Temp
End

