-- 别怪 MS ,连总数一起查出来
create table test(id int identity, xtype varchar(10),oid int, oname varchar(20))
go
insert into test(xtype,oid,oname) select top 100 xtype,id,left(name,20) from sysobjects
go
insert into test(xtype,oid,oname) select xtype,oid,oname from test
go 5
select COUNT(*) as '这里是总数'from test where xtype ='U'
go
print '这里是结果集'
go
with m as (
select id, xtype, oid, oname ,
count(*) over() rscount ,
row_number() over(order by id) rn
from test
where xtype = 'U'
)
select * from m where rn between 101 and 120
go
drop table test
go
这里是总数
-----------
352
(1 行受影响)
这里是结果集
id xtype oid oname rscount rn
----------- ---------- ----------- -------------------- ----------- --------------------
885 U 98099390 peremployee 352 101
895 U 594101157 class 352 102
897 U 626101271 course 352 103
899 U 658101385 departments 352 104
901 U 690101499 message 352 105
903 U 711673583 test 352 106
904 U 722101613 sel_cou 352 107
906 U 754101727 student 352 108
908 U 786101841 teach 352 109
910 U 818101955 teacher 352 110
977 U 62623266 sysdiagrams 352 111
978 U 98099390 peremployee 352 112
988 U 594101157 class 352 113
990 U 626101271 course 352 114
992 U 658101385 departments 352 115
994 U 690101499 message 352 116
996 U 711673583 test 352 117
997 U 722101613 sel_cou 352 118
999 U 754101727 student 352 119
1001 U 786101841 teach 352 120
[code=sql]select *,row_number() rk from tb a join xxx on xxx where xxx
--这里可是一个复杂的查询.
--最后@@rowcount获取总数
#11
其实是有总记录的数的.
select *,row_number() rk from tb
set @totalrows=@@Rowcount--这个就是总记录数
关键不会只是这样的求出结果。
[code=sql]select *,row_number() rk from tb a join xxx on xxx where xxx
--这里可是一个复杂的查询.
--最后@@rowcount获取总数
select *,row_number() rk from XX
分页查询不是返回全部结果,要么分页,外层还有个条件如: where rk between 21 and 40 。
如果只是求count,也没必要这么查了
#12
-- 别怪 MS ,连总数一起查出来
create table test(id int identity, xtype varchar(10),oid int, oname varchar(20))
go
insert into test(xtype,oid,oname) select top 100 xtype,id,left(name,20) from sysobjects
go
insert into test(xtype,oid,oname) select xtype,oid,oname from test
go 5
select COUNT(*) as '这里是总数'from test where xtype ='U'
go
print '这里是结果集'
go
with m as (
select id, xtype, oid, oname ,
count(*) over() rscount ,
row_number() over(order by id) rn
from test
where xtype = 'U'
)
select * from m where rn between 101 and 120
go
drop table test
go
这里是总数
-----------
352
(1 行受影响)
这里是结果集
id xtype oid oname rscount rn
----------- ---------- ----------- -------------------- ----------- --------------------
885 U 98099390 peremployee 352 101
895 U 594101157 class 352 102
897 U 626101271 course 352 103
899 U 658101385 departments 352 104
901 U 690101499 message 352 105
903 U 711673583 test 352 106
904 U 722101613 sel_cou 352 107
906 U 754101727 student 352 108
908 U 786101841 teach 352 109
910 U 818101955 teacher 352 110
977 U 62623266 sysdiagrams 352 111
978 U 98099390 peremployee 352 112
988 U 594101157 class 352 113
990 U 626101271 course 352 114
992 U 658101385 departments 352 115
994 U 690101499 message 352 116
996 U 711673583 test 352 117
997 U 722101613 sel_cou 352 118
999 U 754101727 student 352 119
1001 U 786101841 teach 352 120
with list as(
SELECT ROW_NUMBER() OVER(order by order by ) AS num,*
FROM 表
)
select (select COUNT(1) from list) as Count,* from list
where num between 1 and 50
这是我现在用的 每行第一列count就是总条数
#18
with list as(
SELECT ROW_NUMBER() OVER(order by order by ) AS num,*
FROM 表
)
select (select COUNT(1) from list) as Count,* from list
where num between 1 and 50
这是我现在用的 每行第一列count就是总条数
其实也是跟我这一样的。
求总记录一个语句:
select COUNT(1) from list
分页一个语句:
with list as(
SELECT ROW_NUMBER() OVER(order by order by ) AS num,*
FROM 表
)
select * from list
where num between 1 and 50
-- 别怪 MS ,连总数一起查出来
create table test(id int identity, xtype varchar(10),oid int, oname varchar(20))
go
insert into test(xtype,oid,oname) select top 100 xtype,id,left(name,20) from sysobjects
go
insert into test(xtype,oid,oname) select xtype,oid,oname from test
go 5
select COUNT(*) as '这里是总数'from test where xtype ='U'
go
print '这里是结果集'
go
with m as (
select id, xtype, oid, oname ,
count(*) over() rscount ,
row_number() over(order by id) rn
from test
where xtype = 'U'
)
select * from m where rn between 101 and 120
go
drop table test
go
这里是总数
-----------
352
(1 行受影响)
这里是结果集
id xtype oid oname rscount rn
----------- ---------- ----------- -------------------- ----------- --------------------
885 U 98099390 peremployee 352 101
895 U 594101157 class 352 102
897 U 626101271 course 352 103
899 U 658101385 departments 352 104
901 U 690101499 message 352 105
903 U 711673583 test 352 106
904 U 722101613 sel_cou 352 107
906 U 754101727 student 352 108
908 U 786101841 teach 352 109
910 U 818101955 teacher 352 110
977 U 62623266 sysdiagrams 352 111
978 U 98099390 peremployee 352 112
988 U 594101157 class 352 113
990 U 626101271 course 352 114
992 U 658101385 departments 352 115
994 U 690101499 message 352 116
996 U 711673583 test 352 117
997 U 722101613 sel_cou 352 118
999 U 754101727 student 352 119
1001 U 786101841 teach 352 120
select *,row_number() rk from tb
set @totalrows=@@Rowcount--这个就是总记录数
关键不会只是这样的求出结果。
[code=sql]select *,row_number() rk from tb a join xxx on xxx where xxx
--这里可是一个复杂的查询.
--最后@@rowcount获取总数
#11
其实是有总记录的数的.
select *,row_number() rk from tb
set @totalrows=@@Rowcount--这个就是总记录数
关键不会只是这样的求出结果。
[code=sql]select *,row_number() rk from tb a join xxx on xxx where xxx
--这里可是一个复杂的查询.
--最后@@rowcount获取总数
select *,row_number() rk from XX
分页查询不是返回全部结果,要么分页,外层还有个条件如: where rk between 21 and 40 。
如果只是求count,也没必要这么查了
#12
-- 别怪 MS ,连总数一起查出来
create table test(id int identity, xtype varchar(10),oid int, oname varchar(20))
go
insert into test(xtype,oid,oname) select top 100 xtype,id,left(name,20) from sysobjects
go
insert into test(xtype,oid,oname) select xtype,oid,oname from test
go 5
select COUNT(*) as '这里是总数'from test where xtype ='U'
go
print '这里是结果集'
go
with m as (
select id, xtype, oid, oname ,
count(*) over() rscount ,
row_number() over(order by id) rn
from test
where xtype = 'U'
)
select * from m where rn between 101 and 120
go
drop table test
go
这里是总数
-----------
352
(1 行受影响)
这里是结果集
id xtype oid oname rscount rn
----------- ---------- ----------- -------------------- ----------- --------------------
885 U 98099390 peremployee 352 101
895 U 594101157 class 352 102
897 U 626101271 course 352 103
899 U 658101385 departments 352 104
901 U 690101499 message 352 105
903 U 711673583 test 352 106
904 U 722101613 sel_cou 352 107
906 U 754101727 student 352 108
908 U 786101841 teach 352 109
910 U 818101955 teacher 352 110
977 U 62623266 sysdiagrams 352 111
978 U 98099390 peremployee 352 112
988 U 594101157 class 352 113
990 U 626101271 course 352 114
992 U 658101385 departments 352 115
994 U 690101499 message 352 116
996 U 711673583 test 352 117
997 U 722101613 sel_cou 352 118
999 U 754101727 student 352 119
1001 U 786101841 teach 352 120
with list as(
SELECT ROW_NUMBER() OVER(order by order by ) AS num,*
FROM 表
)
select (select COUNT(1) from list) as Count,* from list
where num between 1 and 50
这是我现在用的 每行第一列count就是总条数
#18
with list as(
SELECT ROW_NUMBER() OVER(order by order by ) AS num,*
FROM 表
)
select (select COUNT(1) from list) as Count,* from list
where num between 1 and 50
这是我现在用的 每行第一列count就是总条数
其实也是跟我这一样的。
求总记录一个语句:
select COUNT(1) from list
分页一个语句:
with list as(
SELECT ROW_NUMBER() OVER(order by order by ) AS num,*
FROM 表
)
select * from list
where num between 1 and 50