博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL.PRO_PageForIdTable
阅读量:5090 次
发布时间:2019-06-13

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

/****** Object:  StoredProcedure [dbo].[PRO_PageForIdTable]    Script Date: 04/29/2014 11:23:21 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author:        Goosoz@163.com-- Create date: 2013-04-03-- Description:    -- =============================================CREATE proc [dbo].[PRO_PageForIdTable](    @TableName  NVARCHAR(50),        --要查询的表名称,单表名称    @FieldsName NVARCHAR(1024),    --要返回的列名称 :*    @IdName NVARCHAR(50),        --标识列名称 能够唯一标识数据:ID    @StrWhere NVARCHAR(max),    --要求带WHERE关键字:where addtime>'2012'    @OrderASC bit,                --是否升序,0:DESC,1:ASC 要求和    @Page INT,                    --页码从1开始    @PageSize INT,                --    @TotalRow INT output    -- 记录总数 )asBEGIN    SET NOCOUNT ON;    DECLARE @sqlOrder AS NVARCHAR(512);    DECLARE @sql AS NVARCHAR(MAX);    if(@OrderASC=0)        set @sqlOrder='order by '+@IdName+' desc';    else        set @sqlOrder='order by '+@IdName+' asc';        begin        if(@Page<2)            --select top 20 id from tb_errorlog where id>33333 and id<444444 order by id desc            set @sql=' select top '+ CONVERT(NVARCHAR(20),@PageSize) +' '+@FieldsName+' from '+@TableName+' '+@StrWhere+' '+@sqlOrder;        else if(@OrderASC=0)            begin                if(@StrWhere='')                    --        select top 20 id from tb_errorlog                    --        where id<(select min(id) from (select top 30000 id from tb_errorlog order by id desc) as cnic_t_2012_001)                    --        order by id desc                    set @sql='select top '+ CONVERT(NVARCHAR(20),@PageSize) +' '+@FieldsName+' from '+@TableName                    +' where '+@IdName+'<(select min('+@IdName+') from (select top '+CONVERT(NVARCHAR(20),@PageSize*(@Page-1))+' '+@IdName+' from '+@TableName+' '+@sqlOrder+') as cnic_t_2012_001) '                    +@sqlOrder                else                    --        select top 20 id from tb_errorlog where id>33333 and id<444444                    --        and id<(select min(id) from (select top 30000 id from tb_errorlog where id>33333 and id<444444 order by id desc) as cnic_t_2012_001)                    --        order by id desc                    set @sql='select top '+ CONVERT(NVARCHAR(20),@PageSize) +' '+@FieldsName+' from '+@TableName+' '+ @StrWhere                    +' and '+@IdName+'<(select min('+@IdName+') from (select top '+CONVERT(NVARCHAR(20),@PageSize*(@Page-1))+' '+@IdName+' from '+@TableName+' '+ @StrWhere+' '+@sqlOrder+') as cnic_t_2012_001) '                    +@sqlOrder            end        else            begin                if(@StrWhere='')                    --        select top 20 id from tb_errorlog                    --        where id<(select min(id) from (select top 30000 id from tb_errorlog order by id desc) as cnic_t_2012_001)                    --        order by id desc                    set @sql=' select top '+ CONVERT(NVARCHAR(20),@PageSize) +' '+@FieldsName+' from '+@TableName                    +' where '+@IdName+'>(select max('+@IdName+') from (select top '+CONVERT(NVARCHAR(20),@PageSize*(@Page-1))+' '+@IdName+' from '+@TableName+' '+@sqlOrder+') as cnic_t_2012_001) '                    +@sqlOrder                else                    --        select top 20 id from tb_errorlog where id>33333 and id<444444                    --        and id<(select min(id) from (select top 30000 id from tb_errorlog where id>33333 and id<444444 order by id desc) as cnic_t_2012_001)                    --        order by id desc                    set @sql='select top '+ CONVERT(NVARCHAR(20),@PageSize) +' '+@FieldsName+' from '+@TableName+' '+ @StrWhere                    +' and '+@IdName+'>(select max('+@IdName+') from (select top '+CONVERT(NVARCHAR(20),@PageSize*(@Page-1))+' '+@IdName+' from '+@TableName+' '+ @StrWhere+' '+@sqlOrder+') as cnic_t_2012_001) '                    +@sqlOrder                end    end    --print (@sql);    exec (@sql);    set @sql='set @RowCount=(select count('+@FieldsName+') from '+@TableName+' '+@StrWhere+')';    exec sp_executesql @sql, N'@RowCount int output', @TotalRow output;ENDGO
SqlParameter[] Param = new SqlParameter[]{                new SqlParameter("@TableName","tb-xxx"),//0                new SqlParameter("@FieldsName","*"),//1                new SqlParameter("@IdName","ID"),//2                new SqlParameter("@StrWhere",sql.ToString()),//3 带where                new SqlParameter("@OrderASC",SortType==1),//4 此参数必须先强制转换为object类型                new SqlParameter("@Page",page),//5                new SqlParameter("@PageSize",pageSize),//6                new SqlParameter("@TotalRow",0)//7            };            Param[7].Direction = ParameterDirection.Output;            using (SqlDataReader dr = DbHelperSQL.RunProcedure("PRO_PageForIdTable", Param))            {                while (dr.Read())                {                    rtun.Add(GetModel(dr));                }                dr.Close();            }            TotalRow = Convert.ToInt32(Param[7].Value);            MaxPage = Units.GetPageCount(TotalRow, pageSize);            return rtun;

 

 

 

 

转载于:https://www.cnblogs.com/CodeBase/p/3698843.html

你可能感兴趣的文章
【深度学习】caffe 中的一些参数介绍
查看>>
Python-Web框架的本质
查看>>
QML学习笔记之一
查看>>
App右上角数字
查看>>
从.NET中委托写法的演变谈开去(上):委托与匿名方法
查看>>
小算法
查看>>
201521123024 《java程序设计》 第12周学习总结
查看>>
新作《ASP.NET MVC 5框架揭秘》正式出版
查看>>
IdentityServer4-用EF配置Client(一)
查看>>
WPF中实现多选ComboBox控件
查看>>
读构建之法第四章第十七章有感
查看>>
Windows Phone开发(4):框架和页 转:http://blog.csdn.net/tcjiaan/article/details/7263146
查看>>
Unity3D研究院之打开Activity与调用JAVA代码传递参数(十八)【转】
查看>>
python asyncio 异步实现mongodb数据转xls文件
查看>>
TestNG入门
查看>>
【ul开发攻略】HTML5/CSS3菜单代码 阴影+发光+圆角
查看>>
IOS-图片操作集合
查看>>
IO—》Properties类&序列化流与反序列化流
查看>>
测试计划
查看>>
Mysql与Oracle 的对比
查看>>