-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPLSQL.sql
355 lines (259 loc) · 7.11 KB
/
PLSQL.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
set serveroutput on
declare
doc_name varchar(30);
begin
--displays 1 row
select doctor_name into doc_name from DOCTOR where DOCTOR.DOCTOR_ID=01;
dbms_output.put_line(doc_name);
end;
/
--%type
set serveroutput on
declare
p_name PATIENT.PATIENT_NAME%type;
p_age PATIENT.AGE%type;
p_gender PATIENT.GENDER%type;
begin
select PATIENT.patient_name , age , PATIENT.GENDER into p_name,p_age , p_gender from PATIENT where patient_id=101;
dbms_output.put_line('name : '||p_name||' ; age : '||p_age ||' ; Gender : '||p_gender );
end;
/
--default value and insert
set serveroutput on
declare
p_id PATIENT.PATIENT_ID%type :=134;
p_name PATIENT.PATIENT_NAME%type :='Ananya';
p_email PATIENT.EMAIL%type :='[email protected]';
p_phone PATIENT.PHONE_NO%type :='0123569';
p_age PATIENT.AGE%type :=23;
p_gender PATIENT.GENDER%type :='F';
begin
insert into Patient values (p_id,p_name,p_email,p_phone,p_age,p_gender);
end;
/
--example
select * from Patient where patient_id=134;
delete from Patient where patient_id=134;
--%rowtype
set serveroutput on
declare
doc_row DOCTOR%rowtype;
begin
select doctor_id,doctor_name,unit into doc_row.doctor_id,doc_row.doctor_name,doc_row.unit from DOCTOR where doctor_id=7;
dbms_output.put_line(doc_row.doctor_id || ' '|| doc_row.DOCTOR_NAME || ' '|| doc_row.UNIT);
end;
/
--cursor
--1
set serveroutput on
declare
p_var PATIENT%Rowtype;
cursor C is select * from PATIENT where age>20;
begin
open C;
fetch C into p_var.PATIENT_ID,p_var.patient_name,p_var.email,p_var.PHONE_NO,p_var.AGE,p_var.GENDER;
while C%FOUND loop
fetch C into p_var.PATIENT_ID,p_var.patient_name,p_var.email,p_var.PHONE_NO,p_var.AGE,p_var.GENDER;
dbms_output.put_line(p_var.patient_id || ' '|| p_var.patient_name );
end loop;
close C;
end;
/
--2: rowcount
set serveroutput on
declare
cursor C is select * from medical_history;
var medical_history%Rowtype;
i int :=0;
begin
open C;
fetch C into var.patient_id, var.condition,var.treatment_history;
while C%FOUND
loop
--step by step rows
dbms_output.put_line('row : ' || C%rowcount);
fetch C into var.patient_id, var.condition,var.treatment_history;
end loop;
-- Total row
dbms_output.put_line('Total row : ' || C%rowcount);
close C;
end;
/
--Varray
set serveroutput on
declare
type namelist is Varray(5) of DOCTOR.DOCTOR_NAME%type;
counter int;
names DOCTOR.DOCTOR_NAME%type;
namearray namelist :=namelist();
begin
counter :=1;
for x in 3..6 loop
select doctor_name into names from DOCTOR where doctor_id=x;
namearray.extend();
namearray (counter) :=names;
counter := counter +1;
end loop;
counter :=1;
while counter <=namearray.count
loop
DBMS_OUTPUT.PUT_LINE(namearray(counter));
counter:=counter+1;
end loop;
end;
/
--Varray without extend()
set serveroutput on
declare
type namelist is Varray(5) of DOCTOR.DOCTOR_NAME%type;
counter int;
names DOCTOR.DOCTOR_NAME%type;
namearray namelist := namelist('name1','name2','name3','name4');
begin
counter :=1;
for x in 4..7 loop
select doctor_name into names from DOCTOR where doctor_id=x;
namearray (counter) :=names;
counter := counter +1;
end loop;
counter :=1;
while counter <=namearray.count
loop
DBMS_OUTPUT.PUT_LINE(namearray(counter));
counter:=counter+1;
end loop;
end;
/
--conditional statements
set serveroutput on
DECLARE
working_unit DOCTOR.UNIT%type;
BEGIN
FOR x IN 1..18
LOOP
SELECT unit INTO working_unit FROM doctor WHERE doctor_id=x;
if working_unit='burn'
then
dbms_output.put_line(' works in burn unit ');
elsif working_unit='surgery'
then
dbms_output.put_line(' This doctor is a surgeon ');
else
dbms_output.put_line(' other ');
end if;
END LOOP;
END;
--procedure
create or replace procedure Find_Doc(pat_name in varchar,doc_name out varchar) as
text varchar(30);
--doc_name DOCTOR.DOCTOR_NAME%type;
begin
select doctor_name into doc_name from DOCTOR where doctor_id = (select doctor_id from ADMITTED_PATIENTS where patient_id = (select patient_id from Patient where patient_name=pat_name));
text:='procedure result : ';
dbms_output.put_line(text || doc_name);
end;
/
set serveroutput on
declare
pat_name PATIENT.PATIENT_NAME%type := 'Shimu';
doc_name DOCTOR.DOCTOR_NAME%type;
begin
find_doc(pat_name,doc_name);
end;
/
--using SYS_REFCURSOR
--given a patient name find the name of the doctor in charge
set serveroutput on
--multiple row output
CREATE OR REPLACE PROCEDURE Find_Doc_many(pat_name IN VARCHAR2, doc_name OUT SYS_REFCURSOR) AS
text VARCHAR2(30);
BEGIN
OPEN doc_name FOR
SELECT doctor_name
FROM DOCTOR
WHERE doctor_id = (
SELECT doctor_id
FROM ADMITTED_PATIENTS
WHERE patient_id = (
SELECT patient_id
FROM Patient
WHERE patient_name = pat_name
)
);
DBMS_OUTPUT.PUT_LINE('Procedure result:');
LOOP
FETCH doc_name INTO text;
EXIT WHEN doc_name%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(text);
END LOOP;
CLOSE doc_name;
END;
/
set serveroutput on
DECLARE
pat_name PATIENT.PATIENT_NAME%TYPE := 'Shimu';
doc_name SYS_REFCURSOR;
BEGIN
Find_Doc_many(pat_name, doc_name);
END;
--Given a condition check if any currently admitted patient has history of this condition
set serveroutput on
Create or replace Procedure find_pat (condn in varchar, p_name out varchar,pat OUT SYS_REFCURSOR) AS
begin
OPEN pat for
select patient_name into p_name from PATIENT where patient_id in (select patient_id from ADMITTED_PATIENTS where patient_id in (
select patient_id from MEDICAL_HISTORY where condition=condn));
loop
fetch pat into p_name ;
EXIT WHEN pat%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('patient ' || p_name || ' has ' || condn );
END LOOP;
CLOSE pat;
END;
DECLARE
pat SYS_REFCURSOR;
p_name PATIENT.PATIENT_NAME%type;
BEGIN
find_pat('Stroke',p_name,pat);
END;
/
-- if any patient is admitted for more than 10 days print the patient's name
set serveroutput on
create OR replace procedure Days(d OUT SYS_REFCURSOR) AS
p_name PATIENT.PATIENT_NAME%type;
begin
open d for select patient_name into p_name from PATIENT where patient_id in (select patient_id from ADMITTED_PATIENTS where
release_date-admit_date>=10);
loop
fetch d into p_name ;
EXIT WHEN d%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('patient ' || p_name || ' has severe condition' );
END LOOP;
CLOSE d;
END;
DECLARE
d SYS_REFCURSOR;
BEGIN
Days(d);
END;
/
--function
create or replace function find_pat_name(var1 in int) return varchar AS
value patient.patient_name%type;
begin
select patient_name into value from patient where patient_id=var1;
return value;
end;
/
set serveroutput on
declare
value varchar(20);
begin
value:=find_pat_name(121);
dbms_output.put_line ('The Functional value : ' || value);
end;
/
--drop function or procedure
drop procedure Find_Doc;
drop function find_pat_name;
drop PROCEDURE Find_Doc_many;