-
Notifications
You must be signed in to change notification settings - Fork 849
/
Copy pathcursor-for-loop.sql
106 lines (92 loc) · 2.15 KB
/
cursor-for-loop.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
/*
You can embed the SELECT directly inside the FOR loop. That's easiest, but that also means
you cannot reuse the SELECT in another FOR loop, if you have that need.
*/
BEGIN
FOR rec IN (SELECT * FROM employees)
LOOP
DBMS_OUTPUT.put_line (rec.last_name);
END LOOP;
END;
/
/*
You can also declare the cursor explicitly and then reference that in the FOR loop. You can
then use that same cursor in another context, such as another FOR loop.
*/
DECLARE
CURSOR emps_cur
IS
SELECT * FROM employees;
BEGIN
FOR rec IN emps_cur
LOOP
DBMS_OUTPUT.put_line (rec.last_name);
END LOOP;
FOR rec IN emps_cur
LOOP
DBMS_OUTPUT.put_line (rec.salary);
END LOOP;
END;
/
/*
You can add a parameter list to a cursor, just like you can a function.
You can only have IN parameters. Well, that makes sense, right?
*/
DECLARE
CURSOR emps_cur (department_id_in IN INTEGER)
IS
SELECT * FROM employees
WHERE department_id = department_id_in;
BEGIN
FOR rec IN emps_cur (1700)
LOOP
DBMS_OUTPUT.put_line (rec.last_name);
END LOOP;
FOR rec IN emps_cur (50)
LOOP
DBMS_OUTPUT.put_line (rec.salary);
END LOOP;
END;
/
/*
You can declare a cursor at the package level and then reference that cursor in multiple
program units. Remember, though, that if you explicitly open a packaged cursor (as in
OPEN emus_pkg.emps_cur), it will stay open until you close it explicitly (or disconnect).
*/
CREATE OR REPLACE PACKAGE emps_pkg
IS
CURSOR emps_cur
IS
SELECT * FROM employees;
END;
/
BEGIN
FOR rec IN emps_pkg.emps_cur
LOOP
DBMS_OUTPUT.put_line (rec.last_name);
END LOOP;
END;
/
/*
You can even hide the SELECT inside the package body in case you don't want developers to know the details of the query.
*/
CREATE OR REPLACE PACKAGE emps_pkg
IS
CURSOR emps_cur
RETURN employees%ROWTYPE;
END;
/
CREATE OR REPLACE PACKAGE BODY emps_pkg
IS
CURSOR emps_cur RETURN employees%ROWTYPE
IS
SELECT * FROM employees;
END;
/
BEGIN
FOR rec IN emps_pkg.emps_cur
LOOP
DBMS_OUTPUT.put_line (rec.last_name);
END LOOP;
END;
/