5 t-sql sql-server sql-server-2012
我已经创建了一个项目历史表,我想自动增加VersionId分区ItemId.
ID ItemId VersionId ItemContent Created 1 1 1 fooo 2015-02-24 12:54:00.11 2 2 1 barr 2015-02-24 12:54:15.35 3 1 2 foo 2015-02-24 12:55:00.61 4 1 3 baz 2015-02-24 12:55:45.23 5 2 2 bar 2015-02-24 12:56:00.03
目前,这VersionId是在触发器中设置的.我想要找到的是这样的(我知道这实际上不起作用):
create table ItemHistory
(
Id int identity(1,1) not null primary key
,ItemId int not null references Item(Id)
,VersionId int not null default row_number() over (partition by ItemId order by Id)
,ItemContent varchar(max)
,Created datetime not null default getdate()
)Run Code Online (Sandbox Code Playgroud)
虽然我有一个可行的解决方案,但我想知道我是否缺少一些用于处理此案例的内置功能.
如果您想使用我的评论作为答案
为什么不直接做一个视图呢?您可以在视图中使用 row_number() 。
通过 ItemId、Id 上的索引,应该会非常快。
可能比触发器更少的开销。
我看到您添加了一条只读评论。更有理由观看。
| 归档时间: |
|
| 查看次数: |
357 次 |
| 最近记录: |