• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • pager procedure for sql server

    發表于:2007-07-02來源:作者:點擊數: 標簽:
    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 out
    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

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>