SQL语句:select replace(substring(convert(varchar(20),getdate(),120),1,10),'-','')+cast(MAX(订单号)+1 as varchar(4))
使用存储过程
--创建测试表
create table usertable(userid varchar(20),username nvarchar(20))
go
--创建触发器
create trigger tg_insert on usertable
for insert
as
declare @username nvarchar(20)
declare @userid varchar(20)
declare @num int
select @userid=max(userid) from usertable where userid like 'jzxd' + substring(convert(varchar(10),getdate(),112),5,4) + '%'
if @userid is null
set @userid='jzxd' + substring(convert(varchar(10),getdate(),112),5,4) + '-01'
else
begin
set @num=cast(substring(@userid,10,2) as int)
set @num=@num + 1
if @num