My goal is to insert Temp Table Data in batches to Primary Table, with every batch having a total of 10 records.I'm unable to set 10 count batches.
Example is as follows:
Temp Table Data
===============
034840 7038 001 1589000.00 2022042170100380380041013
034841 7038 001 160000.00 2022042170100380380041015
386551 7038 001 13680.00 2022042170100400400021057
034677 7038 001 195110.19 2022042170100410410021059
386255 7038 001 81450.00 2022042170100410410021060
386801 7038 001 222916.20 2022042170100410410021061
386720 7038 001 10600.00 2022042170100430430011007
386750 7038 001 95114.99 2022042170100430430011008
386743 7038 001 42743.66 2022042170100430430011010
385592 7038 001 12500.00 2022042170100430430011036
386800 7038 001 251786.00 2022042170100490490011027
029831 7038 002 21417.81 2022042174630140400081020
012520 7038 002 32176.46 2022042174630140400081021
343123 7038 002 135000.00 2022042173110470200201007
335751 7038 002 300000.00 2022042172870868086850126
338589 7038 002 8128.00 2022042173110040200141003
Required resalt
==================
BATCH NO
7038001-000001 034840 7038 001 1589000.00 2022042170100380380041013
7038001-000001 034841 7038 001 160000.00 2022042170100380380041015
7038001-000001 386551 7038 001 13680.00 2022042170100400400021057
7038001-000001 034677 7038 001 195110.19 2022042170100410410021059
7038001-000001 386255 7038 001 81450.00 2022042170100410410021060
7038001-000002 386801 7038 001 222916.20 2022042170100410410021061
7038001-000002 386720 7038 001 10600.00 2022042170100430430011007
7038001-000002 386750 7038 001 95114.99 2022042170100430430011008
7038001-000002 386743 7038 001 42743.66 2022042170100430430011010
7038001-000002 385592 7038 001 12500.00 2022042170100430430011036
7038001-000003 386800 7038 001 251786.00 2022042170100490490011027
7038002-000004 029831 7038 002 21417.81 2022042174630140400081020
7038002-000004 012520 7038 002 32176.46 2022042174630140400081021
7038002-000004 343123 7038 002 135000.00 2022042173110470200201007
7038002-000004 335751 7038 002 300000.00 2022042172870868086850126
7038002-000004 338589 7038 002 8128.00 2022042173110040200141003
What I have tried:
Get Batch No Script
==================
ALTER FUNCTION [dbo].[GET_MAX_BATCH_NO](@BRANCH_CODE VARCHAR(3))
RETURNS VARCHAR(20)
AS
BEGIN
DECLARE @MAX_BATCH_NUMBER INT
DECLARE @CLEARING_CYCLE INT
DECLARE @PROCESS_DATE VARCHAR(10)
DECLARE @BANK_CODE VARCHAR(4)
DECLARE @BATCH_NO VARCHAR(20)
DECLARE @ORG_BRANCH_CODE VARCHAR(3)
DECLARE @ORG_BRANCH_EXTENSION VARCHAR(2) = '00'
--DECLARE @BRANCH_CODE VARCHAR(3)
SET @PROCESS_DATE = (SELECT PROCESS_DATE FROM DMSIMAGO.dbo.MSYSTEMSETTING)
SET @CLEARING_CYCLE = (SELECT TOP 1 CLEARING_CYCLE FROM DMSIMAGO.dbo.IMG_IWD_INWARD_SPLIT)
SET @ORG_BRANCH_CODE = @BRANCH_CODE
SET @BANK_CODE = (SELECT TOP 1 BANK_CODE FROM DMSIMAGO.dbo.IMG_IWD_INWARD_SPLIT)
SELECT @MAX_BATCH_NUMBER = ISNULL(MAX(CAST(SUBSTRING(BATCH_NO, 11, 5) AS INT)), 0)
FROM DMSIMAGO.dbo.IMG_IWD_INWARDCLEARING
WHERE PROCESS_DATE = @PROCESS_DATE
AND CLEARING_CYCLE = @CLEARING_CYCLE
IF (@MAX_BATCH_NUMBER IS NULL)
BEGIN
SET @MAX_BATCH_NUMBER = 1;
END
ELSE
BEGIN
SET @MAX_BATCH_NUMBER = @MAX_BATCH_NUMBER + 1;
END;
SET @BATCH_NO = @BANK_CODE + @ORG_BRANCH_CODE + '-' + @ORG_BRANCH_EXTENSION + RIGHT('0000' + CAST(@MAX_BATCH_NUMBER AS VARCHAR), 4)
RETURN @BATCH_NO;
END;
Data Insert Script
==================
ALTER PROCEDURE [dbo].[IMG_IWD_INWARD_SPLIT_BATCH]
AS
DECLARE @TRANSACTION_MODE INT
DECLARE @BATCH_TYPE INT
DECLARE @BRANCH_CODE VARCHAR(3)
DECLARE @BATCH_NO VARCHAR(20)
DECLARE @ROW_COUNT INT = 1;
BEGIN
SET NOCOUNT ON;
DECLARE CUR_Branch CURSOR FOR
SELECT BRANCH_CODE,TRANSACTION_MODE FROM IMG_IWD_INWARD_SPLIT GROUP BY BRANCH_CODE,TRANSACTION_MODE
OPEN CUR_Branch
FETCH NEXT FROM CUR_Branch INTO @BRANCH_CODE,@TRANSACTION_MODE
WHILE @@FETCH_STATUS=0
BEGIN
while @ROW_COUNT <= 150
BEGIN
BEGIN
SET @BATCH_NO = dbo.GET_MAX_BATCH_NO(@BRANCH_CODE);
INSERT INTO IMG_IWD_INWARDCLEARING
(
BATCH_NO,
INT_CHEQUE_NO,
OCR_CHEQUE_NO,
CHEQUE_NO,
INT_BANK_CODE,
OCR_BANK_CODE,
BANK_CODE,
INT_BRANCH_CODE,
OCR_BRANCH_CODE,
BRANCH_CODE,
INT_ACCOUNT_NO,
OCR_ACCOUNT_NO,
ACCOUNT_NO ,
INT_TXN_CODE,
OCR_TXN_CODE,
TXN_CODE,
AMOUNT,
CURRENCY_CODE,
UNIQUE_IDENTIFIER,
PREVIOUS_UNIQUE_IDENTIFIER
)
SELECT
@BATCH_NO AS BATCH_NO,
CHEQUE_NO AS INT_CHEQUE_NO,
CHEQUE_NO AS OCR_CHEQUE_NO,
CHEQUE_NO,
BANK_CODE AS INT_BANK_CODE,
BANK_CODE AS OCR_BANK_CODE,
BANK_CODE,
BRANCH_CODE AS INT_BRANCH_CODE,
BRANCH_CODE AS OCR_BRANCH_CODE,
BRANCH_CODE,
ACCOUNT_NO AS INT_ACCOUNT_NO,
ACCOUNT_NO AS OCR_ACCOUNT_NO,
ACCOUNT_NO AS ACCOUNT_NO ,
TXN_CODE AS INT_TXN_CODE,
TXN_CODE AS OCR_TXN_CODE,
TXN_CODE,
AMOUNT,
'LKR' AS CURRENCY_CODE,
UNIQUE_IDENTIFIER,
UNIQUE_IDENTIFIER AS PREVIOUS_UNIQUE_IDENTIFIER
FROM IMG_IWD_INWARD_SPLIT
WHERE 1=1
AND BRANCH_CODE = @BRANCH_CODE
AND TRANSACTION_MODE = @TRANSACTION_MODE
END
set @ROW_COUNT = @ROW_COUNT + 1
end
fetch next from CUR_Branch
FETCH NEXT FROM CUR_Branch INTO @BRANCH_CODE,@TRANSACTION_MODE
END
CLOSE CUR_Branch
DEALLOCATE CUR_Branch
END