forked from jiw065/plsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TEST_UTILITY.pck
276 lines (223 loc) · 10.1 KB
/
TEST_UTILITY.pck
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
266
267
268
269
270
271
272
273
274
275
276
create or replace package TEST_UTILITY is
-- Author : AMBER
-- Created : 2019/4/14 18:06:01
-- Purpose : STORES THE TEST FUNCTION AND PROCEDURES
-- Public type declarations
TYPE T_EMP_SALARY IS TABLE OF NUMBER(8,2)
INDEX BY VARCHAR2(5) ;
TYPE EMP_EMAIL IS RECORD (EMPID EMPLOYEES.EMPLOYEE_ID%TYPE, EMAIL EMPLOYEES.EMAIL%TYPE);
TYPE EINFO IS RECORD ( EID EMPLOYEES.EMPLOYEE_ID%TYPE,
LN EMPLOYEES.LAST_NAME%TYPE,
SAL EMPLOYEES.SALARY%TYPE
);
TYPE MEMP_INFO IS RECORD (TITLE VARCHAR2(10),
EID EMPLOYEES.EMPLOYEE_ID%TYPE,
ELN EMPLOYEES.LAST_NAME%TYPE,
ESAL EMPLOYEES.SALARY%TYPE);
TYPE ELIST IS TABLE OF EINFO;
TYPE T_EMP_EMAIL IS TABLE OF EMP_EMAIL;
TYPE MLIST IS TABLE OF MEMP_INFO;
TYPE EMC IS REF CURSOR RETURN EMP_EMAIL;
TYPE ECV IS REF CURSOR;
-- Public constant declarations
--<ConstantName> constant <Datatype> := <Value>;
-- Public variable declarations
--<VariableName> <Datatype>;
-- Public function and procedure declarations
--function <FunctionName>(<Parameter> <Datatype>) return <Datatype>;
/*********************************************************************
* Object: BOOLEAN_TO_CHAR
* Type: function
* Purpose: Convert boolean type variable to varchar type
* Parameters:
* p_boolean: boolean variable needs to convert
* Return:
* v_char: converted varchar2 value to represent p_boolean
********************************************************************/
FUNCTION GET_EMP_SAL (P_DEPT_ID IN EMPLOYEES.DEPARTMENT_ID%TYPE)
RETURN T_EMP_SALARY;
PROCEDURE GET_EMP_SAL (P_DEPT_ID IN EMPLOYEES.DEPARTMENT_ID%TYPE);
/**
EMPLOYEE_ID NUMBER(6) Primary key of employees table.
FIRST_NAME VARCHAR2(20) Y First name of the employee. A not null column.
LAST_NAME VARCHAR2(25) Last name of the employee. A not null column.
EMAIL VARCHAR2(25) Email id of the employee
PHONE_NUMBER VARCHAR2(20) Y Phone number of the employee; includes country code and area code
HIRE_DATE DATE Date when the employee started on this job. A not null column.
JOB_ID VARCHAR2(10) Current job of the employee; foreign key to job_id column of the
jobs table. A not null column.
SALARY NUMBER(8,2) Y Monthly salary of the employee. Must be greater
than zero (enforced by constraint emp_salary_min)
COMMISSION_PCT NUMBER(2,2) Y Commission percentage of the employee; Only employees in sales
department elgible for commission percentage
MANAGER_ID NUMBER(6) Y Manager id of the employee; has same domain as manager_id in
departments table. Foreign key to employee_id column of employees table.
(useful for reflexive joins and CONNECT BY query)
DEPARTMENT_ID NUMBER(4) Y Department id where employee works; foreign key to department_id
column of the departments table
**/
FUNCTION INSERT_EMPLOYEE(P_EMPID NUMBER DEFAULT NULL, P_LNAME VARCHAR2, P_FNAME VARCHAR2,
P_EMAIL VARCHAR2,P_PNUM VARCHAR2,P_JOBID VARCHAR2,
P_SALARY NUMBER,P_DEPTID NUMBER)
RETURN EMPLOYEES.EMPLOYEE_ID%TYPE;
FUNCTION GET_EMAIL_FROM_DEPT(P_DEPTID IN NUMBER)
RETURN T_EMP_EMAIL PIPELINED;
FUNCTION GET_EMAIL_FROM_DEPT2 (P_DEPTID IN NUMBER)
RETURN EMC;
FUNCTION GET_MANAGER_INFO(MINF ECV)
RETURN MLIST PIPELINED;
FUNCTION GET_MANAGER_INFO2(P_DEPTID IN NUMBER)
RETURN ECV;
FUNCTION GET_MANAGER_INFO3
RETURN ECV;
PROCEDURE INSERT_JOB_TABLE( P_ID IN VARCHAR2,P_TITLE VARCHAR2,P_MIN IN NUMBER, P_MAX IN NUMBER, TABLE_NAME IN VARCHAR2);
end TEST_UTILITY;
/
create or replace package body TEST_UTILITY is
/* -- Private type declarations
TYPE T_EMP_SALARY IS TABLE OF NUMBER(8,2)
INDEX BY VARCHAR2(5) ; */
-- Private constant declarations
-- <ConstantName> constant <Datatype> := <Value>;
-- Private variable declarations
--<VariableName> <Datatype>;
-- Function and procedure implementations
FUNCTION GET_EMP_SAL (P_DEPT_ID IN EMPLOYEES.DEPARTMENT_ID%TYPE)
RETURN T_EMP_SALARY
IS
A_EMP_SAL T_EMP_SALARY;
BEGIN
FOR EMP IN (SELECT EMPLOYEE_ID, SALARY FROM EMPLOYEES WHERE DEPARTMENT_ID = P_DEPT_ID)LOOP
A_EMP_SAL(TO_CHAR(EMP.EMPLOYEE_ID)):= EMP.SALARY;
END LOOP;
RETURN A_EMP_SAL;
END;
PROCEDURE GET_EMP_SAL (P_DEPT_ID IN EMPLOYEES.DEPARTMENT_ID%TYPE)
IS
A_EMP_SAL T_EMP_SALARY;
I VARCHAR2(5);
BEGIN
A_EMP_SAL := GET_EMP_SAL (P_DEPT_ID);
I := A_EMP_SAL.FIRST;
WHILE I IS NOT NULL LOOP
DBMS_OUTPUT.put_line('SALARY OF '||I ||' IS '||A_EMP_SAL(I));
I:= A_EMP_SAL.NEXT(I);
END LOOP;
END;
FUNCTION INSERT_EMPLOYEE(P_EMPID NUMBER DEFAULT NULL, P_LNAME VARCHAR2, P_FNAME VARCHAR2,
P_EMAIL VARCHAR2,P_PNUM VARCHAR2,P_JOBID VARCHAR2,
P_SALARY NUMBER,P_DEPTID NUMBER)
RETURN EMPLOYEES.EMPLOYEE_ID%TYPE
IS
V_EMPID EMPLOYEES.EMPLOYEE_ID%TYPE;
DUP_PK EXCEPTION;
PRAGMA EXCEPTION_INIT(DUP_PK,-2292);
BEGIN
V_EMPID := NVL(P_EMPID,EMPLOYEES_SEQ.NEXTVAL);
INSERT INTO EMPLOYEES(EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
EMAIL,
PHONE_NUMBER,
HIRE_DATE,
JOB_ID,
SALARY,
COMMISSION_PCT,
MANAGER_ID,
DEPARTMENT_ID)
VALUES(V_EMPID,P_FNAME,P_LNAME,P_EMAIL,P_PNUM,SYSDATE,P_JOBID,P_SALARY,NULL,101,P_DEPTID);
RETURN V_EMPID;
EXCEPTION
WHEN DUP_PK THEN
DBMS_OUTPUT.put_line('DUP PRIMARY KEY : EMAIL, EMPID');
DBMS_OUTPUT.put_line(DBMS_UTILITY.format_error_stack||' '||DBMS_UTILITY.format_error_backtrace);
END;
/*
FUNCTION GET_EMAIL_FROM_DEPT(P_DEPTID IN NUMBER)
RETURN T_EMP_EMAIL PIPELINED AS CURSOR K IS
SELECT EMPLOYEE_ID, EMAIL FROM EMPLOYEES WHERE DEPARTMENT_ID = P_DEPTID ;
BEGIN
FOR REC IN K LOOP
PIPE ROW(REC);
END LOOP ;
END;*/
FUNCTION GET_EMAIL_FROM_DEPT(P_DEPTID IN NUMBER)
RETURN T_EMP_EMAIL PIPELINED
IS
BEGIN
FOR E IN (SELECT EMPLOYEE_ID, EMAIL FROM EMPLOYEES WHERE DEPARTMENT_ID = P_DEPTID)LOOP
PIPE ROW (E);
END LOOP;
END;
FUNCTION GET_EMAIL_FROM_DEPT2 ( P_DEPTID IN NUMBER)
RETURN EMC
IS
CC EMC;
BEGIN
OPEN CC FOR SELECT EMPLOYEE_ID, EMAIL FROM EMPLOYEES WHERE DEPARTMENT_ID = P_DEPTID;
RETURN CC;
END;
FUNCTION GET_MANAGER_INFO(MINF ECV)
RETURN MLIST PIPELINED
IS
EC ECV;
ML MEMP_INFO;
m EINFO;
EL Elist;
BEGIN
loop
FETCH MINF INTO M.EID,M.LN,M.SAL,EC;
ML.TITLE := 'MANAGER';
ml.EID := m.EID;
ml.ELN := m.LN;
ml.ESAL := m.SAL;
EXIT WHEN MINF%NOTFOUND;
PIPE ROW (ML);
FETCH EC bulk collect into EL;
FOR J IN EL.FIRST .. EL.LAST LOOP
ML.TITLE := 'EMPLOYEE';
ML.EID :=EL(J).EID;
ML.ELN :=EL(J).LN;
ML.ESAL :=EL(J).SAL;
PIPE ROW (ML);
END LOOP;
END LOOP;
CLOSE MINF;
return;
END;
FUNCTION GET_MANAGER_INFO2(P_DEPTID IN NUMBER)
RETURN ECV
IS
EC ECV;
BEGIN
OPEN EC FOR 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 WHERE D.DEPARTMENT_ID = P_DEPTID;
RETURN EC;
END;
FUNCTION GET_MANAGER_INFO3
RETURN ECV
IS
EC ECV;
BEGIN
OPEN EC FOR 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;
RETURN EC;
END;
PROCEDURE INSERT_JOB_TABLE( P_ID IN VARCHAR2,P_TITLE VARCHAR2,P_MIN IN NUMBER, P_MAX IN NUMBER, TABLE_NAME IN VARCHAR2)
IS
BEGIN
EXECUTE IMMEDIATE 'INSERT INTO '||TABLE_NAME||' VALUES( :1,:2, :3, :4)'
USING P_ID, P_TITLE,P_MIN,P_MAX;
END;
begin
NULL;
end TEST_UTILITY;
/