Encapsulate the ID generation into one or more stored procedures and use them from client as well as from T-SQL side.
-- ======================================================================
-- id table
CREATE TABLE Ids (
Name VARCHAR(30) PRIMARY KEY CLUSTERED
,NextId INT NOT NULL
);
-- ======================================================================
-- init ids for Inventory
INSERT INTO Ids VALUES('Inventory', 1);
GO
-- ======================================================================
-- general procedure to allocate new ids
ALTER PROCEDURE AllocateIds
@nextId INT OUTPUT
,@name VARCHAR(30)
,@count INT
AS
SET NOCOUNT ON;
UPDATE Ids SET
@nextId = NextId
,NextId += @count
WHERE Name = @name
GO
-- ======================================================================
-- special procedure to get new inventory ids
ALTER PROCEDURE GetInventoryId
@inventoryId VARCHAR(30) OUTPUT
AS
SET NOCOUNT ON;
DECLARE @nextId INT;
EXECUTE AllocateIds @nextId OUTPUT, 'Inventory', 1;
SET @inventoryId = 'INV-' + REPLICATE('0', 4 - LEN(CAST(@nextId AS VARCHAR(20)))) + CAST(@nextId AS VARCHAR(20));
GO
-- ======================================================================
-- sample usage
DECLARE @inventoryId VARCHAR(30);
EXECUTE GetInventoryId @inventoryId OUTPUT;
PRINT @inventoryId;
Greets
Flo