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
--/********************参数说明*********************************************
[email protected] --要显示的表或多个表的连接
[email protected] --要查询出的字段列表,*表示全部字段
[email protected] --每页显示的记录个数
[email protected] --要显示那一页的记录
[email protected] --查询条件,不需where
[email protected] --用于排序的主键
[email protected] --用于排序,如:id desc (多个id desc,dt asc)
[email protected] --排序,1-顺序,2-倒序
[email protected] --查询到的总记录数
[email protected] --耗时测试时间差
--****************************************************************************/
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 = (@[email protected]) / @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
--/********************参数说明*********************************************
[email protected] --要显示的表或多个表的连接
[email protected] --要查询出的字段列表,*表示全部字段
[email protected] --每页显示的记录个数
[email protected] --要显示那一页的记录
[email protected] --查询条件,不需where
[email protected] --用于排序的主键
[email protected] --用于排序,如:id desc (多个id desc,dt asc)
[email protected] --排序,1-顺序,2-倒序
[email protected] --查询到的总记录数
[email protected] --耗时测试时间差
---- 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 (‘[email protected]+‘) AND ‘ --本来有条件再加上此条件
-- SET @where2=‘ WHERE (‘[email protected]+‘) ‘ --原本没有条件而加上此条件
-- END
-- --SET @sql=‘SELECT @intResult=COUNT(*) FROM ‘[email protected][email protected]
-- BEGIN
-- SET @sql=‘SELECT @sqlcount=COUNT(*) FROM (select ‘[email protected]+‘ from ‘+ @strTable + @where2 +‘) As tmptab‘
-- END
-- --print @sql
-- EXEC sp_executesql @sql,N‘@sqlcount int OUTPUT‘,@sqlcount OUTPUT --计算总记录数
-- SELECT @RecordCount = @sqlcount --设置总记录数
-- SELECT @PageCount = (@[email protected])/@pageSize --设置总页数
-- IF @pageIndex=1 --第一页
-- BEGIN
-- SET @sql=‘SELECT TOP ‘+CAST(@pageSize AS varchar(200))+‘ ‘[email protected]+‘ FROM ‘[email protected][email protected]+‘ORDER BY ‘+ @strSortField
-- END
-- Else
-- BEGIN
-- IF @strOrderBy=1
-- SET @sql=‘SELECT TOP ‘+CAST(@pageSize AS varchar(200))+‘ ‘[email protected]
-- + ‘ FROM ‘[email protected][email protected]
-- [email protected]+‘>(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))+‘ ‘[email protected]
-- +‘ FROM ‘[email protected][email protected]
-- +‘ ORDER BY ‘[email protected]
-- +‘ ) t‘
-- +‘)‘
-- + ‘ ORDER BY ‘[email protected]
-- ELSE
-- SET @sql=‘SELECT TOP ‘+CAST(@pageSize AS varchar(200))+‘ ‘[email protected]
-- +‘ FROM ‘[email protected][email protected]
-- [email protected]+‘<(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))+‘ ‘[email protected]
-- +‘ FROM ‘[email protected][email protected]
-- +‘ORDER BY ‘[email protected]
-- +‘) t‘
-- +‘)‘
-- +‘ ORDER BY ‘[email protected]
-- 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