当前位置:数据库 > SQLServer >>

mssql 多表 第几行到第几行 查询

比如要查询 表 order 下的 order_sn 和 表 goods 下的 goods_name 查询全部的话 SELECT goods_name,order_sn FROM goods,orde where goods.order_id=order.order_id 这样就可以 如果查 第100行到200行,这样的mssql 要怎么写 关键是联合表查询比较麻烦
补充:不好意思写错了
应该是
SELECT Top * goods_name,order_sn FROM goods,orde where goods.order_id=order.order_id
答案:SELECT Top * goods_name,order_sn FROM goods,orde where id>(select TOP 100 id from goods) and id<(select TOP 200 id from goods) and goods.order_id=order.order_id
其他:1假如order_id是主键的话,select top(200) goos_name,order_sn from goods,order where goods.order_id=order.order_id
1假如两表中都没有主键的话,那么你可以使用游标
declare mycursor scroll cursor --定义一个滚动游标,mycursor是游标名字
for select top(200) goods_name,order_sn  from goods,order where goods.order_id=order.order_id --表示游标的范围在这个select语句之间
open mycursor--打开游标
fetch absolute 100 from mycursor--游标移动到第100行
while @@fetch_status=0--值为0表示执行成功,为-1表示执行失败,为-2表示提取的行不存在
fetch next from mycursor--移动到下一行
close mycursor--关闭游标
deallocate mycursor--释放游标 

上一个:安装MSSQL安装程序配置服务器失败,参考服务器错误日志和C:\WINDOWS\sqlstp.log了解更多
下一个:中国人用mssql的多吗

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,