CREATE TABLE p (
id INTEGER NOT NULL,
cat CHAR(1),
txt VARCHAR(10),
PRIMARY KEY(id)
)
GO
INSERT INTO p VALUES (1, 'A', 'Record 1')
GO
INSERT INTO p VALUES (2, 'A', 'Record 2')
GO
INSERT INTO p VALUES (3, 'A', 'Record 3')
GO
INSERT INTO p VALUES (4, 'B', 'Record 4')
GO
INSERT INTO p VALUES (5, 'A', 'Record 5')
GO
INSERT INTO p VALUES (6, 'A', 'Record 6')
GO
INSERT INTO p VALUES (7, 'A', 'Record 7')
GO
INSERT INTO p VALUES (8, 'A', 'Record 8')
GO
INSERT INTO p VALUES (9, 'B', 'Record 9')
GO
INSERT INTO p VALUES (10, 'A', 'Record 10')
GO
INSERT INTO p VALUES (11, 'A', 'Record 11')
GO
INSERT INTO p VALUES (12, 'A', 'Record 12')
GO
SELECT * FROM p
GO
CREATE PROCEDURE getpag @pagsiz INTEGER, @pagnum INTEGER, @cat CHAR(1)
AS
BEGIN
DECLARE @nrows INTEGER
DECLARE @npags INTEGER
DECLARE @firstrow INTEGER
DECLARE @lastrow INTEGER
SELECT @nrows = COUNT(*) FROM p WHERE cat = @cat
SET @npags = (@nrows + @pagsiz - 1) / @pagsiz
SET @firstrow = (@pagnum - 1) * @pagsiz + 1
SET @lastrow = @pagnum * @pagsiz
;
WITH temp AS (SELECT *,ROW_NUMBER() OVER(ORDER BY id ASC) AS rn FROM p WHERE cat = @cat)
SELECT @pagnum AS currpage,@npags AS totalpages,* FROM temp WHERE rn BETWEEN @firstrow AND @lastrow
END
GO
EXEC getpag 4,1,'A'
GO
EXEC getpag 4,2,'A'
GO
EXEC getpag 4,3,'A'
GO
EXEC getpag 5,2,'A'
GO
DROP PROCEDURE getpag
GO
DROP TABLE p
GO
giver:
1> CREATE TABLE p (
2> id INTEGER NOT NULL,
3> cat CHAR(1),
4> txt VARCHAR(10),
5> PRIMARY KEY(id)
6> )
7> GO
1> INSERT INTO p VALUES (1, 'A', 'Record 1')
2> GO
(1 row affected)
1> INSERT INTO p VALUES (2, 'A', 'Record 2')
2> GO
(1 row affected)
1> INSERT INTO p VALUES (3, 'A', 'Record 3')
2> GO
(1 row affected)
1> INSERT INTO p VALUES (4, 'B', 'Record 4')
2> GO
(1 row affected)
1> INSERT INTO p VALUES (5, 'A', 'Record 5')
2> GO
(1 row affected)
1> INSERT INTO p VALUES (6, 'A', 'Record 6')
2> GO
(1 row affected)
1> INSERT INTO p VALUES (7, 'A', 'Record 7')
2> GO
(1 row affected)
1> INSERT INTO p VALUES (8, 'A', 'Record 8')
2> GO
(1 row affected)
1> INSERT INTO p VALUES (9, 'B', 'Record 9')
2> GO
(1 row affected)
1> INSERT INTO p VALUES (10, 'A', 'Record 10')
2> GO
(1 row affected)
1> INSERT INTO p VALUES (11, 'A', 'Record 11')
2> GO
(1 row affected)
1> INSERT INTO p VALUES (12, 'A', 'Record 12')
2> GO
(1 row affected)
1> SELECT * FROM p
2> GO
id cat txt
----------- ---- ----------
1 A Record 1
2 A Record 2
3 A Record 3
4 B Record 4
5 A Record 5
6 A Record 6
7 A Record 7
8 A Record 8
9 B Record 9
10 A Record 10
11 A Record 11
12 A Record 12
(12 rows affected)
1> CREATE PROCEDURE getpag @pagsiz INTEGER, @pagnum INTEGER, @cat CHAR(1)
2> AS
3> BEGIN
4> DECLARE @nrows INTEGER
5> DECLARE @npags INTEGER
6> DECLARE @firstrow INTEGER
7> DECLARE @lastrow INTEGER
8> SELECT @nrows = COUNT(*) FROM p WHERE cat = @cat
9> SET @npags = (@nrows + @pagsiz - 1) / @pagsiz
10> SET @firstrow = (@pagnum - 1) * @pagsiz + 1
11> SET @lastrow = @pagnum * @pagsiz
12> ;
13> WITH temp AS (SELECT *,ROW_NUMBER() OVER(ORDER BY id ASC) AS rn FROM p W
HERE cat = @cat)
14> SELECT @pagnum AS currpage,@npags AS totalpages,* FROM temp WHERE rn BET
WEEN @firstrow AND @lastrow
15> END
16> GO
1> EXEC getpag 4,1,'A'
2> GO
currpage totalpages id cat txt rn
----------- ----------- ----------- ---- ---------- --------------------
1 3 1 A Record 1 1
1 3 2 A Record 2 2
1 3 3 A Record 3 3
1 3 5 A Record 5 4
(4 rows affected)
1> EXEC getpag 4,2,'A'
2> GO
currpage totalpages id cat txt rn
----------- ----------- ----------- ---- ---------- --------------------
2 3 6 A Record 6 5
2 3 7 A Record 7 6
2 3 8 A Record 8 7
2 3 10 A Record 10 8
(4 rows affected)
1> EXEC getpag 4,3,'A'
2> GO
currpage totalpages id cat txt rn
----------- ----------- ----------- ---- ---------- --------------------
3 3 11 A Record 11 9
3 3 12 A Record 12 10
(2 rows affected)
1> EXEC getpag 5,2,'A'
2> GO
currpage totalpages id cat txt rn
----------- ----------- ----------- ---- ---------- --------------------
2 2 7 A Record 7 6
2 2 8 A Record 8 7
2 2 10 A Record 10 8
2 2 11 A Record 11 9
2 2 12 A Record 12 10
(5 rows affected)
1> DROP PROCEDURE getpag
2> GO
1> DROP TABLE p
2> GO
Eksemplet er naturligvis ikke helt magen til dine data, men jeg mener at den her måde at strukturere SP på gør at man kan læse den.