pager procedure for sql server

来源:互联网 发布:iphone7 usb共享网络 编辑:程序博客网 时间:2024/05/16 09:55
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO ALTER procedure dbo.pager ( @page_num int = 1, @item_count int = 0, @query_suffix nvarchar(4000), @query_fields nvarchar(4000) = '*', @query_key nvarchar(4000), @record_count int = null output ) /****************************************************************************** ** 文件: pager.sql ** 名称: pager ** ** 参数:@page_num 页码,起始页码为 1,默认页码为 1。 ** @item_count 每页显示的记录条数,默认显示0条。0条表示取出所有数据。 ** @query_suffix 查询后缀,即 from 后的所有语句。 ** @query_fields 需要查询的字段, 默认为所有字段 '*'。 ** @query_key 查询主键 ** @record_count 纪录总数,当参数为 null 的时候,不执行汇总操作。该参数暂未生效。 ** ** 描述: 分页器。 ** 取出指定区间的查询。 ** 使用须知: ** 1 查询必须显式指定排序方式。 ** 2 query_key 必须在 query_fields 中出现。 ** 3 query_key 应该是个不允许为空,且不重复的字段。 ** 示例: ** 用该函数取出 ** select a.id, a.name, b.school_name from user a, school b where a.school_id=b.id order by a.id desc ** 第二页的数据,每页显示 10 条纪录 ** exec pager @page_num = 2, @item_count = 10, @query_suffix='user a, school b where a.school_id=b.id order by a.id desc', @query_fields='a.id, a.name, b.school_name', @query_key='a.id' ** ** 要取出所有纪录 ** exec pager @query_suffix='user a, school b where a.school_id=b.id order by a.id desc', @query_fields='a.id, a.name, b.school_name', @query_key='a.id' ** ** 创建:whxbb@20030108 ** 修改: ** ** ** 返回:成功 0 失败错误代号. *******************************************************************************/ As -- 查询语句 declare @query nvarchar(4000) declare @query_start nvarchar(4000) declare @query_end varchar(4000) -- 错误号 declare @error_code int -- 起始记录号 declare @begin_no int -- 结束记录号 declare @end_no int set @query_suffix = ' from ' + @query_suffix if (@item_count = 0) begin set @query = 'select ' + @query_fields + ' ' + @query_suffix end else if (@page_num = 1) begin -- 第一页,直接使用 top n 取值 set @query = 'select top ' + cast(@item_count as nvarchar(10)) + ' ' + @query_fields + ' ' + @query_suffix end else -- 不是第一页 begin -- 上页的最后一个纪录号 set @begin_no = (@page_num - 1) * @item_count -- 本页的最后一个纪录号 set @end_no = @begin_no + @item_count -- 构建分页查询语句 set @query_start = 'select top ' + cast(@end_no as nvarchar(10)) + ' ' + @query_fields set @query_start = @query_start + ' from (' + @query_start + ' ' + @query_suffix + ') as query_table where' set @query_end = 'select top ' + cast(@begin_no as nvarchar(10)) + ' ' + @query_key + ' ' + @query_suffix set @query = @query_start + ' ' + @query_key + ' not in(' + @query_end + ')' end print 'Query constructed: ' + @query -- 执行分页查询语句 exec(@query) set @error_code = @@error if @error_code <> 0 goto error_handle if (@record_count is not null) begin-- 统计结果总数 -- 创建一个临时存储过程用于带出构建查询语句的结果 set @query = 'create procedure #tmp_procedure_pager_count(@count int output)as select top 100 percent ' + @query_key + ' ' + @query_suffix + ' select @count=@@rowcount' print 'Count query constructed:' + @query exec(@query) set @error_code = @@error if @error_code <> 0 goto error_handle -- 执行临时存储过程 exec #tmp_procedure_pager_count @record_count output set @error_code = @@error if @error_code <> 0 goto error_handle -- 删除临时存储过程 drop procedure #tmp_procedure_pager_count end error_handle: return @error_code return @error_code GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO -- use example execute pager @page_num = 1, @item_count = 3324, @query_suffix=' test where 1=1 order by entity_id', @query_key='entity_id' declare @i int set @i = 0 exec pager @query_suffix=' test where 1=1 order by entity_id', @query_fields='entity_name, entity_short_name, entity_id', @query_key='entity_id', @record_count=@i output select @i <script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击