sql分页存储过程简析(2)

来源:未知 责任编辑:责任编辑 发表时间:2015-03-01 01:38 点击:
DECLARE @new_order2 VARCHAR(1000)
DECLARE @new_order3 VARCHAR(1000)
DECLARE @Sql VARCHAR(8000)
DECLARE @SqlCount NVARCHAR(4000)
IF ISNULL(@where,'') = ''
BEGIN
SET @new_where1 = ' '
SET @new_where2 = ' WHERE '
END
ELSE  www.2cto.com  
BEGIN
SET @new_where1 = ' WHERE ' + @where
SET @new_where2 = ' WHERE ' + @where + ' AND '
END
IF ISNULL(@order,'') = '' OR @SortType = 1 OR @SortType = 2
BEGIN
IF @SortType = 1
   BEGIN
   SET @new_order1 = ' ORDER BY ' + @PrimaryKey + ' ASC'
   SET @new_order2 = ' ORDER BY ' + @PrimaryKey + ' DESC'
   END
IF @SortType = 2
   BEGIN
   SET @new_order1 = ' ORDER BY ' + @PrimaryKey + ' DESC'
   SET @new_order2 = ' ORDER BY ' + @PrimaryKey + ' ASC'
   END
END
ELSE
BEGIN
SET @new_order1 = ' ORDER BY ' + @Order
END
IF @SortType = 3 AND CHARINDEX(','+@PrimaryKey+' ',','+@Order)>0
BEGIN
SET @new_order1 = ' ORDER BY ' + @Order
SET @new_order2 = @Order + ','
SET @new_order2 = REPLACE(REPLACE(@new_order2,'ASC,','{ASC},'),'DESC,','{DESC},')
SET @new_order2 = REPLACE(REPLACE(@new_order2,'{ASC},','DESC,'),'{DESC},','ASC,')
SET @new_order2 = ' ORDER BY ' + SUBSTRING(@new_order2,1,LEN(@new_order2)-1)
IF @FieldList <> '*'  www.2cto.com  
   BEGIN
   SET @new_order3 = REPLACE(REPLACE(@Order + ',','ASC,',','),'DESC,',',')
   SET @FieldList = ',' + @FieldList
   WHILE CHARINDEX(',',@new_order3)>0
    BEGIN
    IF CHARINDEX(SUBSTRING(','+@new_order3,1,CHARINDEX(',',@new_order3)),','+@FieldList+',')>0
     BEGIN
     SET @FieldList =
     @FieldList + ',' + SUBSTRING(@new_order3,1,CHARINDEX(',',@new_order3))
     END
    SET @new_order3 = SUBSTRING(@new_order3,CHARINDEX(',',@new_order3)+1,LEN(@new_order3))
    END
   SET @FieldList = SUBSTRING(@FieldList,2,LEN(@FieldList))
   END
END
SET @SqlCount = 'SELECT @TotalCount=COUNT(*),@TotalPageCount=CEILING((COUNT(*)+0.0)/'
+ CAST(@PageSize AS VARCHAR)+') FROM ' + @TableName + @new_where1
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • sql常见面试题
  • SQL SERVER 2005性能之跟踪
  • sql server 列转行
  • SQL SERVER2008日常自动化备份
  • SQL Server 2005 镜像构建手册
  • LINUX上RMAN自动备份脚本
  • 如何将多个SQL查询统计结果一次显示出来
  • SQL编程(一)
  • 浅谈SQL Server中的事务日志(三)----在简单恢复模式下日志的角色
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索 - 移动版 - 返回顶部
Copyright © 2008-2013 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1