-
Notifications
You must be signed in to change notification settings - Fork 0
/
PLSQL 4.GUN.sql
executable file
·325 lines (243 loc) · 6.07 KB
/
PLSQL 4.GUN.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
select text from all_source
where lower(text) like '%function%to_char%'
and name='STANDARD';
/
create or replace package test_pkg
is
v_emp_id number :=100;
type emp_id_arr is table of number index by pls_integer;
type emp_arr is table of employees%rowtype;
cursor emp_cur is select * from employees
where manager_id=100;
procedure print_nth_sal(p_nth number);
procedure print_sal(p_data in out number);
procedure t_proc(n number);
function get_nth_sal (p_nth number,p_dept_id number default -1)
return number;
end;
/
create or replace package body test_pkg
is
v_date date default sysdate;
procedure print_nth_sal(p_nth number)
is
cursor emp_cur is select * from employees order by salary desc;
begin
for i in emp_cur loop
if emp_cur%rowcount=p_nth then
yaz(i.last_name ||' '||i.salary);
exit;
end if;
end loop;
end;
procedure print_sal(p_data in out number)
is
begin
select salary into p_data from employees where employee_id=p_data;
end;
procedure t_proc(n number)
is
begin
if n > 500 then
return;
end if;
yaz('DENEME');
end;
function get_nth_sal (p_nth number,p_dept_id number default -1)
return number
is
start_cputime number;
end_cputime number;
cursor emp_cur is select salary from employees
where department_id=p_dept_id
or p_dept_id=-1
order by salary desc;
type emp_cur_arr is table of emp_cur%rowtype;
v_emp_arr emp_cur_arr;
begin
start_cputime:=sys.dbms_utility.get_cpu_time;
open emp_cur;
fetch emp_cur bulk collect into v_emp_arr;
close emp_cur;
if v_emp_arr.exists(p_nth) then
end_cputime:=sys.dbms_utility.get_cpu_time;
yaz((end_cputime-start_cputime));
return v_emp_arr(p_nth).salary;
else
end_cputime:=sys.dbms_utility.get_cpu_time;
yaz((end_cputime-start_cputime));
return null;
end if;
end;
end;
/
select test_pkg.get_nth_sal(5)
from dual;
begin
select employee_id
into test_pkg.v_emp_id
from employees
where last_name ='Abel';
yaz(test_pkg.v_emp_id);
end;
/
begin
yaz(test_pkg.v_emp_id);
end;
/
create or replace package dept_manage_pkg
is
procedure add_dept (p_dept_id number,
p_dept_name varchar2,
p_loc_id number);
procedure add_dept (p_dept_name varchar2,
p_loc_id number);
end;
/
create or replace package body dept_manage_pkg
is
------- ILLEGAL REF ------
procedure yaz( x varchar2);
--------------------------
procedure add_dept (p_dept_id number,
p_dept_name varchar2,
p_loc_id number)
is
begin
insert into departments(department_id,department_name,location_id)
values (p_dept_id,p_dept_name,p_loc_id);
commit;
yaz('1 row inserted');
end;
procedure add_dept (p_dept_name varchar2,
p_loc_id number)
is
begin
insert into departments(department_id,
department_name,
location_id)
values (departments_seq.nextval,
p_dept_name,
p_loc_id);
commit;
yaz('1 row inserted');
end;
procedure yaz( x varchar2)
is
begin
dbms_output.put_line(x);
end;
begin
dbms_output.put_line('Bu paket ilk defa memory ye yüklenirtken
ilk bu blok parças? çal???r');
end;
/
begin
dept_manage_pkg.add_dept('test2',1800);
end;
-- WITH ADMIN USER
create directory myfolder_dir as 'C:\myfolder';
grant read,write on directory myfolder_dir to hr;
----------------------------
declare
f_file utl_file.file_type;
cursor emp_cur is select * from employees;
begin
f_file:=utl_file.fopen('MYFOLDER_DIR','rapor.csv','A');
for i in emp_cur loop
utl_file.put_line(f_file,i.last_name||','||i.salary||','||i.hire_date);
end loop;
utl_file.fclose(f_file);
end;
/
--- DYNAMIC SQL ---
begin
execute immediate 'create table abc(n number)';
end;
/
create or replace procedure print_data(p_emp_id number,
p_col_name varchar2)
is
v_data varchar2(50);
v_stmt varchar2(5000);
begin
v_stmt:= 'select '||p_col_name||' from employees
where employee_id='||p_emp_id;
execute immediate v_stmt into v_data;
yaz(v_stmt);
yaz(v_data);
end;
/
begin
print_data(100,'username from all_users where rownum=1 union all select salary');
end;
select username from all_users;
create or replace
procedure print_data(p_cond varchar2,
p_col_name varchar2,
p_tname varchar2)
is
type data_arr is table of varchar2(500);
v_data data_arr;
v_stmt varchar2(5000);
begin
v_stmt:= 'select '||p_col_name||' from '||p_tname||
' where '||p_cond;
execute immediate v_stmt bulk collect into v_data;
yaz(v_stmt);
for i in v_data.first..v_data.last loop
yaz(v_data(i));
end loop;
end;
/
begin
print_data('department_id>150','department_name','departments');
end;
/
begin
print_data('1=1','username','all_users');
end;
/
create or replace
procedure print_data(p_cond varchar2,
p_col_name varchar2,
p_tname varchar2)
is
type data_arr is table of varchar2(500);
v_data data_arr;
v_stmt varchar2(5000);
begin
v_stmt:= 'select '||p_col_name||' from '||dbms_assert.simple_sqlname(p_tname)||
' where employee_id=:deger';
execute immediate v_stmt bulk collect into v_data using p_cond;
yaz(v_stmt);
for i in v_data.first..v_data.last loop
yaz(v_data(i));
end loop;
end;
/
begin
print_data('0 union all select table_name from all_tables','last_name','employees');
end;
/
begin
sys.change_password('sys','hr');
end;
select * from abc;
delete from abc;
commit;
begin
insert into abc values (1);
test_proc;
insert into abc values (4);
rollback;
end;
/
create or replace procedure test_proc
is
pragma autonomous_transaction;
begin
insert into abc values (2);
insert into abc values (3);
commit;
end;