TYPE ID 1 10001 2 20001 3 30001 1 10002 2 20002 2 20003 怎么得来的?看不出规律啊!
type 是 1、2、3、4或者 A\B\C\D ID 要处理为 10001 A0001 A0002等等
create trigger trig_tb_insert on tb for insert as update tb set id=a.type+right('000'+ltrim(isnull((select max(right(ID,4)) from tb where left(id,1)=a.type),0)+1),4) from inserted a where tb.主键=a.主键 go --然后,你向表里插数据看看,当然不包括inserted列,会自动生成的。
1 10001
2 20001
3 30001
1 10002
2 20002
2 20003
怎么得来的?看不出规律啊!
ID 要处理为 10001
A0001
A0002等等
for insert
as
update tb set id=a.type+right('000'+ltrim(isnull((select max(right(ID,4)) from tb where left(id,1)=a.type),0)+1),4)
from inserted a where tb.主键=a.主键
go
--然后,你向表里插数据看看,当然不包括inserted列,会自动生成的。