-- 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-DEPARTMENTS' 
);
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 
    ('0', 'OTHER'),
    ('1','K5'),
    ('2','AK'),
    ('3','PU')

-- 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;
