zoukankan      html  css  js  c++  java
  • 机票分页存储过程!

    USE [FlightDB]
    GO
    /****** Object: StoredProcedure [dbo].[ProFlightPageList] Script Date: 2016/8/3 17:30:30 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    --/********************参数说明*********************************************
    --@strTable --要显示的表或多个表的连接
    --@strField --要查询出的字段列表,*表示全部字段
    --@pageSize --每页显示的记录个数
    --@pageIndex --要显示那一页的记录
    --@strWhere --查询条件,不需where
    --@strSortKey --用于排序的主键
    --@strSortField --用于排序,如:id desc (多个id desc,dt asc)
    --@strOrderBy --排序,1-顺序,2-倒序
    --@RecordCount --查询到的总记录数
    --@UsedTime --耗时测试时间差

    --****************************************************************************/
    ALTER PROCEDURE [dbo].[ProFlightPageList]
    @TableName VARCHAR(max)= NULL,--表名称,或者是表连接字符串,多表连接例如:student as s inner join dwinfo as dw on s.dwbh=dw.bh
    @SelectField VARCHAR(max)=NULL,--欲显示的列(多列用逗号分开),例如:id,name
    @PageSize INT =10,--每页显示条数
    @PageIndex INT=1,--记录开始数
    @Where VARCHAR(max)=NULL,--查询条件,''代表没有条件,单条件或者多条件,多条件例如:name='啊' and id=10
    @StrOrder VARCHAR(max) =NULL,--排序列(多个排序列用逗号分开),例如:id desc,name as
    @RecordCount INT OUTPUT, -- 返回记录总数
    @PageCount int OUTPUT, --总页数
    @UsedTime int OUTPUT --查询耗时,毫秒为单位
    AS
    BEGIN
    SET NOCOUNT ON;
    DECLARE @SqlQuery NVARCHAR(max);---查询列表
    DECLARE @SQLRowCount NVARCHAR(MAX) -- 用于查询记录总数的语句
    DECLARE @totalRecord INT;
    DECLARE @timediff DATETIME

    Begin Tran
    SELECT @timediff=getdate()
    --计算总记录数
    IF ( @Where = '''' OR @Where = '' OR @Where IS NULL )
    SET @SQLRowCount = 'select @totalRecord = count(*) from ' + @TableName
    ELSE
    SET @SQLRowCount = 'select @totalRecord = count(*) from ' + @TableName + ' where ' + @Where

    EXEC sp_executesql @SQLRowCount,N'@totalRecord int OUTPUT',@RecordCount OUTPUT--计算总记录数

    IF( @PageIndex = 1 OR @PageIndex = 0 OR @PageIndex < 0 )--查询第一页
    BEGIN
    IF( @Where IS NULL )--if(@Where='')

    SET @SqlQuery='select top ' + CONVERT(VARCHAR, @PageSize)
    + ' row_number() over(order by ' + @StrOrder
    + ' ) as RowNumber,' + @SelectField + ' from '
    + @TableName;

    ELSE

    SET @SqlQuery='select top ' + CONVERT(VARCHAR, @PageSize)
    + ' row_number() over(order by ' + @StrOrder
    + ' ) as RowNumberx,' + @SelectField + ' from '
    + @TableName + ' where ' + @Where;

    END
    ELSE--查询其它页
    BEGIN
    IF( @Where IS NULL )--if(@Where='')
    BEGIN
    SET @SqlQuery='with cte as (select row_number() over(order by '
    + @StrOrder + ' ) as RowNumber,' + @SelectField
    + ' from ' + @TableName
    + '

    )
    select * from cte where RowNumber between '
    +
    CONVERT(VARCHAR, (@PageIndex-1)*@PageSize+1)
    + ' and '
    + CONVERT(VARCHAR, @PageIndex*@PageSize)
    END
    ELSE
    BEGIN
    SET @SqlQuery='with cte as ( select row_number() over(order by '
    + @StrOrder + ' ) as RowNumber,' + @SelectField
    + ' from ' + @TableName + ' where ' + @Where
    + '

    )

    select * from cte where RowNumber between '
    +

    CONVERT(VARCHAR, (@PageIndex-1)*@PageSize+1)
    + ' and '
    + CONVERT(VARCHAR, @PageIndex*@PageSize)
    END
    END
    EXEC (@SqlQuery)

    If @@Error <> 0
    BEGIN
    ROLLBACK Tran
    RETURN -1
    END
    ELSE
    BEGIN
    COMMIT TRAN
    SET @UsedTime = datediff(ms,@timediff,getdate())--耗时
    SET @PageCount = (@RecordCount+@PageSize-1) / @PageSize --总页数
    END
    END


    --USE [FlightDB]
    --GO
    --/****** Object: StoredProcedure [dbo].[ProFlightPageList] Script Date: 2016/2/23 13:29:29 ******/
    --SET ANSI_NULLS ON
    --GO
    --SET QUOTED_IDENTIFIER ON
    --GO
    --/********************参数说明*********************************************
    --@strTable --要显示的表或多个表的连接
    --@strField --要查询出的字段列表,*表示全部字段
    --@pageSize --每页显示的记录个数
    --@pageIndex --要显示那一页的记录
    --@strWhere --查询条件,不需where
    --@strSortKey --用于排序的主键
    --@strSortField --用于排序,如:id desc (多个id desc,dt asc)
    --@strOrderBy --排序,1-顺序,2-倒序
    --@RecordCount --查询到的总记录数
    --@UsedTime --耗时测试时间差

    ---- exec ProHotelPageList 'Hotel','*',10,1,'1=1','id','id DESC',1,0,0
    --****************************************************************************/

    --ALTER PROCEDURE [dbo].[ProFlightPageList]
    -- @strTable varchar(1000) = ''--表名
    -- ,@strField VARCHAR(1000) = '*' --查询字段
    -- ,@pageSize INT = 10 --每页多少条记录
    -- ,@pageIndex int = 1 --当前页
    -- ,@strWhere varchar(1000) = '1=1' --查询条件
    -- ,@strSortKey varchar(1000) = 'id' --主键
    -- ,@strSortField varchar(500) = 'id DESC' --排序
    -- ,@strOrderBy bit = 1 --1表示升序 2表示倒序
    -- ,@RecordCount int OUTPUT --总记录数
    -- ,@PageCount int OUTPUT --总页数
    -- ,@UsedTime int OUTPUT --查询耗时,毫秒为单位
    --AS
    --SET NOCOUNT ON
    --Declare @sqlcount INT, @timediff DATETIME
    --select @timediff=getdate()
    --Begin Tran
    -- DECLARE @sql nvarchar(500),@where1 varchar(200),@where2 varchar(200)
    -- IF @strWhere is null or rtrim(@strWhere)=''
    -- BEGIN--没有查询条件
    -- SET @where1=' WHERE '
    -- SET @where2=' '
    -- END
    -- ELSE
    -- BEGIN--有查询条件
    -- SET @where1=' WHERE ('+@strWhere+') AND ' --本来有条件再加上此条件
    -- SET @where2=' WHERE ('+@strWhere+') ' --原本没有条件而加上此条件
    -- END
    -- --SET @sql='SELECT @intResult=COUNT(*) FROM '+@strTable+@where2

    -- BEGIN
    -- SET @sql='SELECT @sqlcount=COUNT(*) FROM (select '+@strSortKey+' from '+ @strTable + @where2 +') As tmptab'
    -- END
    -- --print @sql

    -- EXEC sp_executesql @sql,N'@sqlcount int OUTPUT',@sqlcount OUTPUT --计算总记录数
    -- SELECT @RecordCount = @sqlcount --设置总记录数
    -- SELECT @PageCount = (@sqlcount+@pageSize-1)/@pageSize --设置总页数

    -- IF @pageIndex=1 --第一页
    -- BEGIN
    -- SET @sql='SELECT TOP '+CAST(@pageSize AS varchar(200))+' '+@strField+' FROM '+@strTable+@where2+'ORDER BY '+ @strSortField
    -- END
    -- Else
    -- BEGIN
    -- IF @strOrderBy=1
    -- SET @sql='SELECT TOP '+CAST(@pageSize AS varchar(200))+' '+@strField
    -- + ' FROM '+@strTable+@where1
    -- +@strSortKey+'>(SELECT MAX('+case when charindex('.',@strSortKey)>0 then right(@strSortKey,len(@strSortKey)-charindex('.',@strSortKey)) else @strSortKey end+') '
    -- + ' FROM (SELECT TOP '+CAST(@pageSize*(@pageIndex-1) AS varchar(200))+' '+@strSortKey
    -- +' FROM '+@strTable+@where2
    -- +' ORDER BY '+@strSortField
    -- +' ) t'
    -- +')'
    -- + ' ORDER BY '+@strSortField
    -- ELSE
    -- SET @sql='SELECT TOP '+CAST(@pageSize AS varchar(200))+' '+@strField
    -- +' FROM '+@strTable+@where1
    -- +@strSortKey+'<(SELECT MIN('+case when charindex('.',@strSortKey)>0 then right(@strSortKey,len(@strSortKey)-charindex('.',@strSortKey)) else @strSortKey end+') '
    -- + ' FROM (SELECT TOP ' +CAST(@pageSize*(@pageIndex-1) AS varchar(200))+' '+@strSortKey
    -- +' FROM '+@strTable+@where2
    -- +'ORDER BY '+@strSortField
    -- +') t'
    -- +')'
    -- +' ORDER BY '+@strSortField
    -- END
    -- --PRINT @sql
    -- EXEC(@sql)

    --If @@Error <> 0
    -- BEGIN
    -- ROLLBACK Tran
    -- RETURN -1
    -- END
    --Else
    -- BEGIN
    -- COMMIT TRAN
    -- SET @UsedTime = datediff(ms,@timediff,getdate())--耗时
    -- RETURN @sqlcount
    -- END

  • 相关阅读:
    django2自动发现项目中的url
    Python中的__name__
    阻塞与非阻塞、同步与异步、I/O模型
    Python中的关键字的用法
    元类
    数据库介绍
    django+nginx+uwsgi 项目部署
    centos7安装mysql5.6
    MySQL5.7的新特性
    Python进行MySQL数据库操作
  • 原文地址:https://www.cnblogs.com/taomylife/p/5733765.html
Copyright © 2011-2022 走看看