主頁 > 知識庫 > SQLServer存儲過程實現(xiàn)單條件分頁

SQLServer存儲過程實現(xiàn)單條件分頁

熱門標(biāo)簽:千呼電話機器人可以試用嗎 電銷需要外呼系統(tǒng)嗎 互聯(lián)網(wǎng)電話外呼系統(tǒng) 零成本地圖標(biāo)注賺錢 我要地圖標(biāo)注數(shù)量有限制嗎 電話機器人怎么代理商 家庭農(nóng)場地圖標(biāo)注名稱怎樣起名 400電話辦理泰安 安卡拉地圖標(biāo)注app

話不多說,請看代碼:

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
) AS
DECLARE @MainSQL_QC nvarchar (2000) --Main SQL sentence
DECLARE @Var_QC VARCHAR (100) --Temporary variate
DECLARE @Order_QC VARCHAR (400) --the sort to rank
SET @Records_total = 0
SET @Pages_total = 0
IF @ShowRecords = 1
OR @ShowPages = 1
BEGIN
IF @Rows_condition != ''
SET @MainSQL_QC = 'select @Records_total = count(1) from [' + @Table_name + '] where ' +@Rows_condition
ELSE
SET @MainSQL_QC = 'select @Records_total = count(1) from [' + @Table_name + ']' EXEC sp_executesql @MainSQL_QC,
 N'@Records_total int out' ,@Records_total OUTPUT
END
IF @ShowPages = 1
BEGIN
IF @Records_total = @PageSizes
SET @Pages_total = 1
ELSE
BEGIN
SET @Pages_total = @Records_total /@PageSizes
IF (@Records_total %@PageSizes) > 0
SET @Pages_total = @Pages_total + 1
END
END
IF @Order_type = 1
BEGIN
SET @Var_QC = '(select min'
SET @Order_QC = ' order by [' + @Rows_order + '] desc'
END
ELSE
BEGIN
SET @Var_QC = '>(select max'
SET @Order_QC = ' order by [' + @Rows_order + '] asc'
END
IF @PageIndex = 1
BEGIN
IF @Rows_condition != ''
SET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] where ' + @Rows_condition + ' ' + @Order_QC
ELSE
SET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] ' + @Order_QC
END
ELSE
BEGIN
IF @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_QC
ELSE
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 + ']' + @Order_QC + ') as Tmep_QC)' + @Order_QC
END 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 獲得一個指定列名中的最小值并輸出

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

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

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

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

您可能感興趣的文章:
  • EasyUi+Spring Data 實現(xiàn)按條件分頁查詢的實例代碼
  • Thinkphp搜索時首頁分頁和搜索頁保持條件分頁的方法
  • jQuery條件分頁 代替離線查詢(附代碼)

標(biāo)簽:文山 新鄉(xiāng) 濱州 池州 黃山 來賓 大同 東營

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SQLServer存儲過程實現(xiàn)單條件分頁》,本文關(guān)鍵詞  SQLServer,存儲,過程,實現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《SQLServer存儲過程實現(xiàn)單條件分頁》相關(guān)的同類信息!
  • 本頁收集關(guān)于SQLServer存儲過程實現(xiàn)單條件分頁的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章