if exists(select * from sysobjects where id = object_id(N'up_GetMusicByCondition') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure up_GetMusicByConditionGO--分页存储过程create procedure up_GetMusicByCondition( @Condition nvarchar(1000), --查询条件 @PageSize int, --每页显示多少条 @CurrentPage int, --当前显示第几页 @Pages int output --分页后的总页数)asbegin declare @sql nvarchar(2000)--声明一个字符串变量 declare @total int --辅助变量,保存按当前条件总共有多少条记录 --给@Pages赋值 set @sql='select @temp=COUNT(*) from [Music],[Country] where [Music].[CountryId]=[Country].[CountryId] and '+@Condition exec sp_executesql @sql,N'@temp int output',@total output --求出总页数 set @Pages=ceiling(CONVERT(decimal(6,2),@total)/@PageSize) --查询目标页数据 declare @begin int --要找的数据的起始编码 declare @end int --要找的数据的结束编码 set @begin=@PageSize*(@CurrentPage-1)+1 set @end=@CurrentPage*@PageSize set @sql='select * from ( select row_number() over(order by Id) AS ''Num'' ,[Music].*,[Country].[CountryName] from [Music],[Country] where [Music].[CountryId]=[Country].[CountryId] and ' +@Condition+') T where T.Num between ' +convert(varchar(5),@begin) +' and ' +convert(varchar(5),@end) print @sql--可以不要是为了调试找错 exec(@sql)end