SQL Server 单表和多表联查的万能存储过程
--单表万能存储过程 create procedure Proc_SingleTablePager ( @TableName varchar(50), --表名 @ReFieldsStr varchar(200) = '*', --字段名(全部字段为'*') @OrderString varchar(200), --排序字段(必须!支持多字段不用加order by) @WhereString varchar(500) = N'', --条件语句(不用加where) @PageSize int, --每页多少条记录 @PageIndex int = 1, --指定当前为第几页 @TotalRecord int output --返回总记录数 ) as begin --处理开始点和结束点 declare @StartRecord int; declare @EndRecord int; declare @TotalCountSql nvarchar(500); declare @SqlString nvarchar(2000); set @StartRecord = (@PageIndex - 1) * @PageSize + 1 --起始记录 set @EndRecord = @StartRecord + @PageSize - 1 --结尾记录 set @TotalCountSql = N'select @TotalRecord = count(*) from ' + @TableName;--总记录数语句 set @SqlString = N'(select row_number() over (order by '+ @OrderString +') as rowId,'+@ReFieldsStr+' from '+ @TableName;--查询语句 -- if(@WhereString != '' or @WhereString != null) begin set @TotalCountSql = @TotalCountSql + ' where ' + @WhereString; set @SqlString = @SqlString+ ' where'+ @WhereString; end --第一次执行得到 exec sp_executesql @totalCountSql,N'@TotalRecord int out',@TotalRecord output;--返回总记录数 --执行主语句 SET @SqlString ='select * from ' + @SqlString + ') as t where rowId between ' + ltrim(str(@StartRecord)) + ' and ' + ltrim(str(@EndRecord)); exec(@SqlString) end declare @total int exec Proc_SingleTablePager R_UserInfo,'*',UserID,' DepartmentID like ''CEF7F651-D4CD-4E6A-93CF-875401939AF8''',2,1,@total output select @total --多表万能存储过程 create procedure Proc_MultipleTablesPager ( @tblName varchar(255), --表名(注意:可以多表连接) @strGetFields varchar(1000) = '*', --需要返回的列 @OrderfldName varchar(255) = '', --排序的字段名 @PageSize int, --页尺寸 @PageIndex int = 1, --页码 @doCount int = 1 output, --查询到的记录数 @OrderType bit = 0, --设置排序类型,非0值则降序 @strWhere varchar(500) = '' --查询条件(注意:不要加where) ) as begin declare @strSQL nvarchar(4000) --主语句 declare @strTmp varchar(110) --临时变量 declare @strOrder varchar(300) --排序类型 if @strWhere != '' set @strSQL = 'select @doCount=count(*) from '+@tblName+' where '+@strWhere else set @strSQL = 'select @doCount=count(*) from '+@tblName exec sp_executesql @strSQL,N'@doCount int out',@doCount out --以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。一下的所有代码都是@doCount为0的情况 set @strSQL = ''; if @OrderType != 0 begin set @strTmp = '<(select min' --如果@OrderType不是0,就要执行降序,这句很重要 set @strOrder = ' order by ['+@OrderfldName+'] desc' end else begin set @strTmp = '>(select max' set @strOrder = ' order by ['+@OrderfldName+'] asc' end if @PageIndex = 1 begin if @strWhere != '' set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ' + @tblName + ' where ' + @strWhere + ' ' + @strOrder else --如果是第一页就执行上面的代码,这样会加快执行的速度 set @strSQL = 'select top '+str(@PageSize)+' '+@strGetFields+' from '+@tblName+' '+@strOrder end else --一下代码赋予了@strSQL真正执行的sql代码 begin set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ' + @tblName + ' where [' + @OrderfldName + ']' + @strTmp + ' (['+ @OrderfldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['+ @OrderfldName + '] from ' + @tblName + ' ' + @strOrder + ') as tblTmp)'+ @strOrder if @strWhere != '' set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ' + @tblName + ' where [' + @OrderfldName + ']' + @strTmp + '([' + @OrderfldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' [' + @OrderfldName + '] from ' + @tblName + ' where ' + @strWhere + ' ' + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder end exec(@strSQL) end