1
set ANSI_NULLS ON
2
set QUOTED_IDENTIFIER ON
3
GO
4
ALTER procedure [dbo].[GetProductsByDeliverReason]
5
(
6
@deliverReason nvarchar(200),
7
@DescriptionLength int,
8
@PageNumber int,
9
@ProductsPerPage int,
10
@howManyProducts int output
11
)
12
AS
13
BEGIN
14
declare @Products table
15
(
16
RowNumber int,
17
ProductID int,
18
Name nvarchar(50),
19
Description nvarchar(1000),
20
Price money,
21
Image1FileName nvarchar(50),
22
Image2FileName nvarchar(50),
23
OnCatalogPromotion bit
24
)
25
26
declare @categoryID int
27
select @categoryID=CategoryID from DeliverReasonCategory where DeliverReason=@deliverReason
28
insert into @Products
29
select ROW_NUMBER() over (order by Product.ProductID),Product.ProductID,
30
Name,substring(Description,1,@DescriptionLength)+'
' as Description,Price,
31
Image1FileName,Image2FileName,OnCatalogPromotion from Product join DeliverReasonProductCategory
32
on Product.ProductID=DeliverReasonProductCategory.ProductID
33
where DeliverReasonProductCategory.CategoryID=@categoryID
34
35
select @howManyProducts=count(ProductID) from @Products
36
37
select ProductID,Name,Description,Price,Image1FileName,Image2FileName,OnCatalogPromotion
38
from @Products
39
where RowNumber>(@PageNumber-1)*@ProductsPerPage and RowNumber<=@PageNumber*@ProductsPerPage
40
END
41

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30


31

32

33

34

35

36

37

38

39

40

41

在此我简要的说明一下,该存储过程首先声明一个内存表变量用户保存从数据库中提取的全部数据,然后向该内存表中插入数据,注意在插入数据是多了一列,名为RowNumber,这列的值是用29行的ROW_NUMBER函数产生的。最后第37到39行取出真正想要的数据。