CREATE trigger [dbo].[Debt_Insert] on [dbo].[Debt]
for insert
as
declare @tmpOrderID1 varchar(30)
select @tmpOrderID1= OrderID from debt where OrderID like '%JK'+Convert(varchar(4),year(getdate()))+'%' order by ID
if(@tmpOrderID1 is null or ltrim(rtrim(@tmpOrderID1))='')
begin
set @tmpOrderID1='JK'+Convert(varchar(4),year(getdate()))+'0001'
end
else
begin
set @tmpOrderID1=right(@tmpOrderID1,4)
set @tmpOrderID1='JK'+Convert(varchar(4),year(getdate()))+right('0000'+convert(varchar(4),@tmpOrderID1+1),4)
end
update Debt set OrderID=@tmpOrderID1 from Debt inner join inserted on Debt.ID=Inserted.ID
转载于:https://www.cnblogs.com/tkaren/p/3769811.html