【发布时间】:2013-11-25 04:54:01
【问题描述】:
我有两个表“主要”和“移动”。 “主”表有记录,我通过执行存储过程将 3 行顺序移动到“移动”表。因此,每次我执行存储过程时,它应该检查下一组 3 行是否从最后一次移动发生的行中的“主”表中顺序读取并插入到“移动”表中。
select rowid from @main
rowid
-----------------------
1
2
3
4
5
现在当我执行查询时,它应该从“主”表中取出 3 行并插入到“移动”表中。假设我运行查询 5 次,这就是我希望每次运行时“已移动”表包含的方式。
1,2,3 --"moved" table has these rows 1st time when I run the query
4,5,1 --"moved" table has these rows 2nd time when I run the query
2,3,4 --"moved" table has these rows 3rd time when I run the query
5,1,2 --"moved" table has these rows 4th time when I run the query
3,4,5 --"moved" table has these rows 5th time when I run the query
1,2,3 --"moved" table has these rows 6th time when I run the query
...所以顺序读取继续。从上次运行结束的下一行开始按顺序读取。
我已经问过了,但答案只是部分有效。当“主”表中的值按顺序增长或不按顺序增长时,它不起作用。
提前感谢您的帮助。
【问题讨论】:
-
没有
ORDER BY子句就没有顺序,sequential 没有意义。您想使用哪一列来指定从@main中获取的值的顺序? (似乎只有一列,您不喜欢它提供的结果。) -
“Main”表是基表,只有一列“RowID”。而“已移动”表是也只有一列“RowID”的目标表。我正在尝试从上到下读取行,并在到达表末尾时再次从顶部开始 - 递归。只要我得到上面给定的输出,查询就可以是任何东西。主表只有“RowID”列。
标签: sql-server-2008-r2 sql-server-2012 common-table-expression