SQL SERVER根据当前年月日+5位流水号创建流水号

it2024-04-03  52

创建一个test表

create table test(id varchar(15), --流水号,日期(8)+流水号(5) name varchar(10) --其他字段 )

创建一个触发器

create trigger t_insert on test INSTEAD OF insert as declare @id varchar(15),@id1 int,@head varchar(8) select * into #tb from inserted set @head=convert(varchar,getdate(),112) select @id=max(id) from test where id like @head+'%' if @id is null set @id1=0 else set @id1=cast(substring(@id,9,5) as int) update #tb set @id1=@id1+1 ,id=@head+right('00000'+cast(@id1 as varchar),5) insert into test select * from #tb

添加三条数据

insert into test(name) select 'aa' union all select 'bb' union all select 'cc'

在数据库中查询 在VS中只需复制添加代码即可完成添加操作(可完成日志操作)流水号会根据当天添加记录,到第二天流水号会从1开始记录

最新回复(0)