cnblogs/dcrenl/SQL 2005 分页存储过程.html

78 lines
5.6 KiB
HTML
Raw Normal View History

2024-09-24 12:43:01 +08:00
<div id="sina_keyword_ad_area2" class="articalContent ">
<div>-- =============================================</div>
<div>-- Description: &nbsp;<wbr>&nbsp;<wbr>&lt;高效分页存储过程适用于Sql2005以上&gt;</div>
<div>-- Notes: &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&lt;排序字段强烈建议建索引&gt;</div>
<div>-- Page2005'dyj_vclient','client_id,last_dt','client_id','last_dt','desc','',20,1,0</div>
<div>-- =============================================</div>
<div>CREATE Procedure [dbo].[sp_page]&nbsp;<wbr></div>
<div>&nbsp;<wbr>@TableName varchar(2000), &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> --表名</div>
<div>&nbsp;<wbr>@Fields varchar(2000) = '*',&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>--字段名(全部字段为*)</div>
<div>&nbsp;<wbr>@PrimaryKey varchar(50), &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>--主键</div>
<div>&nbsp;<wbr>@OrderField varchar(500), &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>--排序字段(必须!支持多字段)</div>
<div>&nbsp;<wbr>@sqlWhere varchar(500) = Null,&nbsp;<wbr> --条件语句(不用加where)</div>
<div>&nbsp;<wbr>@pageSize int, &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>--每页多少条记录</div>
<div>&nbsp;<wbr>@pageIndex int = 1 , &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>--指定当前为第几页</div>
<div>&nbsp;<wbr>@TotalPage int output &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>--返回总页数&nbsp;<wbr></div>
<div>as</div>
<div>begin</div>
<div>&nbsp;<wbr></div>
<div>&nbsp;<wbr> &nbsp;<wbr> Begin Tran --开始事务</div>
<div>&nbsp;<wbr></div>
<div>&nbsp;<wbr> &nbsp;<wbr> Declare @sqlnvarchar(4000);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> Declare @totalRecordint; &nbsp;<wbr> &nbsp;<wbr></div>
<div>&nbsp;<wbr> &nbsp;<wbr> ifisnull(@PrimaryKey,'')=''</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>set @PrimaryKey='*'</div>
<div>&nbsp;<wbr> &nbsp;<wbr> --计算总记录数</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr></div>
<div>&nbsp;<wbr> &nbsp;<wbr> if (@SqlWhere='' or@sqlWhere=NULL)</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> set @sql = 'select @totalRecord = count('&#43;@PrimaryKey &#43;') from ' &#43; @TableName</div>
<div>&nbsp;<wbr> &nbsp;<wbr> else</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> set @sql = 'select @totalRecord = count('&#43;@PrimaryKey &#43;') from ' &#43; @TableName &#43; ' where ' &#43; @sqlWhere</div>
<div>&nbsp;<wbr></div>
<div>&nbsp;<wbr> &nbsp;<wbr> EXEC sp_executesql@sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr></div>
<div>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr></div>
<div>&nbsp;<wbr> &nbsp;<wbr> --计算总页数</div>
<div>&nbsp;<wbr> &nbsp;<wbr> select@TotalPage=CEILING((@totalRecord&#43;0.0)/@PageSize)</div>
<div>&nbsp;<wbr></div>
<div>&nbsp;<wbr> &nbsp;<wbr> if (@SqlWhere='' or@sqlWhere=NULL)</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> set @sql = 'Select * FROM (select ROW_NUMBER()Over(order by ' &#43; @OrderField &#43; ') as rowId,' &#43; @Fields &#43; ' from '&#43; @TableName&nbsp;<wbr></div>
<div>&nbsp;<wbr> &nbsp;<wbr> else</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> set @sql = 'Select * FROM (select ROW_NUMBER()Over(order by ' &#43; @OrderField &#43; ') as rowId,' &#43; @Fields &#43; ' from '&#43; @TableName &#43; ' where ' &#43; @SqlWhere &nbsp;<wbr>&nbsp;<wbr></div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr></div>
<div>&nbsp;<wbr> &nbsp;<wbr> --处理页数超出范围情况</div>
<div>&nbsp;<wbr> &nbsp;<wbr> if@PageIndex&lt;=0&nbsp;<wbr></div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> Set @pageIndex = 1</div>
<div>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr></div>
<div>&nbsp;<wbr> &nbsp;<wbr> if@pageIndex&gt;@TotalPage</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> Set @pageIndex = @TotalPage</div>
<div>&nbsp;<wbr></div>
<div>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>--处理开始点和结束点</div>
<div>&nbsp;<wbr> &nbsp;<wbr> Declare @StartRecordint</div>
<div>&nbsp;<wbr> &nbsp;<wbr> Declare @EndRecordint</div>
<div>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr></div>
<div>&nbsp;<wbr> &nbsp;<wbr> set @StartRecord =(@pageIndex-1)*@PageSize &#43; 1</div>
<div>&nbsp;<wbr> &nbsp;<wbr> set @EndRecord =@StartRecord &#43; @pageSize - 1</div>
<div>&nbsp;<wbr></div>
<div>&nbsp;<wbr> &nbsp;<wbr> --继续合成sql语句</div>
<div>&nbsp;<wbr> &nbsp;<wbr> set @Sql = @Sql &#43; ') as[page_result_table] where rowId between ' &#43;Convert(varchar(50),@StartRecord) &#43; ' and ' &#43;&nbsp;<wbr>Convert(varchar(50),@EndRecord)</div>
<div>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr></div>
<div>&nbsp;<wbr> &nbsp;<wbr> Exec(@Sql)</div>
<div>&nbsp;<wbr> &nbsp;<wbr>---------------------------------------------------</div>
<div>&nbsp;<wbr> &nbsp;<wbr> If @@Error&lt;&gt; 0</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>Begin</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> RollBack Tran</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> Return -1</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>End</div>
<div>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>Else</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>Begin</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> Commit Tran</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> Return @totalRecord ---返回记录总数</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> End&nbsp;<wbr> &nbsp;<wbr></div>
<div>end</div>
<div><br>
</div>
</div>