--下面的代码生成长度为12的编号,编号以BH开头,前四位数字为col字段,其余6位为流水号。--得到新编号的函数alter FUNCTION f(@col int)RETURNS char(12)ASBEGIN RETURN(SELECT 'BH'+RIGHT(10000+@col,4)+RIGHT(1000001+ISNULL(RIGHT(MAX(BH),6),0),6) FROM tb WITH(XLOCK,PAGLOCK) where col=@col) ENDGO--在表中应用函数drop table tb;CREATE TABLE tb(BH char(12) PRIMARY KEY,col int not null)--插入资料BEGIN TRAN INSERT tb(BH,col) VALUES(dbo.f(1),1) INSERT tb(BH,col) VALUES(dbo.f(2),2) INSERT tb(BH,col) VALUES(dbo.f(3),3) INSERT tb(BH,col) VALUES(dbo.f(4),4) INSERT tb(BH,col) VALUES(dbo.f(1),1) INSERT tb(BH,col) VALUES(dbo.f(1),1) INSERT tb(BH,col) VALUES(dbo.f(1),1) INSERT tb(BH,col) VALUES(dbo.f(2),2) INSERT tb(BH,col) VALUES(dbo.f(2),2) INSERT tb(BH,col) VALUES((SELECT 'BH'+RIGHT(10000+2,4)+RIGHT(1000001+ISNULL(RIGHT(MAX(BH),6),0),6) FROM tb WITH(XLOCK,PAGLOCK) where col=2) ,2)COMMIT TRAN--显示结果SELECT * FROM tb/*结果BH0001000001 1BH0001000002 1BH0001000003 1BH0001000004 1BH0002000001 2BH0002000002 2BH0002000003 2BH0002000004 2BH0003000001 3BH0004000001 4*/