Click here to Skip to main content
16,021,687 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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
Posted
Updated 11-Jul-24 5:14am
v13
Comments
Richard Deeming 11-Jul-24 6:06am    
And?

Dumping two copies of the same 271 lines of unexplained code does not make a question.

Click the green "Improve question" and update your question to explain precisely what you are trying to do, what you have tried, and where you are stuck.
OriginalGriff 11-Jul-24 6:15am    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.

Use the "Improve question" widget to edit your question and provide better information.
Richard Deeming 11-Jul-24 7:05am    
No, repeating the title in the question still does not make this a question.

You need to explain precisely and clearly what you are trying to do, what you have tried, and where you are stuck.

The quality of answers you get will directly reflect the effort you put into asking the question. If you continue to put zero effort into your question, then nobody is going to put any effort into understand and answering it.
Richard Deeming 11-Jul-24 7:19am    
You are clearly incapable of asking a cogent question. The most successful debugging technique[^] is currently beyond your reach.

You can continue to make minor edits to the wording of your "question"; but until you can actually articulate precisely what you are trying to do, what you have tried, and where you are stuck, you will not get any answers.
Dave Kreskowiak 11-Jul-24 9:45am    
Seriously, go read this: Asking questions is a skill[^]

1 solution

This line is wrong
SQL
while @ROW_COUNT = 150
@ROW_COUNT will never be 150 because the line that increments it is within that loop. It should be
SQL
while @ROW_COUNT <= 150

EDIT:
Actually if using <=then you should also use
SQL
DECLARE @ROW_COUNT INT = 1;
OR leave the declaration as-is and use
SQL
while @ROW_COUNT < 150
 
Share this answer
 
v2
Comments
chamindat 11-Jul-24 10:51am    
its not work
Dave Kreskowiak 11-Jul-24 11:29am    
"It not work", or "It doesn't work" is the most USELESS problem description ever. It tells us nothing at all about the problem, what you expect the code to do, what is actually happening, any error messages, ... nothing at all.

Seriously. Go click the link in my other comment above and start reading it.

CHill60 12-Jul-24 7:23am    
There may well be other issues with your code but this is definitely something you need to fix first. After that you might like to actually tell me what does or doesn't happen. Help us to help you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900