博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
高效存储过程分页
阅读量:6272 次
发布时间:2019-06-22

本文共 3416 字,大约阅读时间需要 11 分钟。

set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgoALTER PROCEDURE [dbo].[db_article_GetArticlePagination]( @pagesize int, @pageindex int,@classid int,@sp_keyes varchar(200),@isSmallClass bit)AS	SET NOCOUNT ON;declare @filter varchar(4000)declare @tb varchar(50)declare @id varchar(50)set @tb='db_article'set @id='a_id 'if(@classid is null)set @classid=0if(@sp_keyes<>'')set @sp_keyes= ' and (sp_keyes LIKE ''%' + @sp_keyes + '%'')'else set @sp_keyes=''if(@isSmallClass=0)set @filter='a_hidden=1  and  ( '+cast(@classid as varchar(20))+'=0 OR classid= '+cast(@classid as varchar(20))+' or classid IN   (SELECT classid  FROM db_sys_class  WHERE nsort = ''article'' AND c_class = '+cast(@classid as varchar(20))+')) ' +@sp_keyeselseset @filter='a_hidden=1  and  ( '+cast(@classid as varchar(20))+'=0 OR classid  = '+cast(@classid as varchar(20))+')'+@sp_keyes--print @filterexec sp_Pagination @tb,@id,'a_id desc',@pageindex,@pagesize,'a_id,classid,  username, a_topic, a_author, a_comto, a_pic, a_summary, a_counter,  a_tim, a_arrtim',@filter,null

  

 

set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgoALTER    PROCEDURE [dbo].[sp_Pagination](@Tables varchar(1000),@PrimaryKey varchar(100),@Sort varchar(200) = NULL,@CurrentPage int = 1,@PageSize int = 10,@Fields varchar(1000) = '*',@Filter varchar(1000) = NULL,@Group varchar(1000) = NULL)AS/*默认排序*/IF @Sort IS NULL OR @Sort = ''SET @Sort = @PrimaryKeyDECLARE @SortTable varchar(100)DECLARE @SortName varchar(100)DECLARE @strSortColumn varchar(200)DECLARE @operator char(2)DECLARE @type varchar(100)DECLARE @prec int/*设定排序语句.*/IF CHARINDEX('DESC',@Sort)>0BEGINSET @strSortColumn = REPLACE(@Sort, 'DESC', '')SET @operator = '<='ENDELSEBEGINIF CHARINDEX('ASC', @Sort) = 0SET @strSortColumn = REPLACE(@Sort, 'ASC', '')SET @operator = '>='ENDIF CHARINDEX('.', @strSortColumn) > 0BEGINSET @SortTable = SUBSTRING(@strSortColumn, 0, CHARINDEX('.',@strSortColumn))SET @SortName = SUBSTRING(@strSortColumn, CHARINDEX('.',@strSortColumn) + 1, LEN(@strSortColumn))ENDELSEBEGINSET @SortTable = @TablesSET @SortName = @strSortColumnENDSELECT @type=t.name, @prec=c.precFROM sysobjects o JOIN syscolumns c on o.id=c.idJOIN systypes t on c.xusertype=t.xusertypeWHERE o.name = @SortTable AND c.name = @SortNameIF CHARINDEX('char', @type) > 0SET @type = @type + '(' + CAST(@prec AS varchar) + ')'DECLARE @strPageSize varchar(50)DECLARE @strStartRow varchar(50)DECLARE @strFilter varchar(1000)DECLARE @strSimpleFilter varchar(1000)DECLARE @strGroup varchar(1000)/*默认当前页*/IF @CurrentPage < 1SET @CurrentPage = 1/*设置分页参数.*/SET @strPageSize = CAST(@PageSize AS varchar(50))SET @strStartRow = CAST(((@CurrentPage - 1)*@PageSize + 1) AS varchar(50))/*筛选以及分组语句.*/IF @Filter IS NOT NULL AND @Filter != ''BEGINSET @strFilter = ' WHERE ' + @Filter + ' 'SET @strSimpleFilter = ' AND ' + @Filter + ' 'ENDELSEBEGINSET @strSimpleFilter = ''SET @strFilter = ''ENDIF @Group IS NOT NULL AND @Group != ''SET @strGroup = ' GROUP BY ' + @Group + ' 'ELSESET @strGroup = ''/*执行查询语句*/EXEC('DECLARE @SortColumn ' + @type + 'SET ROWCOUNT ' + @strStartRow + 'SELECT @SortColumn=' + @strSortColumn + ' FROM ' + @Tables + @strFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort + 'SET ROWCOUNT ' + @strPageSize + 'SELECT ' + @Fields + ' FROM ' + @Tables + ' WHERE ' + @strSortColumn + @operator + ' @SortColumn ' + @strSimpleFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort + '')

  

转载于:https://www.cnblogs.com/B-bowen/p/3991054.html

你可能感兴趣的文章
dangdang.ddframe.job中页面修改表达式后进行检查
查看>>
Web基础架构:负载均衡和LVS
查看>>
Linux下c/c++相对路径动态库的生成与使用
查看>>
SHELL实现跳板机,只允许用户执行少量允许的命令
查看>>
SpringBoot 整合Redis
查看>>
nodejs安装以及环境配置(很好的node安装和配置文章,少走很多弯路)
查看>>
2014上半年大片早知道
查看>>
Android 6.0指纹识别App开发案例
查看>>
ios runtime基础知识
查看>>
正文提取算法
查看>>
Arcgis Engine(ae)接口详解(8):临时元素(element)
查看>>
大数据技术核心之ETL
查看>>
轻松学PHP
查看>>
Android android.support.design包
查看>>
CheckBox:屏蔽setChecked方法对OnCheckedChangeListener的影
查看>>
java线程池
查看>>
UI面试内容
查看>>
Linux之RPM详解
查看>>
Windows Azure 常见问题及测试题
查看>>
flex DateChooser
查看>>