-- Create Dictionary
DECLARE @JustForDebug TINYINT = 0;

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

SELECT @Name AS Name;

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

INSERT INTO [DiunaBI-PedrolloPL].[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 
    ('01','<nieznany>'),
    ('02','DOLNOŚLĄSKIE'),
    ('03','KUJAWSKO-POMORSKIE'),
    ('04','LUBELSKIE'),
    ('05','LUBUSKIE'),
    ('06','ŁÓDZKIE'),
    ('07','MAŁOPOLSKIE'),
    ('08','MAZOWIECKIE'),
    ('09','OPOLSKIE'),
    ('10','PODKARPACKIE'),
    ('11','PODLASKIE'),
    ('12','POMORSKIE'),
    ('13','ŚLĄSKIE'),
    ('14','ŚWIĘTOKRZYSKIE'),
    ('15','WARMIŃSKO-MAZURSKIE'),
    ('16','WIELKOPOLSKIE'),
    ('17','ZACHODNIOPOMORSKIE');

-- 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-PedrolloPL].[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;
