-
Notifications
You must be signed in to change notification settings - Fork 0
/
PL-SQL Cursors.sql
147 lines (130 loc) · 3.1 KB
/
PL-SQL Cursors.sql
1
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
-- PL-SQL Cursors Examples
DROP TABLE Point2D;
CREATE TABLE Point2D
(
X INTEGER,
Y INTEGER,
CONSTRAINT Point2D_PK
PRIMARY KEY (X, Y)
);
INSERT INTO Point2D VALUES (5, 6);
INSERT INTO Point2D VALUES (6, 7);
INSERT INTO Point2D VALUES (11, 13);
-- Select one row into variables
DECLARE
A NUMBER;
B NUMBER;
BEGIN
SELECT X, Y INTO A, B
FROM Point2D
WHERE Y > 12;
IF (B > A) THEN
INSERT INTO Point2D VALUES(B, A);
END IF;
END;
/
SELECT * FROM Point2D;
-- Doesn't work when more than one row returns
DECLARE
A NUMBER;
B NUMBER;
BEGIN
SELECT X, Y INTO A, B
FROM Point2D
WHERE Y < 12;
IF (B > A) THEN
INSERT INTO Point2D VALUES(B, A);
END IF;
END;
/
-- Find all points between 1 and 10 (x-axis)
DECLARE
A NUMBER;
B NUMBER;
vCount NUMBER;
BEGIN
FOR counter IN 1 .. 10
LOOP SELECT COUNT(*) INTO vCount
FROM Point2D
WHERE X BETWEEN counter AND counter + 1;
IF vCount = 1 THEN
SELECT X,Y INTO A, B
FROM Point2D
WHERE X BETWEEN counter AND counter + 1;
DBMS_OUTPUT.PUT_LINE('Found ' || A || ' and ' || B);
ELSE DBMS_OUTPUT.PUT_LINE('Too many (or too few) values -> ' || vCount);
END IF;
END LOOP;
END;
/
DROP TABLE Point2D;
CREATE TABLE Point2D
(
X INTEGER,
Y INTEGER,
CONSTRAINT Point2D_PK
PRIMARY KEY (X, Y)
);
INSERT INTO Point2D VALUES (5, 6);
INSERT INTO Point2D VALUES (6, 7);
INSERT INTO Point2D VALUES (11, 13);
INSERT INTO Point2D VALUES (37, 42);
INSERT INTO Point2D VALUES (15, 14);
-- A more complicated query
DECLARE
A Point2D.X%TYPE;
B Point2D.Y%TYPE;
CURSOR T1Cursor IS
SELECT X, Y
FROM Point2D
WHERE X < Y;
BEGIN
OPEN T1Cursor;
LOOP FETCH T1Cursor INTO A, B;
IF T1Cursor%FOUND THEN
DBMS_OUTPUT.PUT_LINE('I just found ' || A || ', ' || B);
END IF;
EXIT WHEN T1Cursor%NOTFOUND;
INSERT INTO Point2D VALUES(B, A);
END LOOP;
DBMS_OUTPUT.PUT_LINE('In total, we fetched ' || T1Cursor%rowcount || ' rows.');
CLOSE T1Cursor;
END;
/
SELECT * FROM Point2D;
-- A parametrized cursor
DECLARE
A Point2D.X%TYPE;
B Point2D.Y%TYPE;
CURSOR T1Cursor (LOW INTEGER, HIGH INTEGER) IS
SELECT X, Y
FROM Point2D
WHERE X BETWEEN LOW AND HIGH;
BEGIN
OPEN T1Cursor(1, 8);
LOOP FETCH T1Cursor INTO A, B;
EXIT WHEN T1Cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('We will try to insert (' || B || ', ' || A || ')');
DBMS_OUTPUT.PUT_LINE('In total, we fetched ' || T1Cursor%rowcount || ' row(s).');
END LOOP;
CLOSE T1Cursor;
END;
/
SELECT * FROM Point2D;
-- An example with Student2 table
DECLARE
st_id Student2.SID%TYPE;
ln Student2.LastName%TYPE;
fn Student2.FirstName%TYPE;
CURSOR STCursor IS
SELECT SID, LastName, FirstName
FROM Student2;
BEGIN
OPEN STCursor;
LOOP FETCH STCursor INTO st_id, ln, fn;
EXIT WHEN STCursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Student: ' || fn || ' ' || ln);
END LOOP;
CLOSE STCursor;
END;
/