'Insert millions of rows into a SQL Server table

I have 2 tables one is record and other is codes table. I would like to insert millions of codes into the codes table. It has a foreign key reference to the record key. Before inserting to the codes table we need to check if the codes already exist or not.

Example: Codes starting number = 50 and quantity = 5.

So it will generate 50,51,52,53,54 values in the codes table and ids which will refer to the record table.

In actual I am inserting millions of codes. Right now I have used a while loop which makes the whole process slow. Will batch be of any use. If so please let me know how.

My SQL query is:

/

*

--EXEC  [SaveCodesAndPrintedEncodingByQuantity] @code ='2000000' , @quantity =100000, @recordType ='DocumentRecord',@codeType='MAGID'  
ALTER PROCEDURE [dbo].[SaveCodesAndPrintedEncodingByQuantity] (
    @code VARCHAR(max)
    ,@codeLength INT = 9
    ,@quantity INT
    ,@recordType VARCHAR(max)
    ,@codeType VARCHAR(max)
    )
AS
BEGIN
    DECLARE @OutputId AS TABLE (Id INT)
    DECLARE @codeInt INT;
    DECLARE @output VARCHAR(max)
    DECLARE @sum INT = 0
    DECLARE @codeValue INT
    DECLARE @OutParam VARCHAR(max)
    DECLARE @RecordId INT
    DECLARE @BatchSize INT;
    SET @codeInt = CONVERT(BIGINT, @code)
    SET @sum = @codeInt + @quantity
    
    BEGIN TRANSACTION;
    
    BEGIN TRY
        WHILE (@codeInt < @sum)
        BEGIN
            SET @codeValue = @codeInt
            EXEC [dbo].[GetPrintedEncoding] @code = @codeInt
                ,@OutParam = @output OUTPUT
            IF NOT EXISTS (
                    SELECT 1
                    FROM codes
                    WHERE code =  @codeInt
                    )
            BEGIN
                INSERT INTO RECORDS (
                    RecordType
                    ,DateCreated
                    )
                OUTPUT inserted.Id
                INTO @OutputId(Id)
                VALUES (
                    @recordType
                    ,GETDATE()
                    )
                SELECT @RecordId = Id
                FROM @OutputId
                INSERT INTO CODES (
                    CODE
                    ,RecordID
                    ,CodeType
                    ,PrintedEncoding
                    )
                VALUES (
                     @codeValue             
                    ,@RecordId
                    ,@codeType
                    ,@output
                    )
            END
            ELSE
            BEGIN
                RAISERROR (
                        'Code already exists'
                        ,16
                        ,1
                        )
                     
            END
            SET @codeInt = @codeInt + 1
        END
        COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
    SELECT ERROR_MESSAGE() AS ErrorMessage;
    
        ROLLBACK
    END CATCH
END


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source