- UID
- 1415
- 主题
新手上路
- 金钱
-
- 威望
-
- 贡献
-
- 日志
- 好友
- 帖子
- 主题
- 听众
- 收听
- 性别
- 保密
|
2005) 8 l# w2 \" b B& g
CREATE PROCEDURE [dbo].[GetUsers] --得到用户列表,注意这里的参数 / |& n8 m) H% }9 ?2 l( d5 }# |& ?
@RowIndex int,
3 \% u) Z" S( h8 C# i0 y( A @RecordCount int 1 u2 J' E! r2 g& r8 Q0 ^. U
AS
6 K! {( U7 m# L8 t BEGIN
* ]2 Q5 F, \& d- P; | SET NOCOUNT ON;
_% g6 n2 S( x6 u3 f. D1 \0 r0 ^ With VUsers as ( 9 E& t+ b' W: e6 \' H# E
select *,row_number() over (order at UserID desc) for RowNum
3 p! }$ L* x) V from MyUsers
" S( d$ s- p- \/ p )
, I& }7 Y; i/ Q' ~7 t select * from VUsers ! h' A" C6 {( B/ X1 a% E5 k2 V# L, \& R
where RowNum > @RowIndex and RowNum <= (@RowIndex+@RecordCount) 2 G0 P: N& ~+ K/ E9 N
END
' y: B6 z- X7 }- c' x 2. 分页方案一:(利用Not In和SELECT TOP分页) & G `# E' X, O; a/ |1 _
语句形式:
3 l7 ^/ G1 g& }* b SELECT TOP 10 *
9 Q, n% [/ a- `+ T- j( b FROM TestTable
1 m, Z) R6 f3 u# \; v% ^+ T WHERE (ID NOT IN $ }7 R T6 U h: F
(SELECT TOP 20 id : A1 g& _6 E8 f0 q
FROM TestTable
5 u0 t. ~" B& M; k' l- v& T) s6 w' L: D4 } ORDER BY id))
1 z5 v+ M. `0 i, _3 N5 x5 x$ J) n ORDER BY ID
9 J1 Q, y" X( U* a SELECT TOP 页大小 * 0 J5 L* A; W" \! ?1 R
FROM TestTable
- t2 d4 S* a- |) L$ C WHERE (ID NOT IN 0 c4 a6 x1 u" ^# G# f
(SELECT TOP 页大小*页数 id ! O+ B4 I1 M3 K3 e3 H2 _6 j' Q) a
FROM 表 ' ~- w4 H! C- b+ f% u, S) _8 g+ p
ORDER BY id))
. X% ]: |8 j! v Q1 D ORDER BY ID
r6 M- F; y" E* I 3.分页方案二:(利用ID大于多少和SELECT TOP分页)
; ~9 E. a0 c, H0 \# d5 ?! J3 h 语句形式: $ u; C; U" w* D g+ t
SELECT TOP 10 * 5 U) ?3 w0 Y/ L& U7 W
FROM TestTable + k$ S( d& d3 i4 d' b: o! {4 d
WHERE (ID > 5 m, J ]& p( ]! U! A6 m" e
(SELECT MAX(id) ( o) n8 C( }% v# R; ~, o6 W9 d
FROM (SELECT TOP 20 id
/ W5 S1 p& K) }5 L6 Q. H8 Q FROM TestTable & d9 \" z9 v3 r
ORDER BY id) AS T))
. I. T/ g. S4 ]( {$ |' J& M ORDER BY ID # p8 Y4 ~9 e8 r$ t
SELECT TOP 页大小 * & q" q7 n; d/ W9 A% {' I
FROM TestTable . T& `. i" e% t
WHERE (ID >
' f" w# L# L1 P( U7 p, u( q! j8 v (SELECT MAX(id)
( S0 Y. w- u- { FROM (SELECT TOP 页大小*页数 id % _. J% }: ?, _ `. Y' y
FROM 表
5 `5 F7 Y4 g& N, W ORDER BY id) AS T)) A! j7 n, g6 y+ N4 s4 R- i
ORDER BY ID 0 h1 W" D* c P2 W2 R0 ^8 F
4.分页方案三:(利用SQL的游标存储过程分页)
9 R- S! s" v) g8 c8 T: M create procedure XiaoZhengGe
# A1 F) a" |$ k @sqlstr nvarchar(4000),瓷球, --查询字符串
5 ^8 n5 m& C& {4 B0 W) | @currentpage integer, --第N页
; O) Z0 G) W( C( ~+ F- ? @pagesize int --每页行数 2 Z. U% C( j. E8 `2 ^8 s: K+ }1 R
as # H) x% l% o, V# f7 N u) u
set nocount on
" e a1 \5 ^1 {% U' I declare @P1 int, --P1是游标的id ' P" T ]0 X2 l: _0 l) R' w
@rowcount int 4 i/ A* [9 d3 [
exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@ro wcount output 3 ?/ v; r4 ^. ]( ^# {( U% ]
select ceiling(1.0*@rowcount/@pagesize) as 总页数--,@rowcount as 总行数,搅拌站,@currentpage as 当前页 * y( g2 U/ P% H; Q, k
set @currentpage=(@currentpage-1)*@pagesize+1
8 \$ c9 q" ~6 B exec sp_cursorfetch @P1,16,@currentpage,@pagesize " e. ]4 ^( H4 A9 O
exec sp_cursorclose @P1
% v. w0 L5 }# M" k7 Q+ g/ Q: E set nocount off
6 I7 f9 q$ @9 i4 W, @ 其它的方案:如果没有主键,可以用临时表,也可以用方案三做,填料,但是效率会低。 : y" A3 I& p& u' K1 H, `
建议优化的时候,加上主键和索引,上海婚纱摄影,查询效率会提高。 ! e& `% y; K6 d7 u# M5 i/ K; N
通过SQL 查询分析器,变压器,显示比较:我的结论是: ) K- |- X0 G4 }4 k' w
分页方案二:(利用ID大于多少和SELECT TOP分页)效率最高,碳分子筛,需要拼接SQL语句 , W, x% q1 e1 M$ V
分页方案一:(利用Not In和SELECT TOP分页) 效率次之,需要拼接SQL语句
$ D. c1 p I9 d+ r 分页方案三:(利用SQL的游标存储过程分页) 效率最差,但是最为通用" m( m6 y2 [( n1 s G% ]( U3 K& N
relative:9 x( e& h; |9 ]0 P' F7 c
( k- `2 z9 G% }% N1 U) w , j: e; O/ _7 W
behind the excavation process' }. z3 P4 }8 Y* j( k
( Q* t! o! Z# Q, c
Adoption of the Random
0 r; m/ K' Y. Q8 Q
/ S; s* `, |& V* `( \. B9 r2 e+ y We tin alarm !" X3 o4 W" j9 Q$ f0 m& ~
& c, R+ Q3 N- d# y, A
China's satellite pictures tin be seen the class o3 I3 \4 _: r6 ~: r* e( c
6 Q. ^7 ^2 [+ ?+ p; J6 \ Copyright statement |
|