-
Notifications
You must be signed in to change notification settings - Fork 0
/
PL.sql
264 lines (217 loc) · 5.63 KB
/
PL.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
-- PL/SQL variable declaration and print value
SET serveroutput ON
DECLARE
id flight_details.flight_id%type;
dates flight_details.flight_departure_date%type;
price flight_details.price%type;
BEGIN
SELECT flight_id, flight_departure_date, price INTO id, dates, price
FROM flight_details
WHERE flight_id = 1 AND flight_departure_date = DATE '2023-04-12';
dbms_output.put_line('id = ' || id || ', date = ' || dates || ', price = ' || price);
END;
/
-- Insert and set default value
SET serveroutput ON
DECLARE
id flight_details.flight_id%type := 1;
dates flight_details.flight_departure_date%type := DATE '2023-04-15';
price flight_details.price%type := 6000;
BEGIN
INSERT INTO flight_details VALUES (id, dates, price);
END;
/
-- %ROWTYPE
SET serveroutput ON
DECLARE
flight flight_details%rowtype;
BEGIN
SELECT flight_id, flight_departure_date, price INTO flight.flight_id, flight.flight_departure_date, flight.price
FROM flight_details
WHERE flight_id = 1 AND flight_departure_date = DATE '2023-04-12';
dbms_output.put_line('id = ' || flight.flight_id || ', date = ' || flight.flight_departure_date || ', price = ' || flight.price);
END;
/
-- Insert and set default value using %ROWTYPE
SET serveroutput ON
DECLARE
flight flight_details%rowtype;
BEGIN
flight.flight_id := 2;
flight.flight_departure_date := DATE '2023-04-15';
flight.price := 5800;
INSERT INTO flight_details VALUES flight;
END;
/
-- Creating a cursor
SET serveroutput ON
DECLARE
CURSOR pass_cur IS
SELECT profile_id, f_name, l_name FROM passenger;
p_id passenger.profile_id%type;
f_name passenger.f_name%type;
l_name passenger.l_name%type;
BEGIN
OPEN pass_cur;
FETCH pass_cur INTO p_id, f_name, l_name;
WHILE pass_cur%FOUND LOOP
dbms_output.put_line(p_id || ' ' || f_name || ' ' || l_name);
FETCH pass_cur INTO p_id, f_name, l_name;
END LOOP;
CLOSE pass_cur;
END;
/
-- %ISOPEN, %FOUND, %ROWCOUNT, %NOTFOUND
SET serveroutput ON
DECLARE
CURSOR pass_cur IS
SELECT profile_id, f_name, l_name FROM passenger;
p_id passenger.profile_id%type;
f_name passenger.f_name%type;
l_name passenger.l_name%type;
BEGIN
OPEN pass_cur;
FETCH pass_cur INTO p_id, f_name, l_name;
WHILE pass_cur%FOUND LOOP
dbms_output.put_line(p_id || ' ' || f_name || ' ' || l_name);
FETCH pass_cur INTO p_id, f_name, l_name;
END LOOP;
IF (pass_cur%NOTFOUND) THEN
dbms_output.put_line('end');
ELSE
dbms_output.put_line('wrong');
END IF;
dbms_output.put_line('row ' || pass_cur%ROWCOUNT);
CLOSE pass_cur;
IF (pass_cur%ISOPEN) THEN
dbms_output.put_line('Cursor is open');
ELSE
dbms_output.put_line('Cursor is closed');
END IF;
END;
/
-- Procedure with parameter type IN
CREATE OR REPLACE PROCEDURE proc(id IN NUMBER) IS
name passenger.f_name%TYPE;
BEGIN
SELECT f_name INTO name FROM passenger WHERE passenger.profile_id = id;
dbms_output.put_line(name);
END;
/
SET serveroutput ON
DECLARE
var1 passenger.profile_id%TYPE := 111;
BEGIN
proc(var1);
END;
/
-- Procedure with parameter type OUT
CREATE OR REPLACE PROCEDURE proc(name OUT passenger.f_name%TYPE) IS
BEGIN
SELECT f_name INTO name FROM passenger WHERE passenger.profile_id = 111;
END;
/
SET serveroutput ON
DECLARE
name passenger.f_name%TYPE;
BEGIN
proc(name);
dbms_output.put_line(name);
END;
/
-- Procedure with parameter type IN and OUT
CREATE OR REPLACE PROCEDURE proc(id IN NUMBER, name OUT passenger.f_name%TYPE) IS
BEGIN
SELECT f_name INTO name FROM passenger WHERE passenger.profile_id = id;
END;
/
SET serveroutput ON
DECLARE
name passenger.f_name%TYPE;
id passenger.profile_id%TYPE;
BEGIN
id := 111;
proc(id, name);
dbms_output.put_line(name);
END;
/
-- Procedure with no parameters
CREATE OR REPLACE PROCEDURE proc IS
name passenger.f_name%TYPE;
BEGIN
SELECT f_name INTO name FROM passenger WHERE passenger.profile_id = 111;
dbms_output.put_line(name);
END;
/
SET serveroutput ON
DECLARE
BEGIN
proc();
END;
/
-- Procedure with parameter type IN OUT
CREATE OR REPLACE PROCEDURE proc(name IN passenger.f_name%TYPE, id IN OUT NUMBER) IS
aa NUMBER;
b NUMBER;
BEGIN
SELECT id INTO aa FROM passenger WHERE passenger.f_name = name;
dbms_output.put_line(aa);
b := aa + 10;
dbms_output.put_line(b);
UPDATE passenger SET profile_id = b WHERE profile_id = aa;
END;
/
SET serveroutput ON
DECLARE
name passenger.f_name%TYPE := 'lamisa';
id passenger.profile_id%TYPE;
BEGIN
proc(name, id);
END;
/
-- Error showing
SHOW ERRORS PROCEDURE proc;
-- Dropping a procedure
DROP PROCEDURE proc;
-- Function
CREATE OR REPLACE FUNCTION fun(idd IN NUMBER)
RETURN passenger.f_name%TYPE IS
f_name passenger.f_name%TYPE;
BEGIN
SELECT f_name INTO f_name FROM passenger WHERE passenger.profile_id = idd;
RETURN f_name;
END;
/
SET serveroutput ON
DECLARE
id passenger.profile_id%TYPE := 111;
BEGIN
dbms_output.put_line(fun(id));
END;
/
-- Error showing
SHOW ERRORS FUNCTION fun;
-- Dropping a function
DROP FUNCTION fun;
-- If/Else/Loop
SET serveroutput ON
DECLARE
CURSOR c IS
SELECT * FROM flight_details;
avrg NUMBER;
rec flight_details%ROWTYPE;
BEGIN
OPEN c;
FETCH c INTO rec;
SELECT AVG(price) INTO avrg FROM flight_details;
WHILE c%FOUND LOOP
IF rec.price < avrg THEN
dbms_output.put_line('Price is low');
ELSE
dbms_output.put_line('Price is high');
END IF;
FETCH c INTO rec;
END LOOP;
CLOSE c;
END;
/