【SQLSERVER学习笔记】分页存储过程+调用

 1 USE [数据库名]
 2 GO
 3 
 4 SET ANSI_NULLS ON
 5 GO
 6 
 7 SET QUOTED_IDENTIFIER ON
 8 GO
 9 CREATE PROCEDURE [dbo].[存储过程名]
10     @pageIndex INT,
11     @pageSize INT,    
12     @totalCount INT OUTPUT    
13 AS 
14 SET NOCOUNT ON
15 
16 DECLARE @PageStart INT
17 DECLARE @PageEnd   INT
18 
19 SET @pageStart=(@pageIndex-1)*@pageSize
20 SET @pageEnd=@pageIndex*@pageSize
21 BEGIN 
22     SELECT @totalCount=COUNT(1) FROM [dbo].[表名]
23     SELECT 
24          [Id]
25         ,字段1
26         ,字段2
27         ,.....        
28     FROM 
29         (SELECT 
30             ROW_NUMBER() OVER (ORDER BY 字段名) AS RowNumber,
31              [Id]
32             ,[Name]
33             ,[Phone]
34             ,[Age]
35             ,[CreateTime] 
36         FROM [dbo].[表名] WITH(NOLOCK)) AS Temp
37     WHERE Temp.RowNumber>@PageStart AND Temp.RowNumber<@PageEnd
38     ORDER BY Temp.CreateTime DESC,Temp.Name
39 END
40 
41 GO

调用此存储过程的方法:

1 USE [数据库名]
2 GO
3 DECLARE @totalResult INT
4 EXEC [存储过程名] 1,10,@totalResult output
5 SELECT @totalResult

 

posted @ 2015-12-19 21:44  人生无赖  阅读(252)  评论(0编辑  收藏  举报