ADO循环语句怎么转成sql语句

浮躁88 2012-02-15 09:23:26

RxRecordset rst;
sql="select * from A";
rst.Open(sql);
while(!rst.end())
{
int Id=atoi(rst.GetFieldValue("Id"));
RxRecordset rs;
sql.Format("select top 100 * from %s where Id >= %d and Id<%d",B,Id*100,Id*100+99);
rs.Open(sql);
while(!rs.isend())
{
rs.MoveNext();
}
rst.MoveNext();
}
...全文
54 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
三断笛 2012-02-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xxyj6450 的回复:]

引用 4 楼 qianjin036a 的回复:

引用 3 楼 xxyj6450 的回复:

C/C++ code

RxRecordset rst;
sql="select b.* from B as cross apply(select top 100 * from A where b.Id >= a.id*100 and Id<a.id*100+99) as x";
……
[/Quote]

改错地方了。。。


RxRecordset rst;
sql.format("select top 100 * from %s as b cross apply (select top 100 * from A where b.Id >= a.id*100 and Id<a.id*100+99) as x",B);
rst.Open(sql);
while(!rst.isend())
{
rst.MoveNext();
}
三断笛 2012-02-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 qianjin036a 的回复:]

引用 3 楼 xxyj6450 的回复:

C/C++ code

RxRecordset rst;
sql="select b.* from B as cross apply(select top 100 * from A where b.Id >= a.id*100 and Id<a.id*100+99) as x";
rst.Open(sql);
while(!rst.isend())
……
[/Quote]
你说的是,我没仔细理解楼主的代码。

那可以这样:


RxRecordset rst;
sql.format("select top 100 * from %s cross apply (select top 100 where exists(select 1 from A where b.Id >= a.id*100 and Id<a.id*100+99)",B);
rst.Open(sql);
while(!rst.isend())
{
rst.MoveNext();
}

-晴天 2012-02-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xxyj6450 的回复:]

C/C++ code

RxRecordset rst;
sql="select top 100 * from B where exists(select 1 from A where b.Id >= a.id*100 and Id<a.id*100+99)";
rst.Open(sql);
while(!rst.isend())
{
……
[/Quote]

应该不是这样的需求.

楼主程序的意思是先获得一个RST,然后根据这个RST的各个不同值,分别获得另外表中的100条记录,而且表名也是由变量引入的.

如果是这个意思的话,那只能根据原来的程序来实现,因为,你的 B 是否为常量字串未知(如果为常量字串,没必要用 %s).

另外,这以一百条记录为一组的多数组数据是如何使用的,在楼主程序中并未体现,这也与如何改SQL语句有很大关系.
三断笛 2012-02-15
  • 打赏
  • 举报
回复

RxRecordset rst;
sql="select top 100 * from B where exists(select 1 from A where b.Id >= a.id*100 and Id<a.id*100+99)";
rst.Open(sql);
while(!rst.isend())
{
rst.MoveNext();
}
叶子 2012-02-15
  • 打赏
  • 举报
回复
sql中的循环可以用while 数据的循环可以用游标。
  • 打赏
  • 举报
回复
不懂,去C++板块问问,那边应该有人会。

不然就等大牛来吧

34,593

社区成员

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

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