--number is in the format: 'N000000001' create function f_getMaxNumber() returns varchar(10) as begin declare @no varchar(10) select @no=isnull('N'+right((select '000000000'+cast(cast(right(max(no),9) as int)+1 as varchar(10)) from tb),9), 'N000000001') return @no end gocreate table tb(col varchar(3),no varchar(10) default dbo.f_getMaxNumber()) insert tb(col) select 'abc' union all select 'sdf' union all select 'sdf'select * from tbdrop table tb drop function dbo.f_getMaxNumber /* col no ---- ---------- abc N000000001 sdf N000000002 sdf N000000003 */
--(4)生成流水号 if object_id('tb') is not null drop table tb drop function dbo.FC_Next create function dbo.FC_Next() returns char(8) as begin return (select 'BH'+right(1000001+isnull(right(max(BH),6),0),6) from tb) end create table tb ( BH char(8) primary key default dbo.FC_Next(), col int ) select * from tb
begin tran insert into tb (col) values (1) insert into tb (col) values (2) insert into tb(BH,col) values (dbo.FC_Next(),14) commit tran select * from tb
CREATE TABLE dbo.tb ( flg int NOT NULL IDENTITY (1, 1), id AS 'a'+right('000000'+convert(varchar,flg),6) PERSISTED not null PRIMARY KEY CLUSTERED , col1 nvarchar(50) NOT NULL ) ON [PRIMARY] GO
CREATE TABLE dbo.tb ( flg int NOT NULL IDENTITY (1, 1), id AS 'a'+right('000000'+convert(varchar,flg),6) PERSISTED not null PRIMARY KEY CLUSTERED , col1 nvarchar(50) NOT NULL ) ON [PRIMARY] GO insert into tb(col1) select 'abc' union all select 'def' union all select 'hij' select id,col1 from tb go drop table tb /* id col1 ------------- -------------------------------------------------- a000001 abc a000002 def a000003 hij */
--创建得到当前日期的视图
CREATE VIEW v_GetDate
AS
SELECT dt=CONVERT(CHAR(6),GETDATE(),12)
GO--得到新编号的函数
CREATE FUNCTION f_NextBH()
RETURNS char(12)
AS
BEGIN
DECLARE @dt CHAR(6)
SELECT @dt=dt FROM v_GetDate
RETURN(
SELECT @dt+RIGHT(1000001+ISNULL(RIGHT(MAX(BH),6),0),6)
FROM tb WITH(XLOCK,PAGLOCK)
WHERE BH like @dt+'%')
END
GO--在表中应用函数
CREATE TABLE tb(
BH char(12) PRIMARY KEY DEFAULT dbo.f_NextBH(),
col int)--插入资料
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)--显示结果
SELECT * FROM tb
/*--结果
BH col
------------------- -----------
050405000001 1
050405000002 2
050405000003 4
050405000004 14
--*/
create function f_getMaxNumber()
returns varchar(10)
as
begin
declare @no varchar(10)
select @no=isnull('N'+right((select '000000000'+cast(cast(right(max(no),9) as int)+1 as varchar(10)) from tb),9), 'N000000001')
return @no
end
gocreate table tb(col varchar(3),no varchar(10) default dbo.f_getMaxNumber())
insert tb(col)
select 'abc' union all
select 'sdf' union all
select 'sdf'select * from tbdrop table tb
drop function dbo.f_getMaxNumber
/*
col no
---- ----------
abc N000000001
sdf N000000002
sdf N000000003
*/
if object_id('tb') is not null drop table tb
drop function dbo.FC_Next
create function dbo.FC_Next()
returns char(8)
as
begin
return (select 'BH'+right(1000001+isnull(right(max(BH),6),0),6) from tb)
end
create table tb
(
BH char(8) primary key default dbo.FC_Next(),
col int
)
select * from tb
begin tran
insert into tb (col) values (1)
insert into tb (col) values (2)
insert into tb(BH,col) values (dbo.FC_Next(),14)
commit tran
select * from tb
(
flg int NOT NULL IDENTITY (1, 1),
id AS 'a'+right('000000'+convert(varchar,flg),6) PERSISTED not null PRIMARY KEY CLUSTERED ,
col1 nvarchar(50) NOT NULL
) ON [PRIMARY]
GO
(
flg int NOT NULL IDENTITY (1, 1),
id AS 'a'+right('000000'+convert(varchar,flg),6) PERSISTED not null PRIMARY KEY CLUSTERED ,
col1 nvarchar(50) NOT NULL
) ON [PRIMARY]
GO
insert into tb(col1) select 'abc' union all select 'def' union all select 'hij'
select id,col1 from tb
go
drop table tb
/*
id col1
------------- --------------------------------------------------
a000001 abc
a000002 def
a000003 hij
*/