使用 CTE 代替光标
我有以下表结构。
我只想将 SubId 更新为 null 且 RawLineNumber 升序 1 且 SeqNumber 升序 1 的所有行
RawlineNumber Claimid SubId SeqNumber
1 6000 A100 1
2 6000 NULL 2
3 6000 NULL 3
10 6000 A200 1
11 6000 NULL 2
25 6000 A300 1
26 6000 NULL 2
27 6000 NULL 3
我要更新 RawLineNumber 2 和 3 的 SubId 与 A100,
SubId of RawLineNumber 11,A200,
A300 的 RawLineNumber 26 和 27 的 SubId。
我有一个游标可以完成这项工作,但是我可以有一个 CTE 来处理它吗?
I have the following table structure.
I just want to update SubId to all the rows where it is null and where the RawLineNumber is ascending by 1 and also the SeqNumber ascending by 1.
RawlineNumber Claimid SubId SeqNumber
1 6000 A100 1
2 6000 NULL 2
3 6000 NULL 3
10 6000 A200 1
11 6000 NULL 2
25 6000 A300 1
26 6000 NULL 2
27 6000 NULL 3
I want to update
SubId of RawLineNumber 2 and 3 with A100,
SubId of RawLineNumber 11 with A200,
SubId of RawLineNumber 26 and 27 with A300.
I have a cursor which does the job but can I have a CTE to take care of it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于需要一种隐性解决方案,我决定编写一个。它还适用于 Seqnumbers 和 RawlineNumber 中的间隙
Since a recusive solution was requested, I decided to write one. Also it works for gaps in Seqnumbers and RawlineNumber
一个不那么简单的 SQL 脚本应该可以实现您想要的:
内部子选择(T3)为我们提供了当前行之前 seqnumber = 1 的最后一行,
外部子选择为我们提供了该行的 SubID(使用窗口函数会更有效,但由于您没有提到特定的 RDBMS,所以我坚持这一点:-))
A not-so simple SQL script should achieve what you want:
The inner subselect (T3) gives us the last row having seqnumber = 1 before the current line,
the outer subselect gives us the SubID for this row (using windowing functions would be more efficient, but since you didn't mention a specific RDBMS, I stick with this :-) )