请教Insert触发器问题
有这样的一个表aaa
ID OrderID AddTime
1 20070420001 2007-4-20
2 20070420002 2007-4-20
3 20070420003 2007-4-20
4 20070421001 2007-4-21
5 20070421002 2007-4-21
问:新增一条记录ID=6、AddTime=2007-4-21,要求OrderID自动变成20070421003
这个触发器怎样写啊?
------解决方案--------------------create trigger a on aaa after update as
declare @b int
select @b=max(cast(right(OrderID,3),int) from aaa where AddTime= '2007-4-21 '
update aaa set OrderID=replace(AddTime, '- ', ' ')+ @b
如是datetime类型 请用上面的日期部分
------解决方案--------------------create trigger test_tr on dbo.ta
after insert
as
if (select count(1) from inserted)> 1
begin
raiserror 50001 '禁止后台多记录插入 '
rollback tran
return
end
declare @i char(3)
select @i=right(1000+rtrim(isnull(max(right(a.orderid,3)),0)+1),3)
from ta a join inserted i on convert(varchar(8),a.AddTime,112)=convert(varchar(8),i.AddTime,112)
update ta
set orderid=convert(varchar(8),i.AddTime,112)+@i
from inserted i join ta on i.id=ta.id
以上支持一条一条插入