分页存储过程
上一篇 / 下一篇 2007-06-05 15:59:09 / 个人分类:ASP
)Cc+x&Sk4J0create procedure sp_search51Testing软件测试网B$E6g"E:AV4P:{6cZ
@tblName varchar(255), -- 表名51Testing软件测试网A?'H Q${1_5r$z&q }9O
51Testing软件测试网2j8[n"H*a.Q)C@strGetFields varchar(1000) = '*', -- 需要返回的列51Testing软件测试网Fv!}B$o`w
&m%V&rS-S?R0@fldName varchar(255)='', -- 排序的字段名51Testing软件测试网y lU2l0GgX)Dtxp
\;Y'{ d8g~,M0@PageSize int = 10, -- 页尺寸51Testing软件测试网3nY lTO
J V4x%\?0a4}4z7J T0@PageIndex int = 1, -- 页码51Testing软件测试网k3Nm,G4d5b
9?Z SCE m1\0@doCount bit = 0, -- 返回记录总数, 非 0 值则返回51Testing软件测试网RJ!_4]C|j}3[K
MSF5P'Ui7^1K0@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序51Testing软件测试网6WX(l.S0n8N3e.~kl
51Testing软件测试网s!kWr$I{@strWhere varchar(1500) = '' -- 查询条件 (注意: 不要加 where)
&ZGa&[&[|b1p0Kj:F|q0AS51Testing软件测试网%pNH.L(uq
)\:@ZK P*d0declare @strSQL varchar(5000) -- 主语句
MKe ?$^0Lf!y^h7iP,zb0declare @strTmp varchar(110) -- 临时变量51Testing软件测试网 Kx9GUb5S5v
51Testing软件测试网;`kX,H#|.Rdeclare @strOrder varchar(400) -- 排序类型51Testing软件测试网^2?@&L0Y(e$v4b-R
51Testing软件测试网,D"W-n"H/f7u@51Testing软件测试网j+C q^w8s T6z
51Testing软件测试网Q7C2\#HLif @doCount != 051Testing软件测试网0E.frn&V/{.s|7H
)MdPp I5m8}0begin51Testing软件测试网B5`'UXX9uj^#d:vd
+mG6V1@ E"E0 if @strWhere !=''
Wkv'|
CD0
7E.acs6H
r:S6t0u0 set @strSQL = "select count(*) as Total from [" + @tblName + "] where "+@strWhere51Testing软件测试网
|3WIP/QC9R
51Testing软件测试网a
Ya&w4^`,A
else
&r+V W\cQ!}jB0 51Testing软件测试网,D
r1W:w~-{V
set @strSQL = "select count(*) as Total from [" + @tblName + "]"
'E ~0E8E^5kI}0end 51Testing软件测试网y;c|zkd;i@_}@/i
51Testing软件测试网c)U#lk]@"I--以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况51Testing软件测试网+x~@V4y\
51Testing软件测试网&Az}1s^v `ifelse
9H[[-h)X*lM051Testing软件测试网KNrQt abegin51Testing软件测试网.eQ;?eO t
51Testing软件测试网#b&l}6om'ga%R"C9H-~Ym&\;ICed7[051Testing软件测试网%a{W}B
if @OrderType != 0
1X?!]l,n;i/A)j8G)v0!seGb s F0LO K6[0 begin
LH2~E|8`.P7|,J1B0x.j(YNP DK4T&X0 set @strTmp = "<(select min"51Testing软件测试网 Im:z o g(jc^
8h5H+C#A J5p0 set @strOrder = " order by [" + @fldName +"] desc"51Testing软件测试网%]SY#@_%U,X IW-VY
m'C,pFV0 --如果@OrderType不是0,就执行降序,这句很重要!
L9C]9_-d!~n1rG0{#YekRG0 end51Testing软件测试网)pm$TsS{}6V.H
51Testing软件测试网!z!N UDVelse51Testing软件测试网@9t bevo&y0q C
51Testing软件测试网\"SIs;s fA1Dbegin51Testing软件测试网lydrz
51Testing软件测试网$K9UhT xK&e9^/V gwset @strTmp = ">(select max"
Oznpq@ K(PFu$[051Testing软件测试网{"S/c-U#U7YW*Tset @strOrder = " order by [" + @fldName +"] asc"
8? H$U4]A;m09KBTr.i^0Z4k0 end
VG*P4yn!|)lr)t051Testing软件测试网0O/['nL8u6?ap~t51Testing软件测试网7sg,h5l:H R;An O/A
51Testing软件测试网E:N*~$S0P|if @PageIndex = 1
H,F Bo| rJ"o051Testing软件测试网:rty6kCnFbegin
2AAc-a1|#V03l3A9T.B_'s+]0 if @strWhere != ''
#Vf*q F3r)m j+N0