Sql server动态加载存储过程--分页

create procedure [dbo].[pro_getStu]
(
@pindex int,  --最小是1,第1页
@psize int,
@name nvarchar,
@dbcount int out--总记录数
)
as
begin

-----取符合条件的记录信息总数量
declare @Sqlcount NVARCHAR(MAX)
SET @Sqlcount = N'SELECT @dbcount = COUNT(1) FROM DoFolder where 1=1 '
if @name is not null
begin
set @Sqlcount+=' and [DoFolderName] like ''%'+@name+'%''';
end
EXEC SP_EXECUTESQL  @Sqlcount,N'@dbcount int output',@dbcount output

-----取符合条件的记录信息
declare @sql varchar(2000);
set @sql='
select * from 
(
select ROW_NUMBER() over(order by DoFolderName) as num,
[DoFolderID],[DoFolderName],[ParentID]
from DoFolder where 1=1 ';

if @name is not null
begin
set @sql+='and [DoFolderName] like ''%'+@name+'%''';
end

set @sql +=') temptable
--where num  between ((页码-1)*几条+1) and (页码*几条)
where num  between '+ convert( varchar,(@pindex-1)*@psize+1) +' and '+ convert(varchar,@pindex*@psize);
print(@sql);
exec(@sql);

end

猜你喜欢

转载自www.cnblogs.com/huosanpie/p/9967501.html