易截截图软件、单文件、免安装、纯绿色、仅160KB

SQL Select N to M Records (single Table)

取表里n到m条纪录的几种方法:
1. 只需要查询前M条数据(0 to M),
1.1 使用 top(M) 方法:
select top(3) * from [tablename]
 
1.2 使用 set rowcount 方法:
http://msdn.microsoft.com/zh-cn/library/ms188774(SQL.90).aspx
set rowcount M
select * from [tablename]
set rowcount 0
权限 要求具有 public 角色成员资格。
要执行set rowcount 0, 否则影响以后查询等.
 
 
2.查询N到M条数据(N to M),
2.1  表里面有标识列
2.1.1
select top (M-N+1) * from [tablename] where [columnname] not in (select top (N) [columnname] from [tablename])
 
2.1.2  逆序显示
select top N * from (select top M * from [tablename] order by [columnname]) temp order by [columnname] desc
 
2.1.3 顺序显示
select * from (select top N * from (select top M * from [tablename] order by [columnname]) temp1 order by [columnname] desc) temp2 order by [columnname]
 
 
2.2 表里有identity属性
select * from [tablename] where identitycol between N and M
 
如[columnname]为identity属性,则可以写成:
select * from [tablename] where [columnname] between N and M
 
2.3 表里面有标识列, 利用临时表
IF Exists(Select 1 from sysObjects Where Name ='temptable' And Type In ('temptable','U'))
begin
    drop table [temptable]
end
select top M * into [temptable] from [tablename] order by [columnname]
set rowcount N
select * from [temptable] order by [columnname] desc
set rowcount 0
drop table [temptable] 
 
2.4 如果tablename里没有其他identity列,那么:
exec sp_dboption [DataBaseName] ,'select into/bulkcopy',true
IF Exists(Select 1 from sysObjects Where Name ='temptable' And Type In ('temptable','U'))
begin
    drop table temptable
end
select identity(int) id0,* into [temptable] from [tablename]
select * from temp where id0 >= N and id0 <= M
drop table temptable 
 
如果你在执行select identity(int) id0,* into [temp


相关文档:

mysql 一条sql语句update更新两个表

你写过一条sql语句来修改两个表的数据吗?
UPDATE test.table1 t1,test.table2 t2 SET t1.aa='a',t1.bb='b',t2.cc='c',WHERE t1.u_id=t2.u_id AND t1.u_id='1'  ;
table1的u_id和table2的u_id是主外键关系 ......

SQL递归查询数据

Tree表如下:  
    NodeId   ParentId     NodeName  
    0           -1              &nb ......

SQL 面试题一

1.用一条Sql语句查询出每门功课都大于80分的学生姓名 
Name
Class
Result
张三
语文
56
张三
数学
88
李四
语文
92
李四
数学
100
王五
语文
88
王五
数学
99
王五
英语
99
-------------------------------------------------------------------------------
答案:
SELECT DISTINCT nam ......

没有SQL Server Management Studio

本文主要内容属转载,但笔者根据该文内容测试成功,故分享于此。
笔者实验环境:Windows Server 2003 Enterprise Edition。
先给出原文链接,稍后调整。
原文链接为:http://www.shilai.cn/2007/5/6/problems-of-installing-sql2005.aspx ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号