CREATE PROCEDURE [dbo].[getInfo]
@tableName varchar(30), --表名
@tiaojiao varchar(200), --查询条件(不加where关键字)
@tableList varchar(200), -- 要查询的列(用,分隔)
@zhujian varchar(10), --主键列
@paixu varchar(200), --排序条件
@pageNum int, --当前页数
@pageSize int, --每页记录数
@recordCount int output --总记录数(存储过程输出参数)
AS
declare @TempSelect nvarchar(600)
if(@tiaojiao!='')
set @TempSelect ='select @recordCount=count(*) from '+@tableName+' where '+@tiaojiao
else
set @TempSelect ='select @recordCount=count(*) from '+@tableName
--执行sql语句,查询总记录数
exec sp_executesql @TempSelect,
N'@recordCount int OUTPUT' , --执行输出数据的sql语句,output出总记录数
@recordCount output
if(@tiaojiao!='')
begin
set @TempSelect='select top '+str(@pageSize)+' '+ @tableList+' from '+@tableName+' where ' +@zhujian+' not in (select top '+str((@pageNum-1)*@pageSize)+' '+ @zhujian +' from '+@tableName+' where '+ @tiaojiao+' '+ @paixu+') and '+@tiaojiao+' '+ @paixu
end
else
set @TempSelect='select top '+str(@pageSize)+' '+ @tableList+' from '+@tableName+' where ' +@zhujian+' not in (select top '+str((@pageNum-1)*@pageSize)+' '+ @zhujian +' from '+@tableName+' '+ @paixu+') '+ @paixu
exec sp_executesql @TempSelect
return @@rowcount
GO