if (exists(select * from sys. procedures where name= ‘proc_paging‘ ))-- 如果 proc_paging这个存储过程存在
drop proc proc_paging -- 那么就删除这个存储过程
go
create proc proc_paging(
@pageSize nvarchar ( 50),
@currentPage nvarchar ( 50),
@tablename nvarchar ( 50),
@orderbycolumn nvarchar ( 50),
@wherewithand nvarchar ( max)
) --创建存储过程
as
begin
-- 声明变量
declare @sql nvarchar( max )
-- 拼接sql
set @sql =
‘select top ‘ +@pageSize + ‘ * from (
select ROW_NUMBER() over(order by ‘ + @orderbycolumn+ ‘) as rowid ,* from ‘+ @tablename+ ‘
where 1=1 ‘ + @wherewithand+ ‘
)as A
where rowid> (‘ +@pageSize + ‘)*((‘+ @currentPage +‘)-1)‘
-- 执行sql 命令
exec (@sql )
end
go
--测试
exec proc_paging 10, 1 ,‘loginfo‘ , ‘id‘, ‘‘