可直接在sql中在表里面写触发器,如
CREATE trigger [dbo].[insert_A]
on [dbo].[T_ls]
for insert
as
declare @id int
declare @num varchar(10)
select @id=id from inserted
IF exists( select 1 from T_LS where SUBSTRING(序号,3,8)=convert(varchar(8),getdate(),112))
begin
select @num= RIGHT('00000000'+CAST(max(right(序号,4)+1) as varchar(10)),4) from T_LS where SUBSTRING(序号,3,8)=convert(varchar(8),getdate(),112) GROUP BY 序号
update T_ls set 序号 ='XN'+convert(varchar(8),getdate(),112)+@num where id=@id
end
else
begin
set @num='0001'
update T_LS set 序号 ='XN'+convert(varchar(8),getdate(),112)+@num where id=@id
end
GO
ALTER TABLE [dbo].[T_ls] ENABLE TRIGGER [insert_A]
GO