USE [Database]

IF OBJECT_ID('[Procedures].[get_next_barcode]', 'P') IS NOT NULL
  DROP PROCEDURE [Procedures].[get_next_barcode];
GO
CREATE PROCEDURE [Procedures].[get_next_barcode]
  @next_barcode AS BIGINT OUTPUT
AS
  BEGIN TRANSACTION;
  SET NOCOUNT ON;

  SET @next_barcode = (
    SELECT CAST([value] AS BIGINT) + 1
    FROM [Tables].[additional_variables]
    WHERE [id] = 1
  );

  UPDATE [Tables].[additional_variables]
  SET [value] = CAST(@next_barcode AS NVARCHAR(13))
  WHERE [id] = 1;
  COMMIT TRANSACTION;
GO
