CREATE PROC [dbo].[GetTableID]
@TableName NVARCHAR(64),
@ID NVARCHAR(25) OUTPUT
AS
BEGIN TRAN
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
IF (NOT EXISTS(SELECT * FROM TableID WHERE TableName = @TableName))
BEGIN
INSERT INTO TableID (TableName, IDValue) VALUES (@TableName, 1)
if(@@error <>0)
begin
rollback
return -1
end
END
DECLARE @TableIDNum INT,@strID NVARCHAR(15), @DomainName NVARCHAR(2)
SELECT @TableIDNum = IDValue FROM TableID WHERE TableName = @TableName
UPDATE TableID SET IDValue = @TableIDNum + 1 WHERE TableName = @TableName
if(@@error <>0)
begin
rollback
return -1
end
SET @strID = CONVERT(NVARCHAR(15), @TableIDNum)
SET @ID = @strID
COMMIT
GO