登录  | 立即注册

游客您好!登录后享受更多精彩

查看: 1060|回复: 0

[js,sql,html教程] 【SQL教程】sql Sever 创建分页储存过程及直接查询

[复制链接]

444

主题

509

帖子

2051

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2051

荣誉管理论坛元老

发表于 2021-6-30 23:12:25 来自手机 | 显示全部楼层 |阅读模式 来自:
  1. 5 l& t# ?# z) e
  2. --创建存储过程
      [3 ^" g1 ]& y( O. W/ f
  3. if exists(select * from sysobjects where name='P_GetStudentInfoPage')
    9 O7 [% n1 G; a3 }- A( J8 c* L% ]
  4. drop proc P_GetStudentInfoPage6 z; p9 I% G$ d
  5. go6 f# d$ [/ m" z6 n! L
  6. create proc [dbo].P_GetStudentInfoPage' k6 u7 K- [7 p" m, a6 M, c! v8 u8 P
  7. (
    # L  f+ `$ t) ?
  8.         @pageIndex int=1,--页号
    ; a: N& F2 R( s  ?6 M
  9.         @pageSize int=10--每页数据条数
    $ x) @/ ]( }9 J) j
  10. )
    4 O* m% I2 Z8 Q5 [! I/ K
  11. as: y' b" P6 k, A" c& ]/ L
  12.         --select count(*) from StudentInfo  --查询数据总数3 [1 w% V9 B0 f! i* k4 C
  13. & w+ G1 X- W: u0 f
  14.         select * from
    & n  O  x% p9 ^# w; ~& p
  15.                 (select ROW_NUMBER() over(order by StudentNo) as rowNum,* from StudentInfo) as tbl
    1 w' _8 N8 _2 g5 O
  16.                 where tbl.rowNum between ((@pageIndex - 1)* @pageSize + 1) and (@pageIndex*@pageSize)
    & i) R- ]& \# N4 M( p; Z) i

  17. ( t; w6 G6 r: V* w5 `7 H
  18. GO
    : f, _  i+ i$ H! W! M  K! m
  19. * D, h. j8 \. o! M$ u5 _; G
  20. --调用
    7 F& Y4 y6 |' j# M. X" w
  21. % B: e1 O, V2 v5 m1 T
  22. EXEC P_GetStudentInfoPage 1,106 B8 `! Y  d* I- q7 a7 K

  23. 2 f2 Y* q! l9 m4 s
  24. EXEC P_GetStudentInfoPage 2,10% K- V2 m0 E) _9 F  I' Z+ p3 P
  25. : Q8 f" L5 t7 F2 e% \" g! o
  26. --------------------------------------直接查询代码
      Y2 E6 s5 y' u* T  l; t/ x
  27. select * from StudentInfo where id>=1 and id<=10
    ! g5 ?% h9 j* c5 d- W! f% W: b
  28. select * from StudentInfo where id>=10 and id<=20  N9 v0 q8 C6 N1 g  I

  29. ! Z1 B; k# R" S% f1 L" d
  30. select * from StudentInfo where id between (1-1)*10+1 and 10 *12 B3 s( L$ s  `
  31. select * from StudentInfo where id between (2-1)*10+1 and 10 *2
      r! ]* ~' B9 s7 P

  32. " t8 N; e* e0 K2 w$ e1 [+ o" ~
  33. select (1-1)*10+1 --1% I3 t$ K/ E- `% y, @
  34. select (2-1)*10+1 --11
    # c" P/ ]! f* I6 V
  35. select (3-1)*10+1 --21' E- \( s8 [8 Y# k$ J

  36. + P8 X: s2 p/ A
  37. select * from StudentInfo where id between 11 and 20
    0 y9 e, {$ J6 I, z+ R! U

  38. 7 O7 f5 |5 _& I: S
  39. select * from
    + }6 s. D5 D7 c4 ?9 y' N
  40. (select ROW_NUMBER() over(order by StudentNo desc) newRows,* from StudentInfo) t) H' P' Q: T5 ?% |& q' j( D: k
  41. where t.newRows between (1-1)*10+1 and 10 *1
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|星空社区 |网站地图

GMT+8, 2024-4-26 23:23 , Processed in 0.050096 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表