码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2015-12-19 23:02:22      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

 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

 

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

标签:

原文地址:http://www.cnblogs.com/baiyunchen/p/4983750.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!