--下面的代码生成长度为8的编号,编号以BH开头,其余6位为流水号。--得到新编号的函数CREATE FUNCTION f_NextBH()RETURNS char(8)ASBEGIN RETURN(SELECT 'BH'+RIGHT(1000001+ISNULL(RIGHT(MAX(BH),6),0),6) FROM tb WITH(XLOCK,PAGLOCK))ENDGO--在表中应用函数CREATE TABLE tb(BH char(8) PRIMARY KEY DEFAULT dbo.f_NextBH(),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(BH,col) VALUES(dbo.f_NextBH(),14)COMMIT TRAN--显示结果SELECT * FROM tb/*--结果BH col---------------- -----------BH000001 1BH000002 2BH000003 4BH000004 14--*/ create table tb(id int identity,name varchar(10),code as 'BH'+right('0000'+cast(id as varchar),5))goinsert tb(name) select 'A'union all select 'B'union all select 'C'union all select 'D'select * from tbdrop table tb/*id name code ----------- ---------- ------------1 A BH000012 B BH000023 C BH000034 D BH00004