mssql sql server 限制返回结果行数的三种方法分享


摘要:
下文将讲述限时返回结果行数的方法



方法1: top 关键字

例:
select top 10 * from [表名]


方法2: 设置sql属性,只返回指定行数的数据

set rowcount 3
select * from [表名]


方法3: between关键字,只返回指定行数的数据

select * from [表名] where [列名] between value1 and value2


例:

	  create table A (keyId int, name varchar(10))
insert into A (keyId,name)values(1,'a')
insert into A (keyId,name)values(2,'b')
insert into A (keyId,name)values(3,'C')
insert into A (keyId,name)values(4,'D')
insert into A (keyId,name)values(5,'E')
insert into A (keyId,name)values(6,'F')

select top 3 keyId,name from A   
---采用top关键字返回相应信息

select keyId,name from A where keyId between 1 and 3 
---采用between关键字返回相应信息

set rowcount 3   ---设定返回行信息 如果设置0 则返回全部,否则返回指定行数据
select keyId,name from A   

go
truncate table A
drop table A
mssql_sqlserver_返回指定行数据三种方法分享

mssql_sqlserver_返回指定行数据三种方法分享