求一触发器实现TABLE中某一字段类似orcal的rownum功能?

breezeforever 2007-07-23 03:16:46
如题
...全文
189 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
breezeforever 2007-07-23
  • 打赏
  • 举报
回复
select *,(select count(1) from article_tbl a where a.art_id >= a.art_id) as num from article_tbl a
art_id title desc times CreateDate num
2 test1 test1 NULL 2007-07-19 00:00:00.000 3
10 test2 test2 NULL 2007-07-20 00:00:00.000 3
11 test3 test3 NULL 2007-07-23 00:00:00.000 3

我想要的结果是
art_id title desc times CreateDate num
2 test1 test1 NULL 2007-07-19 00:00:00.000 1
10 test2 test2 NULL 2007-07-20 00:00:00.000 2
11 test3 test3 NULL 2007-07-23 00:00:00.000 3

当删除其中一条记录时,num也自动更新:
art_id title desc times CreateDate num
2 test1 test1 NULL 2007-07-19 00:00:00.000 1

11 test3 test3 NULL 2007-07-23 00:00:00.000 2
breezeforever 2007-07-23
  • 打赏
  • 举报
回复
select *,(select count(1) from article_tbl a where a.art_id >= a.art_id) as num from article_tbl a
art_id title desc times CreateDate num
2 test1 test1 NULL 2007-07-19 00:00:00.000 2
10 test2 test2 NULL 2007-07-20 00:00:00.000 2
11 test3 test3 NULL 2007-07-23 00:00:00.000 2

我想要的结果是
art_id title desc times CreateDate num
2 test1 test1 NULL 2007-07-19 00:00:00.000 1
10 test2 test2 NULL 2007-07-20 00:00:00.000 2
11 test3 test3 NULL 2007-07-23 00:00:00.000 3

当删除其中一条记录时,num也自动更新:
art_id title desc times CreateDate num
2 test1 test1 NULL 2007-07-19 00:00:00.000 1

11 test3 test3 NULL 2007-07-23 00:00:00.000 2
ojuju10 2007-07-23
  • 打赏
  • 举报
回复
如果是 ms 2000


select *,(select count(1) from tblname where a.id>=id) as num from tblname a
gahade 2007-07-23
  • 打赏
  • 举报
回复
--在触发器中搞对性能有很大影响.一般前台软件都有这个功能吧

create trigger tr_表 on 表
for insert,delete
as
update 表
set 字段=(select count(*) from 表 b where a.id>=b.id)
from 表 a
ojuju10 2007-07-23
  • 打赏
  • 举报
回复
如果是ms 2005
row_number() over (order by col desc)

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧