-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cursor.sql
266 lines (212 loc) · 7.83 KB
/
Cursor.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
-- implicit cursor
-- cursor attributes
DECLARE
V_EMP NUMBER(5);
BEGIN
SELECT EMPLOYEE_ID INTO V_EMP FROM EMPLOYEES WHERE EMPLOYEE_ID = 101;
DBMS_OUTPUT.put_line(SQL%ROWCOUNT || boolean_to_char(SQL%FOUND));
UPDATE EMPLOYEES E SET E.SALARY = 9999 WHERE E.EMPLOYEE_ID = 888;
DBMS_OUTPUT.put_line(SQL%ROWCOUNT || boolean_to_char(SQL%FOUND));
DELETE FROM EMPLOYEES E WHERE E.EMPLOYEE_ID = 999;
DBMS_OUTPUT.put_line(boolean_to_char(SQL%NOTFOUND)|| boolean_to_char(SQL%FOUND));
END;
/
-- explicit cursor
DECLARE
CURSOR C IS SELECT EMPLOYEE_ID, SALARY FROM EMPLOYEES WHERE DEPARTMENT_ID = 100;
V_EID EMPLOYEES.EMPLOYEE_ID%TYPE;
V_SAL EMPLOYEES.SALARY%TYPE;
V_ES C%ROWTYPE;
CURSOR C1 IS SELECT * FROM EMPLOYEES E WHERE E.DEPARTMENT_ID = 100;
V_EMP EMPLOYEES%ROWTYPE;
TYPE EMP_L IS TABLE OF EMPLOYEES%ROWTYPE;
TYPE ES_L IS TABLE OF C%ROWTYPE;
LEMP EMP_L;
LES ES_L;
BEGIN
OPEN C;
FETCH C INTO V_EID, V_SAL;
WHILE C%FOUND LOOP
DBMS_OUTPUT.put_line('V_SAL '||V_EID ||' '||V_SAL);
FETCH C INTO V_EID, V_SAL;
END LOOP;
CLOSE C;
OPEN C;
LOOP
FETCH C INTO V_ES;
EXIT WHEN C%NOTFOUND;
DBMS_OUTPUT.put_line('V_ES '||V_ES.EMPLOYEE_ID||' '||V_ES.SALARY);
END LOOP;
CLOSE C;
OPEN C1;
FETCH C1 BULK COLLECT INTO LEMP;
FOR I IN LEMP.FIRST .. LEMP.LAST LOOP
DBMS_OUTPUT.put_line('LEMP '||LEMP(I).EMPLOYEE_ID||' '||LEMP(I).SALARY);
END LOOP;
CLOSE C1;
OPEN C;
FETCH C BULK COLLECT INTO LES;
FOR I IN LES.FIRST .. LES.LAST LOOP
DBMS_OUTPUT.put_line('LES '||LES(I).EMPLOYEE_ID||' '||LES(I).SALARY);
END LOOP;
CLOSE C;
END;
/
-- explicit cursor with parameter
DECLARE
CURSOR PC(DEPTID EMPLOYEES.DEPARTMENT_ID%TYPE DEFAULT 100) IS
SELECT E.DEPARTMENT_ID DEP,E.LAST_NAME, E.SALARY FROM EMPLOYEES E WHERE E.DEPARTMENT_ID = DEPTID;
V_EMP PC%ROWTYPE;
BEGIN
OPEN PC(80);
LOOP
FETCH PC INTO V_EMP;
EXIT WHEN PC%NOTFOUND;
DBMS_OUTPUT.put_line(V_EMP.LAST_NAME||' '||V_EMP.SALARY ||' '|| V_EMP.DEP);
END LOOP;
CLOSE PC;
END;
-- processing data result set select into ???
DECLARE
CURSOR C is select e.employee_id, e.last_name from employees e ;
V_EMP C%ROWTYPE;
BEGIN
null; -- ???
END;
-- processing data result set for loop
DECLARE
CURSOR C IS SELECT * FROM EMPLOYEES E WHERE E.DEPARTMENT_ID = 100;
V_EMP C%ROWTYPE;
BEGIN
FOR E IN C LOOP
V_EMP := E;
DBMS_OUTPUT.put_line('V_EMP '||V_EMP.EMPLOYEE_ID ||' '||E.LAST_NAME ||' '||c%rowcount);
END LOOP;
END;
-- processing result set via subquires
DECLARE
CURSOR C IS SELECT E.DEPARTMENT_ID,E.LAST_NAME,E.SALARY,S.ASAL FROM EMPLOYEES E,(SELECT ROUND(AVG(E2.SALARY),2) ASAL,
E2.DEPARTMENT_ID
FROM EMPLOYEES E2
GROUP BY E2.DEPARTMENT_ID) S
WHERE E.DEPARTMENT_ID = S.DEPARTMENT_ID
AND E.SALARY > S.ASAL;
V_EMP C%ROWTYPE;
BEGIN
FOR E IN C LOOP
V_EMP := E;
DBMS_OUTPUT.put_line('V_EMP '||V_EMP.DEPARTMENT_ID ||' '||V_EMP.LAST_NAME ||' '||V_EMP.SALARY||' '||V_EMP.ASAL);
END LOOP;
FOR E2 IN C LOOP
DBMS_OUTPUT.put_line('E2 '||E2.DEPARTMENT_ID ||' '||E2.LAST_NAME ||' '||E2.SALARY||' '||E2.ASAL);
END LOOP;
END;
-- FOR LOOP TO ASSIGN CURSOR, TO A COLLECTION
DECLARE
EL TEST_UTILITY.T_EMP_EMAIL := TEST_UTILITY.T_EMP_EMAIL();
CURSOR C IS SELECT EMPLOYEE_ID, EMAIL FROM EMPLOYEES WHERE DEPARTMENT_ID = 100;
VE TEST_UTILITY.EMP_EMAIL;
I NUMBER(5) := 1;
TYPE SL IS TABLE OF EMPLOYEES.SALARY%TYPE;
S SL;
BEGIN
--OPEN C;
DBMS_OUTPUT.put_line(boolean_to_char(EL IS NULL)||EL.COUNT);
--FETCH C BULK COLLECT INTO EL;
--FETCH C BULK COLLECT INTO S;
FOR E IN C LOOP
-- VE := E;
EL.EXTEND;
EL(I).empid := e.employee_id;
I := I+1;
END LOOP;
DBMS_OUTPUT.put_line(EL.COUNT||EL(5).empid);
--CLOSE C;
END;
-- CUROSR VARAIBLES
declare
vc test_utility.EMC;
emailList test_utility.T_EMP_EMAIL;
begin
vc := test_utility.GET_EMAIL_FROM_DEPT2(100);
fetch vc bulk collect into emailList;
for i in emailList.first .. emailList.last loop
dbms_output.put_line(emailList(i).empid||' '||emailList(i).email||' '||boolean_to_char(vc%isopen)||' '|| vc%rowcount);
end loop;
close vc;
dbms_output.put_line(boolean_to_char(vc%isopen));
end;
-- CURSOR EXPRESSION
DECLARE
TYPE C IS REF CURSOR;
--TYPE CC IS RECORD (VC C, DNAME DEPARTMENTS.DEPARTMENT_NAME%TYPE );
-- WRONG: the cursor type cannot be in a collection
VC C;
EVC C;
MVC C;
MEVC C;
TYPE DEPTL IS TABLE OF DEPARTMENTS.DEPARTMENT_NAME%TYPE;
DL DEPTL;
V_DP DEPARTMENTS.DEPARTMENT_NAME%TYPE;
TYPE EINFO IS RECORD ( V_EID EMPLOYEES.EMPLOYEE_ID%TYPE,
V_LN EMPLOYEES.LAST_NAME%TYPE,
V_SAL EMPLOYEES.SALARY%TYPE
);
TYPE ELIST IS TABLE OF EINFO;
EL ELIST;
ML ELIST;
MEL ELIST;
V_M EINFO;
STR VARCHAR2(100);
MSTR VARCHAR2(100);
BEGIN
OPEN VC FOR SELECT D.DEPARTMENT_NAME,
CURSOR(SELECT E.EMPLOYEE_ID, E.LAST_NAME,E.SALARY FROM EMPLOYEES E WHERE E.DEPARTMENT_ID = D.DEPARTMENT_ID) EMPLOYEES,
CURSOR(SELECT M.EMPLOYEE_ID , M.LAST_NAME ,M.SALARY,
CURSOR(SELECT ME.EMPLOYEE_ID,ME.LAST_NAME,ME.SALARY
FROM EMPLOYEES ME
WHERE ME.MANAGER_ID = M.EMPLOYEE_ID) MANAGED_EMPLOYEE
FROM EMPLOYEES M
WHERE M.DEPARTMENT_ID = D.DEPARTMENT_ID
AND M.EMPLOYEE_ID IN
(SELECT DISTINCT MANAGER_ID FROM EMPLOYEES E1
WHERE E1.DEPARTMENT_ID = D.DEPARTMENT_ID)) MANAGER
FROM DEPARTMENTS D;
FETCH VC INTO V_DP,EVC,MVC;
FETCH EVC BULK COLLECT INTO EL;
LOOP
FETCH MVC INTO V_M.V_EID,V_M.V_LN,V_M.V_SAL,MEVC;
EXIT WHEN MVC%NOTFOUND;
MSTR := 'MANAGER: '||V_M.V_EID||' '||V_M.V_LN;
FETCH MEVC BULK COLLECT INTO MEL;
FOR I IN MEL.FIRST .. MEL.LAST LOOP
STR := MSTR||' EMPLOYEE: '||MEL(I).V_EID||' '||MEL(I).V_LN;
DBMS_OUTPUT.put_line(STR);
STR := NULL;
END LOOP;
END LOOP;
/* FOR I IN EL.FIRST .. EL.LAST LOOP
DBMS_OUTPUT.put_line('EMPLOYEE: '||EL(I).V_EID||' '||EL(I).V_LN||' '||EL(I).V_SAL);
END LOOP;
*/
/*CLOSE VC;
CLOSE EVC;
CLOSE MVC; */
END;
/
-- PIPELINED RETURN TYPE WITH CURSOR
SELECT * FROM TABLE(TEST_UTILITY.GET_MANAGER_INFO(CURSOR(SELECT M.EMPLOYEE_ID , M.LAST_NAME ,M.SALARY,
CURSOR(SELECT ME.EMPLOYEE_ID,ME.LAST_NAME,ME.SALARY
FROM EMPLOYEES ME
WHERE ME.MANAGER_ID = M.EMPLOYEE_ID) MANAGED_EMPLOYEE
FROM EMPLOYEES M
WHERE M.DEPARTMENT_ID = 80
AND M.EMPLOYEE_ID IN
(SELECT DISTINCT MANAGER_ID FROM EMPLOYEES E1
WHERE E1.DEPARTMENT_ID = 80)) ));
SELECT DEPARTMENT_NAME, CURSOR(SELECT E.EMPLOYEE_ID, E.LAST_NAME
FROM EMPLOYEES E
WHERE E.EMPLOYEE_ID IN (SELECT DISTINCT MANAGER_ID
FROM EMPLOYEES
WHERE DEPARTMENT_ID = D.DEPARTMENT_ID ) ) MANAGER
FROM DEPARTMENTS D ;