溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SQLServer中怎么利用存儲(chǔ)過程實(shí)現(xiàn)單條件分頁

發(fā)布時(shí)間:2021-07-26 17:00:37 來源:億速云 閱讀:181 作者:Leah 欄目:數(shù)據(jù)庫

SQLServer中怎么利用存儲(chǔ)過程實(shí)現(xiàn)單條件分頁,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

SQLServer Procedure Pagination_basic:ALTER PROCEDURE [qiancheng].[Pagination_basic] (@Table_name VARCHAR (255),--name of table@Rows_target VARCHAR (1000) = '*',--search rows @Rows_condition VARCHAR (1000) = '',--the condition to find target (no where)@Rows_order VARCHAR (255) = '',--the rows to rank@Order_type INT = 0,-- *Q*C* 0 normal 1 down@PageSizes INT = 10,--the size of each page@PageIndex INT = 1,--current page@ShowPages INT,--whether show the pages *Q*C* 1-yes 0-no@ShowRecords INT,--whether show the record *Q*C* 1-yes 0-no@Records_total INT OUTPUT,--returned total records@Pages_total INT OUTPUT --returned total pages) ASDECLARE @MainSQL_QC nvarchar (2000) --Main SQL sentenceDECLARE @Var_QC VARCHAR (100) --Temporary variateDECLARE @Order_QC VARCHAR (400) --the sort to rankSET @Records_total = 0SET @Pages_total = 0IF @ShowRecords = 1OR @ShowPages = 1BEGINIF @Rows_condition != ''SET @MainSQL_QC = 'select @Records_total = count(1) from [' + @Table_name + '] where ' +@Rows_conditionELSESET @MainSQL_QC = 'select @Records_total = count(1) from [' + @Table_name + ']' EXEC sp_executesql @MainSQL_QC, N'@Records_total int out' ,@Records_total OUTPUTENDIF @ShowPages = 1BEGINIF @Records_total <= @PageSizesSET @Pages_total = 1ELSEBEGINSET @Pages_total = @Records_total /@PageSizesIF (@Records_total %@PageSizes) > 0SET @Pages_total = @Pages_total + 1ENDENDIF @Order_type = 1BEGINSET @Var_QC = '<(select min'SET @Order_QC = ' order by [' + @Rows_order + '] desc'ENDELSEBEGINSET @Var_QC = '>(select max'SET @Order_QC = ' order by [' + @Rows_order + '] asc'ENDIF @PageIndex = 1BEGINIF @Rows_condition != ''SET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] where ' + @Rows_condition + ' ' + @Order_QCELSESET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] ' + @Order_QCENDELSEBEGINIF @Rows_condition != ''SET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] where [' + @Rows_order + ']' + @Var_QC + '([' + @Rows_order + ']) from (select top ' + str((@PageIndex - 1) *@PageSizes) + ' [' + @Rows_order + '] from [' + @Table_name + '] where ' + @Rows_condition + ' ' + @Order_QC + ') as Tmep_QC) and ' + @Rows_condition + ' ' + @Order_QCELSESET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] where [' + @Rows_order + ']' + @Var_QC + '([' + @Rows_order + ']) from (select top ' + str((@PageIndex - 1) *@PageSizes) + ' [' + @Rows_order + '] from [' + @Table_name + ']' + @Order_QC + ') as Tmep_QC)' + @Order_QCEND EXEC (@MainSQL_QC)

調(diào)用:execute pagination_basic 'UserDetail','*','','id','1','5','1','1','1','',''

主要是末尾的語句,拆分下來便是這樣:

select top 每頁數(shù) 列名 from [表名] where [排序字段名] < --1 倒序輸出若列 小于之前頁數(shù)的最小值

(select min ( [排序字段名] )from --2 獲得一個(gè)指定列名中的最小值并輸出

(select top (當(dāng)前頁-1)*每頁數(shù) [排序字段名] from [表名] where [條件] [排序類型]) --3 選擇之前頁數(shù)總數(shù)據(jù)倒序輸出

as Tmep_QC)--4 建立一個(gè)名為Tmep_QC的臨時(shí)表--2 獲得一個(gè)指定列名中的最小值并輸出

and [條件] [排序類型]--1 倒序輸出若列 小于之前頁數(shù)的最小值

關(guān)于SQLServer中怎么利用存儲(chǔ)過程實(shí)現(xiàn)單條件分頁問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI