 
  DECLARE @ControlInfoID bigint
  select @ControlInfoID = ControlInfoID from tbControlInfo where ControlInfo = 'OPShift Payment OnDemand' 

declare
   @EntityIdString VARCHAR(max)
SELECT @EntityIdString = COALESCE(@EntityIdString + ', ', '') + CAST(entityid AS VARCHAR(10)) 
FROM tbentity where entityTypeId = 4 and status = 'open'

DECLARE @EntityId bigint
DECLARE cur CURSOR FOR SELECT value from dbo.fn_Split(@EntityIdString,',')
OPEN cur

FETCH NEXT FROM cur INTO @EntityId

WHILE @@FETCH_STATUS = 0

BEGIN

if exists (select * from tbEntity with (nolock) where EntityID = @EntityId)

BEGIN

declare @transactioncashdepositId1 as bigint
declare @transactioncashdepositId2 as bigint

select top 1 @transactioncashdepositId1 = TransactionCashDepositID from tbTransactionCashDeposit with (nolock)
 where EntityID =@EntityId and CashDepositDate = '2020-1-15' and ClerkName = 'Shapiro, Yehoshua' and RegisterSales <> 0

insert into tbTransactionCashDepositControlInfo(TransactionCashDepositID, ControlInfoID, ControlInfoAmount, ControlInfoCount)
values(@transactioncashdepositId1, @ControlInfoID, 20.00, 1)

END

FETCH NEXT FROM cur INTO @EntityId
END


CLOSE cur
DEALLOCATE cur