Sql Server 数据库分页存储过程书写

create proc 存储过程名称
(
@page int, //pageindex
@rows int, //pagesize
@rowCount int out
)
as
begin
--定义字符串变量,表示之后执行的sql语句
declare @strSql nvarchar(2000)
set @strSql=N'select top('+STR(@rows)+') * from (select ROW_NUMBER() over(order by id) as rowNum,* from city )as a where rowNum>'
+STR((@page-1)*@rows)

print @strSql --打印输出拼接后的sql语句
--执行sql语句
exec(@strSql)
--获取总记录数
set @strSql = 'select @total=count(0) from city'
//打印出存储过程语句
print @strSql

exec sp_executesql @strSql,N'@total int out',@total=@rowCount out

end

--执行存储过程
declare @count int
exec 存储过程名称1,3,@count out
print @count

posted @ 2020-06-06 10:20  小浩~  阅读(160)  评论(0编辑  收藏  举报