-- Create Dictionary
DECLARE @JustForDebug TINYINT = 0;

-- SETUP VARIABLES
DECLARE @Number INT = (SELECT COUNT(id) + 1  FROM [diunabi-morska].[dbo].[Layers]);
DECLARE @Name NVARCHAR(50) = CONCAT(
    'L', @Number, '-D-D6-SELL-CODES' 
);
DECLARE @LayerId UNIQUEIDENTIFIER = NEWID();

SELECT @Name AS Name;

IF @JustForDebug = 1
BEGIN
    SELECT 'Just for debug' AS Logger;
    RETURN;
END;

INSERT INTO [diunabi-morska].[dbo].[Layers]
([Id], [Number], [Name], [CreatedAt], [ModifiedAt], [IsDeleted], [CreatedById], [ModifiedById], [Type])
VALUES (@LayerId, @Number, @Name, GETDATE(), GETDATE(), 0, '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 3);

DECLARE @Array TABLE (
    Code NVARCHAR(50),
    Desc1 NVARCHAR(50)
);

INSERT INTO @Array (Code, Desc1)
VALUES 
    ('1002', '1102'),
    ('1003','1202'),
    ('1008','1302'),
    ('1009','1302'),
    ('9085','1203'),
    ('1010','1304'),
    ('9086','1005'),
    ('1021','1206'),
    ('9089','1207'),
    ('9091','1208')

-- Loop through the array and insert into the target table
DECLARE @Code NVARCHAR(50);
DECLARE @Desc1 NVARCHAR(50);

DECLARE CursorArray CURSOR FOR
SELECT Code, Desc1 FROM @Array;

OPEN CursorArray;

FETCH NEXT FROM CursorArray INTO @Code, @Desc1;

WHILE @@FETCH_STATUS = 0
BEGIN
    INSERT INTO [diunabi-morska].[dbo].[Records]
    ([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId])
    VALUES (NEWID(), @Code, @Desc1, GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, @LayerId);

    FETCH NEXT FROM CursorArray INTO @Code, @Desc1;
END;

CLOSE CursorArray;
DEALLOCATE CursorArray;
