T-SQL筆記38_改善SQL分頁查詢效能_sp_cursoropen 和 sp_cursorfetch

之前有分享過 分頁寫法 這次遇到其他種分頁作法 舊系統分頁查詢SP 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Create proc [dbo].[SSP_GetPage] ( @sqlstr nvarchar( 4000 ), @pagepos int = 1 , @pagesize int = 99999999 , @recsize int output ) as begin set nocount on declare @P1 int ,@P2 int ,@P3 int ,@reccount int set @pagepos=(@pagepos- 1 )*@pagesize+ 1 exec sp_cursoropen @P1 output , @sqlstr , 1 , 8193 , @recsize output exec sp_cursorfetch @P1, 16 ,@pagepos,@pagesize exec sp_cursorclose @P1 end 這段程式碼定義了一個名為 SSP_GetPage 的存儲過程。該過程接受四個參數: @sqlstr:一個字符串參數,用於指定將要執行的 SQL 陳述式。 @pagepos:一個整數參數,指定要檢索的記錄起始位置,默認為 1。 @pagesize:一個整數參數,指定要檢索的記錄數目,默認為 99999999。 @recsize:一個輸出參數,指定檢索到的記錄數目。 在存儲過程的主體中,首先使用 set nocount on 語句來禁用向客戶端返回計數信息的功能。 然後聲明了四個整型變量 @P1、@P2、@P3 和 @reccount。 接下來,根據 @pagepos 和 @pagesize 計算要檢索的記錄起始位置,然後執行 sp_cursoropen 系統存儲過程來打開游標,使用 sp...