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

Ref Cursor

------

DECLARE
TYPE ref_cur_eg IS REF CURSOR;
ref_emp_dept ref_cur_eg;
v_name VARCHAR2(30);
v_id NUMBER;
BEGIN
OPEN ref_emp_dept FOR SELECT
emp_name
FROM
xxemp_test;

dbms_output.put_line('EMP Name');
LOOP
FETCH ref_emp_dept INTO v_name;
EXIT WHEN ref_emp_dept%notfound;
dbms_output.put_line(v_name);
END LOOP;

CLOSE ref_emp_dept;
OPEN ref_emp_dept FOR SELECT
dept_id
FROM
xxemp_dept_test;

dbms_output.put_line('DEPT ID');
LOOP
FETCH ref_emp_dept INTO v_name;
EXIT WHEN ref_emp_dept%notfound;
dbms_output.put_line(v_name);
END LOOP;

CLOSE ref_emp_dept;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('error with update'
|| sqlcode
|| sqlerrm);
END;

PL/SQL Collections
----------

1. VARRAY

Declare
type v_array_type is varray (7)
of varchar2(30);

v_day v_array_type:= v_array_type ();


begin
v.day.extend (7);
end;

2. Nested Tables

Declare
type v_array_type is table
of varchar2(30);
v_day v_array_type:= v_array_type ();
begin
v.day.extend (7);
end;

3. Index By Table/Associative Array

Declare
type v_array_type is table
of varchar2(30) index by varchar2(20);
v_day v_array_type;
begin
null;
end;

Mutating Trigge
--------

Bull Collect & Exceptions


--------

Syntax

Declare
type nt_fname is table of varchar2 (20);
fname nt_Fname;
begin
select first_name bulk collect into fname
from emp;
for idx in 1..fname.count
loop
dbms_output.put_line (idx||''|| fname(idx));
end loop;
end;

----

declare
type t_emp_list is table of emp%rowtype;
lv_emp_list t_emp_list := t_emp_list();
begin

select * bulk collect into lv_emp_list


from emp;

forall i in lv_emp_list.first.. lv_emp_list.last


insert into emp_bkup values lv_emp_list(i);
end;

Save EXCEPTION

SQL%BULK_EXCEPTION
SQL%BULK_EXCEPTION.COUNT

SQL%BULK_EXCEPTION(i).ERROR_INDEX
SQL%BULK_EXCEPTION(i).ERROR_CODE

declare
type name_list is table of varchar2(100);
lv_name_list name_list := name_list;
begin
lv_namelist.extends (5);
lv_name_list (1):='AAA';
lv_name_list (2):='BBBBBBB';
lv_name_list (3):='CCCC';
lv_name_list (4):=NULL;
lv_name_list (5):='EEEEE';

forall i in lv_name_list.first.. lv_name_list.last save EXCEPTIONS


insert into student values(lv_name_list (i));
exception
when others then
dbms_output.Put_line ('No of Errors occured = '||sql%bulk_exceptions.count);
for i in 1..sql%bulk_exceptions.count loop
dbms_output.Put_line ('Errors Index= '||sql%bulk_exceptions(i).error_index);
dbms_output.Put_line ('Errors Code= '||sqlrrm (-sql
%bulk_exceptions(i).error_code));

end loop;
end;

Global Temp Table


-----
The ON COMMIT DELETE ROWS clause indicates the data should be deleted at the end of
the transaction, or the end of the session.

CREATE GLOBAL TEMPORARY TABLE my_temp_table (


id NUMBER,
description VARCHAR2(20)
)
ON COMMIT DELETE ROWS;

In contrast, the ON COMMIT PRESERVE ROWS clause indicates that rows should persist
beyond the end of the transaction. They will only be removed at the end of the
session.

CREATE GLOBAL TEMPORARY TABLE my_temp_table (


id NUMBER,
description VARCHAR2(20)
)
ON COMMIT PRESERVE ROWS;

Materialized View
------

Create materialized view test_view as select * from table;

Create materialized view view_name


build (Immediate/Deferred)
refresh (Fast/Complete/force)
on Commit/Demand
With primary key (rowid)
as select

dbms_mview.refresh(‘mview_name’, method => ‘C’);

MVIEW Refresh

1. Complete Refresh is performed by deleting the rows from the snapshot and
inserting the
Rows satisfying the MVIEW query.
2. In Fast refresh only the rows updated since last refresh are pulled from the
master table to
Insert into MVIEW.
* This requires a log table called as MVIEW Log to be created on the Master Table.
3. Force refresh first tries to run a Fast refresh if possible.If fast refresh is
not possible, it performs complete refresh.

No Copy Hint
----
https://www.complexsql.com/what-is-nocopy-hint-with-real-examples-nocopy-hint-
examples/

You might also like