-
Notifications
You must be signed in to change notification settings - Fork 0
/
9. PL SQL Cursors with Parameters (Code Samples).html
128 lines (125 loc) · 4.61 KB
/
9. PL SQL Cursors with Parameters (Code Samples).html
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
<pre class="prettyprint linenums">declare
cursor c_emps (p_dept_id number) is select first_name,last_name,department_name
from employees join departments using (department_id)
where department_id = p_dept_id;
v_emps c_emps%rowtype;
begin
open c_emps(20);
fetch c_emps into v_emps;
dbms_output.put_line('The employees in department of '|| v_emps.department_name|| ' are :');
close c_emps;
open c_emps(20);
loop
fetch c_emps into v_emps;
exit when c_emps%notfound;
dbms_output.put_line(v_emps.first_name|| ' ' ||v_emps.last_name);
end loop;
close c_emps;
end;
--------------- bind variables as parameters
declare
cursor c_emps (p_dept_id number) is select first_name,last_name,department_name
from employees join departments using (department_id)
where department_id = p_dept_id;
v_emps c_emps%rowtype;
begin
open c_emps(:b_emp);
fetch c_emps into v_emps;
dbms_output.put_line('The employees in department of '|| v_emps.department_name|| ' are :');
close c_emps;
open c_emps(:b_emp);
loop
fetch c_emps into v_emps;
exit when c_emps%notfound;
dbms_output.put_line(v_emps.first_name|| ' ' ||v_emps.last_name);
end loop;
close c_emps;
end;
---------------cursors with two different parameters
declare
cursor c_emps (p_dept_id number) is select first_name,last_name,department_name
from employees join departments using (department_id)
where department_id = p_dept_id;
v_emps c_emps%rowtype;
begin
open c_emps(:b_dept_id);
fetch c_emps into v_emps;
dbms_output.put_line('The employees in department of '|| v_emps.department_name|| ' are :');
close c_emps;
open c_emps(:b_dept_id);
loop
fetch c_emps into v_emps;
exit when c_emps%notfound;
dbms_output.put_line(v_emps.first_name|| ' ' ||v_emps.last_name);
end loop;
close c_emps;
open c_emps(:b_dept_id2);
fetch c_emps into v_emps;
dbms_output.put_line('The employees in department of '|| v_emps.department_name|| ' are :');
close c_emps;
open c_emps(:b_dept_id2);
loop
fetch c_emps into v_emps;
exit when c_emps%notfound;
dbms_output.put_line(v_emps.first_name|| ' ' ||v_emps.last_name);
end loop;
close c_emps;
end;
--------------- cursor with parameters - for in loops
declare
cursor c_emps (p_dept_id number) is select first_name,last_name,department_name
from employees join departments using (department_id)
where department_id = p_dept_id;
v_emps c_emps%rowtype;
begin
open c_emps(:b_dept_id);
fetch c_emps into v_emps;
dbms_output.put_line('The employees in department of '|| v_emps.department_name|| ' are :');
close c_emps;
open c_emps(:b_dept_id);
loop
fetch c_emps into v_emps;
exit when c_emps%notfound;
dbms_output.put_line(v_emps.first_name|| ' ' ||v_emps.last_name);
end loop;
close c_emps;
open c_emps(:b_dept_id2);
fetch c_emps into v_emps;
dbms_output.put_line('The employees in department of '|| v_emps.department_name|| ' are :');
close c_emps;
for i in c_emps(:b_dept_id2) loop
dbms_output.put_line(i.first_name|| ' ' ||i.last_name);
end loop;
end;
---------------cursors with multiple parameters
declare
cursor c_emps (p_dept_id number , p_job_id varchar2) is select first_name,last_name,job_id,department_name
from employees join departments using (department_id)
where department_id = p_dept_id
and job_id = p_job_id;
v_emps c_emps%rowtype;
begin
for i in c_emps(50,'ST_MAN') loop
dbms_output.put_line(i.first_name|| ' ' ||i.last_name|| ' - ' || i.job_id);
end loop;
dbms_output.put_line(' - ');
for i in c_emps(80,'SA_MAN') loop
dbms_output.put_line(i.first_name|| ' ' ||i.last_name|| ' - ' || i.job_id);
end loop;
end;
--------------- An error example of using parameter name with the column name
declare
cursor c_emps (p_dept_id number , job_id varchar2) is select first_name,last_name,job_id,department_name
from employees join departments using (department_id)
where department_id = p_dept_id
and job_id = job_id;
v_emps c_emps%rowtype;
begin
for i in c_emps(50,'ST_MAN') loop
dbms_output.put_line(i.first_name|| ' ' ||i.last_name|| ' - ' || i.job_id);
end loop;
dbms_output.put_line(' - ');
for i in c_emps(80,'SA_MAN') loop
dbms_output.put_line(i.first_name|| ' ' ||i.last_name|| ' - ' || i.job_id);
end loop;
end;</pre>