国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQL Server > 正文

SQLServer存儲過程實現單條件分頁

2024-08-31 01:04:45
字體:
來源:轉載
供稿:網友

話不多說,請看代碼:

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)

調用:execute pagination_basic 'UserDetail','*','','id','1','5','1','1','1','',''

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

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

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

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

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

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

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持VeVb武林網!


注:相關教程知識閱讀請移步到MSSQL教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁明县| 白水县| 安丘市| 阿勒泰市| 蓬溪县| 彭州市| 福清市| 白山市| 蒙自县| 门源| 资兴市| 巴青县| 佛教| 马鞍山市| 浙江省| 灵石县| 洪洞县| 邯郸县| 博湖县| 聂荣县| 五台县| 汽车| 龙门县| 昔阳县| 岢岚县| 青岛市| 类乌齐县| 湘乡市| 金堂县| 马尔康县| 四平市| 郎溪县| 娱乐| 长武县| 隆回县| 桐城市| 南溪县| 天津市| 巩义市| 萨嘎县| 汉阴县|