我比较糊涂不知如何下手……表A有序号xh1 和反序号fxh字段:
现在想通过触发器实现如下效果:
修改前:
xh1 fxh
1 3
3 2
4 1
.
.
.
.
.
.修改后:
xh1 fxh
1 3
4 2
5 1
.
.
.
.
.
.
其中
1:xh1为序号按升序排序,可以不连续
2:fxh根据记录数依次为倒序,必须连续,不管修改哪条xh1都对应修改反序号
现在想通过触发器实现如下效果:
修改前:
xh1 fxh
1 3
3 2
4 1
.
.
.
.
.
.修改后:
xh1 fxh
1 3
4 2
5 1
.
.
.
.
.
.
其中
1:xh1为序号按升序排序,可以不连续
2:fxh根据记录数依次为倒序,必须连续,不管修改哪条xh1都对应修改反序号
修改的规律?等等,LZ没有说明清楚:---
:修改前按xh1排序,反序号为相反如果有5条记录那么为5.4.3.2.1,如果是6条为6.5.4.3.2.1楼主的意思是xh1可以随便更改,更改了之后就把fxh的值重新梳理一下?--对!但是xh1不可能相同那是不是删除触发器?? 不是!
for insert,update,delete
as
begin
update t set fxh=(select count(*) from A where xh1>=t.xh1) from A t
end
go
goinsert into tb
select 1,3 union
select 3,2 union
select 4,1select * from tb--如果xh1可以随便改,改大、改小。但不能重复的。
create trigger tr_tb_U on tb
for update
as
if update(xh1)
update a
set fxh=(select count(*) from tb where xh1>=a.xh1)
from tb a
/*
如果xh1的值只能改成比原值大的,那么可以对触发器中的sql再次进行优化。
因为现在是对所有的进行重新更新,担心效率。
*/