模拟SQL SERVER2000数据库的自增ID
数据表:
存储过程:
CREATE TABLE [dbo].[ID_CODEID] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[ID_NAME] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL , --表名
[ID_VALUE] [int] NOT NULL --模拟的自增ID值
) ON [PRIMARY]
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[ID_NAME] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL , --表名
[ID_VALUE] [int] NOT NULL --模拟的自增ID值
) ON [PRIMARY]
存储过程:
CREATE PROCEDURE sp_get_uni_id
@NAME varchar(50),
@STEP int,
@VALUE varchar(20) OUTPUT
AS BEGIN
begin transaction
UPDATE ID_CODEID SET ID_VALUE = ID_VALUE + @STEP WHERE ID_NAME = @NAME
SELECT @VALUE = ID_VALUE - @STEP + 1 FROM ID_CODEID WHERE ID_NAME = @NAME
IF @VALUE IS NULL
BEGIN
INSERT INTO ID_CODEID (ID_NAME, ID_VALUE) VALUES (@NAME, @STEP)
SELECT @VALUE = 1
END
commit
END;
@NAME varchar(50),
@STEP int,
@VALUE varchar(20) OUTPUT
AS BEGIN
begin transaction
UPDATE ID_CODEID SET ID_VALUE = ID_VALUE + @STEP WHERE ID_NAME = @NAME
SELECT @VALUE = ID_VALUE - @STEP + 1 FROM ID_CODEID WHERE ID_NAME = @NAME
IF @VALUE IS NULL
BEGIN
INSERT INTO ID_CODEID (ID_NAME, ID_VALUE) VALUES (@NAME, @STEP)
SELECT @VALUE = 1
END
commit
END;