1: CREATE PROCEDURE [dbo].[usp_Product_GETPAGE]
2: (
3: @page int,
4: @pagelength int,
5: @sortfield varchar(100),
6: @descending bit,
7: @rowcount int output
8: )
9: AS
10: BEGIN
11: SET NOCOUNT OFF
12: DECLARE @Err int
13: SELECT @rowcount = COUNT(*) from [Products]
14: declare @innerrows int
15: declare @sortdesc varchar(100)
16: declare @sortasc varchar(100)
17: declare @a varchar(6)
18: declare @b varchar(6)
19: IF @descending=0
20: BEGIN
21: set @a = ' DESC '
22: set @b = ' ASC '
23: END
24: ELSE
25: BEGIN
26: set @a = ' ASC '
27: set @b = ' DESC '
28: END
29: IF charindex(@sortfield, ' [ProductID]') > 0
30: BEGIN
31: set @sortdesc = ''
32: set @sortasc = ''
33: END
34: ELSE
35: BEGIN
36: set @sortdesc = ', [ProductID] ' + @a
37: set @sortasc = ', [ProductID] ' + @b
38: END
39: set @innerrows = @rowcount - (@page * @pagelength)
40: DECLARE @sql nvarchar(1000)
41: SET @sql = 'SELECT TOP ' + STR(@pagelength) + ' [ProductID], [ProductName] FROM
42: (
43: SELECT TOP ' + STR(@innerrows) + ' [ProductID],
44: [ProductName]
45: FROM
46: [Products]
47: ORDER BY [Products].' + @sortfield + @a + @sortdesc + '
48: ) Alias
49: ORDER BY Alias.' + @sortfield + @b + @sortasc
50: EXEC (@sql);
51: END