ALTER FUNCTION [dbo].[f_NextID](@tabname VARCHAR(50)) RETURNS char(8) AS BEGIN DECLARE @charval CHAR(8) IF LOWER(@tabname) ='B_Fee_Type' SELECT @charval=RIGHT(100000001+ISNULL(MAX(id),0),8) FROM B_Fee_Type WITH(XLOCK,PAGLOCK) ELSE SET @charval='00000001' RETURN @charval END
--在表中应用函数 CREATE TABLE tb( id char(8) PRIMARY KEY DEFAULT dbo.f_NextBH('tb'), col int) --插入资料 BEGIN TRAN INSERT tb(col) VALUES(1) INSERT tb(col) VALUES(2) INSERT tb(col) VALUES(3) DELETE tb WHERE col=3 INSERT tb(col) VALUES(4) INSERT tb(id,col) VALUES(dbo.f_NextBH('tb'),14) COMMIT TRAN --显示结果 SELECT * FROM tb /*--结果 id col ---------------- ----------- 00000001 1 00000002 2 00000003 4 00000004 14 --*/