同时执行两条SQL语句,如何读取数据

山野村夫 2005-12-22 03:40:39
同一个表中
sql1="select * from TableA where ontop=true"
sql2="select * from TableA where ontop=false"
set rs=conn.execute(sql1&";"&sql2)

这样只能获得第一条SQL的数据,如何才能获得全部数据呢
...全文
484 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
machao_fast 2005-12-24
  • 打赏
  • 举报
回复
select * from
(
select * from TableA where ontop=true
union
select * from TableA where ontop=false
)
order by Ontop
山野村夫 2005-12-23
  • 打赏
  • 举报
回复
可以啊
phuson 2005-12-23
  • 打赏
  • 举报
回复
热心人这么多,加满分
=========================
恭喜,顺便问楼主个问题,贴子开了之后还可以改动分数吗?
圆道 2005-12-22
  • 打赏
  • 举报
回复
哦,恭喜
山野村夫 2005-12-22
  • 打赏
  • 举报
回复
要下班了明天早上结贴,感谢各位的帮忙
山野村夫 2005-12-22
  • 打赏
  • 举报
回复
解决了,谢谢大家

代码如下:
sql="select top 3 * from TableA where Ontop=1 union all select top 7 * from TableA where Ontop=0 order by Ontop DESC,ID DESC"

order by 放到后面执行,这样一个rs就实现了需要两个rs才能实现的功能:
3条固顶文章在最前面,后面是7条非固顶文章
圆道 2005-12-22
  • 打赏
  • 举报
回复
固顶的不能这样吗,我就这样做的
sql1="select * from TableA order by ontop=true desc"这就不就固顶了吗?
sql1="select * from TableA order by ontop=true desc,这里还可以加order条件(比如时间)"
山野村夫 2005-12-22
  • 打赏
  • 举报
回复
本贴纯属技术探讨
在文章系统、BBS中涉及到固顶几个贴,后面是普通贴的情况
往往只能分成两个rs实现,如果能用一个rs实现,可以减少资源的占用
山野村夫 2005-12-22
  • 打赏
  • 举报
回复
回复人: xx123731(木头人) ( ) 信誉:100 2005-12-22 16:45:00 得分: 0


思路:
如果要选出100条数据
1.sql1="select * from TableA where Ontop=true order by ID DESC"
2.执行这条SQL语句,显示出数据.用一个变量count_num存储rs.recordcount
3.用100减去count_num
4.sql1="select top "&100-count_num&" * from TableA where Ontop=false order by ID DESC"
5.接着执行这个SQL
===========================

目前我就是使用分成几个rs的办法,不过代码比较烦
山野村夫 2005-12-22
  • 打赏
  • 举报
回复
回复人: likun_vc(www.netin4.com) ( ) 信誉:100 2005-12-22 16:36:54 得分: 0

一条就可以读出来了
sql1="select * from TableA where ontop=true or Ontop=false"
也可以试试

sql1="select * from TableA where ontop=true"
union all
sql2="select * from TableA where Ontop=false "
===============================
1、sql1="select * from TableA where ontop=true or Ontop=false"的办法不是我需要的

2、使用时union all时不能使用order by 语句




jianxuehua 2005-12-22
  • 打赏
  • 举报
回复
联合查询啦
select * from TableA where ontop=true union select * from TableA where ontop=false
jianxuehua 2005-12-22
  • 打赏
  • 举报
回复
select * from TableA where ontop=true union select * from TableA where ontop=false
xx123731 2005-12-22
  • 打赏
  • 举报
回复
思路:
如果要选出100条数据
1.sql1="select * from TableA where Ontop=true order by ID DESC"
2.执行这条SQL语句,显示出数据.用一个变量count_num存储rs.recordcount
3.用100减去count_num
4.sql1="select top "&100-count_num&" * from TableA where Ontop=false order by ID DESC"
5.接着执行这个SQL
山野村夫 2005-12-22
  • 打赏
  • 举报
回复
热心人这么多,加满分
山野村夫 2005-12-22
  • 打赏
  • 举报
回复
TO: iyaya(啥也不说了)
这样不能判断是否到了尾部了??


====================================================
Do Until rs Is Nothing
If rs.State = AdStateOpen Then ''这句报错
While Not rs.EOF
.........
Wend
End If
Set rs = rs.NextRecordset
Loop


圆道 2005-12-22
  • 打赏
  • 举报
回复
一条就可以读出来了
sql1="select * from TableA where ontop=true or Ontop=false"
也可以试试

sql1="select * from TableA where ontop=true"
union all
sql2="select * from TableA where Ontop=false "
山野村夫 2005-12-22
  • 打赏
  • 举报
回复
不一样哦:D
我把SQL语句写完整点
sql1="select top 3 * from TableA where Ontop=true order by ID DESC"
sql2="select top 7 * from TableA where Ontop=false order by ID DESC"

如果TableA中有100行,其中有10行是Ontop=true,
如果用"select top 10 * from TableA"可能一条Ontop=true的行都读不出来,
因为排序时一般用时间和ID排序,这10行Ontop=true的可能排在最底下
iyaya 2005-12-22
  • 打赏
  • 举报
回复
谢谢,如果有N条SQL语句,如何才能判断有多少个rs呢?
====================================================
Do Until rs Is Nothing
If rs.State = AdStateOpen Then
While Not rs.EOF
.........
Wend
End If
Set rs = rs.NextRecordset
Loop
phuson 2005-12-22
  • 打赏
  • 举报
回复
不太明白,sql1和sql2合起来不就是:select * from TableA  吗?
山野村夫 2005-12-22
  • 打赏
  • 举报
回复
回复人: Fanxr(ZhangFan) ( ) 信誉:99 2005-12-22 15:56:00 得分: 0


先调用一个RS,会使用第一个SQL语句的结果集,在完成之后使用

Set Rs=Rs.NextRecordSet就好了,这时Rs就是第二个结果集了,状态是bof
========================================

谢谢,如果有N条SQL语句,如何才能判断有多少个rs呢?
加载更多回复(5)

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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