sql - SQL 仅选择单行号 x
问题描述
为了在 VBA 中处理 SQL 数据,我想使用从 SQL 数据库中逐行读取的循环(For...Next)。由于记录被删除,ID 列有间隙。例如,该表如下所示:
ID Value
1 Peter
3 Paul
4 Mary
9 George
有四行我想跑
For i = 1 to 4
SELECT ???
Next i
当然,“For each”也会有好处。
解决方案
如果您想要 4 行,请使用order by
and top
- 或 a fetch clause
:
select top(4) t.*
from mytable t
order by id
或者:
select t.*
from mytable t
order by id
offset 0 rows fetch first 4 rows only
然后,您可以遍历应用程序中的结果集。
另一方面,如果您只想要第 4 行,则只需更改fetch
子句:
select t.*
from mytable t
order by id
offset 3 rows fetch next 1 row only
推荐阅读
- objective-c - 我应该在 NSPopover 的 showRelativeToRect:ofView:preferredEdge: 方法的 ofView: 参数中传递什么值?
- postgresql - 使用 pgadmin 4 读取 postgresql 8.4 数据库时出错
- angular - 如何使类可观察?
- mysql - 如何解决大量数据的 IN 子句 SQL 查询中的性能问题?
- vba - 增加形状之间的垂直间距
- java - 如何将 Uri 列表中的图像上传到 Google Drive?
- reactjs - 使用 redux react 和 feathersJS 实现新闻提要功能
- tumblr - Tumblr - 使用 1x1 Image html 标签触发端点
- c++ - cmath 导入问题 Mac OS Catalina 和 PCL
- r - 与索引一起使用时cbind与变量名有什么区别