java 調用 sqlserver分頁

存儲過程:
 CREATE PROCEDURE paging
    -- Add the parameters for the stored procedure here 
    --傳入參數 
    @SqlStr nvarchar(4000), --查詢字符串 
    @CurrentPage int, --第N頁(當前頁數) 
    @PageSize int --每頁行數 
AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 
    --定義變量 
    DECLARE @CursorId int --CursorId是遊標的id 
    DECLARE @Rowcount int --總記錄(行)數 
    DECLARE @pageCount int --總頁數 
    -- Insert statements for procedure here 
    
    EXEC sp_cursoropen @CursorId output,@SqlStr, 
        @Scrollopt=1,@Ccopt=1,@Rowcount=@Rowcount OUTPUT 
    
    SET @pageCount=CEILING(1.0*@Rowcount/@PageSize)--設置總頁數 
    
    SELECT @pageCount 
        AS 總頁數,@Rowcount AS 總行數,@CurrentPage AS 當前頁 --提示頁數 

    IF(@CurrentPage>@pageCount)--如果傳入的當前頁碼大入總頁碼數則把當前頁數設爲最後一頁 
        BEGIN 
            SET @CurrentPage = @pageCount--設置當前頁碼數 
        END 
    IF(@CurrentPage<=0)--如果傳入的當前頁碼大入總頁碼數則把當前頁數設爲第一頁 
        BEGIN 
            SET @CurrentPage = 1--設置當前頁碼數 
        END 
    SET @CurrentPage=(@CurrentPage-1)*@PageSize+1 --設置當前頁碼數 

    EXEC sp_cursorfetch @CursorId,16,@CurrentPage,@PageSize 
    EXEC sp_cursorclose @CursorId    --關閉遊標 

    SET NOCOUNT OFF 
END


發佈了39 篇原創文章 · 獲贊 1 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章