Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

--select using cursor

set serveroutput on
declare
cursor c_emp_cursor is
select employee_id,last_name from employees
where department_id=&dep_id;
v_empno employees.employee_id%type;
v_lname employees.last_name%type;
begin
open c_emp_cursor;
loop
fetch c_emp_cursor into v_empno,v_lname;
exit when c_emp_cursor%notfound;
dbms_output.put_line(v_empno||' '||v_lname);
end loop;
end;
---------------Inserting using cursor (Create table - t1)
declare
cursor c_emp_cursor is
select employee_id,last_name from employees
where department_id=&dep_id;
v_empno employees.employee_id%type;
v_lname employees.last_name%type;
begin
open c_emp_cursor;
loop
fetch c_emp_cursor into v_empno,v_lname;
exit when c_emp_cursor%notfound;
insert into t1 values(v_empno,v_lname);
end loop;
end;
----------------------Implicit Cursor
BEGIN
UPDATE employees SET last_name='willes'
WHERE employee_id=200;
IF SQL%FOUND THEN
dbms_output.put_line('Updated - If Found');
END IF;
IF SQL%NOTFOUND THEN
dbms_output.put_line('NOT Updated - If NOT Found');
END IF;
IF SQL%ROWCOUNT>0 THEN
dbms_output.put_line(SQL%ROWCOUNT||' Rows Updated');
ELSE
dbms_output.put_line('NO Rows Updated Found');
END IF;
END;
-------------------Simple Procedure
create or replace procedure p_add

(a in number,
b in number,
c out number) is
begin
c:=a+b;
end p_add;
declare
c number;
begin
P_ADD(5,6,C);
DBMS_OUTPUT.PUT_LINE('RESULT'||' '||C);
end;
--------------------------------------------------------Cursor within Procedure
create or replace procedure pro is
cursor c1 is
select first_name from employees;
c_name c1%rowtype;
begin
open c1;
loop
fetch c1 into c_name;
dbms_output.put_line(c_name.first_name);
exit when c1%notfound;
end loop;
close c1;
end;
------------------------------------------Cursor using For, Associative Array, Joins
declare
TYPE emp_table IS TABLE OF
employees%ROWTYPE INDEX BY PLS_INTEGER;
TYPE job_table IS TABLE OF
jobs%ROWTYPE INDEX BY PLS_INTEGER;
TYPE dept_table IS TABLE OF
departments%ROWTYPE INDEX BY PLS_INTEGER;
my_emp_table emp_table;
my_job_table job_table;
my_dept_table dept_table;
i BINARY_INTEGER :=0;
cursor cur is
select e.employee_id,e.first_name,e.job_id,j.job_title,e.department_id,d.departm
ent_name from employees e, jobs j, departments d
where e.department_id=d.department_id and e.job_id=j.job_id;
BEGIN
FOR rec IN cur
LOOP
i := i +1;
my_emp_table(i).employee_id := rec.employee_id;
my_emp_table(i).first_name := rec.first_name;
my_emp_table(i).job_id :=rec.job_id;
my_job_table(i).job_title :=rec.job_title;
my_dept_table(i).department_id := rec.department_id;

my_dept_table(i).department_name := rec.department_name;
DBMS_OUTPUT.PUT_LINE ('Employee: ' || my_emp_table(i).employee_id||' ' ||my_emp_
table(i).first_name||' '||my_emp_table(i).job_id||' '||
my_job_table(i).job_title||' '||my_dept_table(i).department_id||' '||my_dept_tab
le(i).department_name);
exit when cur%notfound;
END LOOP;
END;
-------------------------------------- Save Exception in Bulk Collect or ForAll-- Need To Try--DECLARE
TYPE NumList
IS
TABLE OF NUMBER;
num_tab NumList := NumList(100,0,110,0,0,199,200,0,NULL);
bulk_errors EXCEPTION;
PRAGMA EXCEPTION_INIT (bulk_errors, -24381 );
BEGIN
FORALL i IN num_tab.FIRST..num_tab.LAST
SAVE EXCEPTIONS
DELETE FROM orders WHERE order_total < 500000/num_tab(i);
EXCEPTION
WHEN bulk_errors THEN
DBMS_OUTPUT.PUT_LINE('Number of errors is: ' || SQL%BULK_EXCEPTIONS.COUNT);
FOR j IN 1..SQL%BULK_EXCEPTIONS.COUNT
LOOP
DBMS_OUTPUT.PUT_LINE ( TO_CHAR(SQL%BULK_EXCEPTIONS(j).error_index) || ' / '
|| SQLERRM(-SQL%BULK_EXCEPTIONS(j).error_code) );
END LOOP;
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Number of errors is: ' || SQL%BULK_EXCEPTIONS.COUNT);
END;
/
----------------Question:
-------------Use array and cursor to fetch and print employee id, name, job and department de
tails
--------------------------------------------------------------------------------------Solution:
===========
declare
cursor c1 is select
e.employee_id, e.first_name || ' ' || e.last_name employee_name,
j.job_id, j.job_title,
d.department_id, d.department_name
from employees e, jobs j, departments d
where e.department_id = d.department_id
and e.job_id = j.job_id;
type emp_arr_type is table of c1%rowtype index by pls_integer;
emp_arr emp_arr_type;
begin
for emp_rec in c1
loop
emp_arr(emp_rec.employee_id).employee_id := emp_rec.employee_id;

emp_arr(emp_rec.employee_id).employee_name := emp_rec.employee_name;
emp_arr(emp_rec.employee_id).job_id := emp_rec.job_id;
emp_arr(emp_rec.employee_id).job_title := emp_rec.job_title;
emp_arr(emp_rec.employee_id).department_id := emp_rec.department_id;
emp_arr(emp_rec.employee_id).department_name := emp_rec.department_name;
--dbms_output.put_line(emp_rec.employee_id);
end loop;
dbms_output.put_line(emp_arr.first || ' - ' || emp_arr.last);
for i in emp_arr.first..emp_arr.last
loop
IF emp_arr.exists(i) then
dbms_output.put(emp_arr(i).employee_id|| ' Name ' || emp_arr(i).employee_n
ame);
dbms_output.put( ' ' || emp_arr(i).job_id || ' Title ' || emp_arr(i).job_t
itle);
dbms_output.put_line(' ' || emp_arr(i).department_id|| ' Dept Name ' || em
p_arr(i).department_name);
end if;
end loop;
end;
---------------------------(OR) - Same as Above with different logic
Use array and cursor to fetch and print employee id, name, job and department de
tails
--------------------------------------------------------------------------------------Solution:
===========
declare
cursor c1 is select
e.employee_id, e.first_name || ' ' || e.last_name employee_name,
j.job_id, j.job_title,
d.department_id, d.department_name
from employees e, jobs j, departments d
where e.department_id = d.department_id
and e.job_id = j.job_id;
type emp_arr_type is table of c1%rowtype index by pls_integer;
emp_arr emp_arr_type;
begin
for emp_rec in c1
loop
emp_arr(emp_rec.employee_id):=emp_rec; -- Instead of assinging separate co
lumn's - Assing the curror to it-end loop;
dbms_output.put_line(emp_arr.first || ' - ' || emp_arr.last);
for i in emp_arr.first..emp_arr.last
loop
IF emp_arr.exists(i) then
dbms_output.put(emp_arr(i).employee_id|| ' Name ' || emp_arr(i).employee_n
ame);
dbms_output.put( ' ' || emp_arr(i).job_id || ' Title ' || emp_arr(i).job_t
itle);
dbms_output.put_line(' ' || emp_arr(i).department_id|| ' Dept Name ' || em
p_arr(i).department_name);
end if;
end loop;

end;
------------------------------------nested procedure
========================
set serveroutput on
create or replace procedure main_proc
is
v_test number:=4;
procedure inner_proc1
is
begin
dbms_output.put_line(v_test||' inner procedure one');
end inner_proc1;
procedure inner_proc2
is
begin
dbms_output.put_line(v_test||' inner procedure two');
end inner_proc2;
begin
while v_test <=10 loop
inner_proc1;
if mod(v_test,2)=0 then
inner_proc2;
end if;
v_test:=v_test+1;
end loop;
end main_proc;
/
begin
main_proc;
end;
/
----------------------------------------------Procedure within function-===================================
set serveroutput on
declare
procedure p1 (
i1 in out number
)
as
begin
i1 := 200;
end p1;
function f1
return number
is
l1 number;
begin
p1(l1);
return l1;
end f1;
begin
dbms_output.put_line(f1);
end;
/

---------------------------

You might also like