Professional Documents
Culture Documents
Pragma
A pragma is a directive to the PL/SQL compiler. Pragmas pass information to the compiler; they are
processed at compile time but do not execute. If you include a call to a built-in package in a SQL
statement, you must include a RESTRICT REFERENCES pragma in your code. This pragma tells the
compiler the purity level (freedom from side effects) of a packaged program.
Pragma keyword is used to give instructions to the compiler. There are 4 types of Pragmas:
A) Exception_Init: - Tells the compiler to associate the specified error number with an identifier
that has been declared an Exception in your current program or an accessible package.
B) Restrict_References: - Tells the compiler the purity level of packaged program. The purity level
is the degree to which a program does not read/write database tables and/or packaged variables.
C) Serially_Reusable: - Tells the runtime engine that package data should not persist between
references. This is used to reduce per-user memory requirements when the package data is only
needed for duration of call and not the duration of session.
D) Autonomous_Transactions: - Tells the compiler that the function, procedure, top-level
anonymous P/L SQL block, object method, or database trigger executes in its own transaction
space.
A materialized view is a database object that contains the results of a query. They are local copies of data
located remotely, or are used to create summary tables based on aggregations of a table's data. Materialized
views, which store data based on remote tables are also, know as snapshots.
The following statement creates the primary-key materialized view on the table emp located on a remote
database.
SQL> CREATE MATERIALIZED VIEW mv_emp_pk
REFRESH FAST START WITH SYSDATE
NEXT SYSDATE + 1/48
WITH PRIMARY KEY
AS SELECT * FROM emp@remote_db;
2. Top Nth Salary
Select min (sal) from EMP a
Where &N > (select count (distinct sal) from emp b where a.sal < b.sal)
Order by sal desc;
)
What are object group?
An object group is a container for a group of objects. You define an object group when you want to
package related objects so you can copy or reference them in another module
Reading.
in_file text_io.file_type;
in_file := text_io.fopen(Rtrim(:control.imp_file),'R');
loop
begin
text_io.get_line(in_file, linebuf);
if ltrim(rtrim(linebuf)) is null then exit
end;
mbenef_accno := to_number(substr(mclient_code,9,8));
text_io.fclose(in_file);
Writing
declare
outfile text_io.file_type;
filename varchar2(100);
str varchar2(100);
cursor c1 is select empno,ename from emp;
begin
filename:='d:\rashid.doc';
outfile:=text_io.fopen(filename,'w');
str:='Empno'||','||'Ename';
text_io.put_line(outfile,str);
str:='';
for i in c1
loop
str:=i.empno||','||i.ename;
text_io.put_line(outfile,str);
END LOOP;
text_io.fclose(outfile);
exception
when others then
text_io.fclose(outfile);
end;
3. Varray/Nested Table (Difference)
Varrays
An array is an ordered set of data elements.
All elements of a given array are of the same datatype.
Each element has an index, which is a number corresponding to the element's position
in the array.
specify a maximum size when you declare the array type.
CREATE OR REPLACE TYPE vcarray AS VARRAY(10) OF VARCHAR2(128);
CREATE TABLE varray_table (id number, col1 vcarray);
NSERT INTO varray_table VALUES (1, vcarray('A'));
COMMIT;
SELECT t1.id, t2.column_value
FROM varray_table t1, TABLE(t1.col1) t2;
Nested Tables
A nested table is an unordered set of data elements, all of the same datatype. It has a single
column, and the type of that column is a built-in type or an object type. If an object type, the
table can also be viewed as a multi-column table, with a column for each attribute of the
object type.
A table type definition does not allocate space. It defines a type, which you can use as
CREATE OR REPLACE TYPE CourseList AS TABLE OF VARCHAR2(64);
CREATE TABLE department (
name VARCHAR2(20),
director VARCHAR2(20),
office VARCHAR2(20),
courses CourseList)
NESTED TABLE courses STORE AS courses_tab
RETURN types.ref_cursor;
END;
TABLE my_temp_table (
Global Temproary Table.
The data in a global temporary table is private, such that data inserted by a session can only be accessed by
that session. The session-specific rows in a global temporary table can be preserved for the whole session, or
just for the current transaction. The ON COMMIT DELETE ROWS clause indicates that the data should be
deleted at the end of the transaction.
column1 NUMBER,
column2 NUMBER
) ON COMMIT DELETE ROWS;
In contrast, the ON COMMIT PRESERVE ROWS clause indicates that rows should be preserved until the end of
the In contrast, the ON COMMIT PRESERVE ROWS clause indicates that rows should be preserved until the
end of the session.
Trigger will get created without any error. But whenever we update test table, we run into
following error.
Now let us create same trigger with pragma autonomous_transaction and execute the update
statement again.
COMMIT;
END;
BULK COLLECT
I have started using BULK COLLECT whenever I need to fetch large volumes of data. This has caused me
some trouble with my DBA, however. He is complaining that although my programs might be running much
faster
DECLARE
TYPE prod_id_tab is TABLE OF products.product_id%TYPE;
TYPE prod_tab IS TABLE OF products%ROWTYPE;
products_id_tab prod_id_tab := prod_id_tab();
products_tab prod_tab := prod_tab();
start_time number;
end_time number;
BEGIN
-- Populate a collection - 10000 rows
FOR i in 1 .. 10000 LOOP
products_id_tab.extend;
products_id_tab(products_id_tab.last) := i+10;
products_tab.extend;
products_tab(products_tab.last).product_id := i;
END LOOP;
Start_time := DBMS_UTILITY.get_time;
FOR i in products_tab.first .. products_tab.last LOOP
UPDATE products SET ROW = products_tab(i) -- ROW available in 9.2
WHERE product_id = products_tab(i).product_id;
END LOOP;
end_time := DBMS_UTILITY.get_time;
DBMS_OUTPUT.PUT_LINE('Conventional Update: '||to_char(end_time-start_time));
Start_time := DBMS_UTILITY.get_time;
FORALL i in products_tab.first .. products_tab.last
UPDATE products SET ROW = products_tab(i) – ROW available in 9.2
WHERE product_id = products_id_tab(i);
end_time := DBMS_UTILITY.get_time;
DBMS_OUTPUT.PUT_LINE('Bulk Update: '||to_char(end_time-start_time));
END;
Data may be Bulk Collected/Fetched into:
Table.column%TYPE
Record of arrays
Table%ROWTYPE
Cursor%ROWTYPE
Array of records
Nested tables
BULK COLLECT – Explicit Cursor- LIMIT
DECLARE
TYPE prod_tab IS TABLE OF products%ROWTYPE;
products_tab prod_tab := prod_tab();
start_time number;
end_time number;
CURSOR products_data IS SELECT * FROM products;
BEGIN
Start_time := DBMS_UTILITY.get_time;
OPEN products_data;
LOOP
FETCH products_data BULK COLLECT INTO products_tab LIMIT 10000;
EXIT WHEN products_data%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Processed '||to_char(products_tab.count)||' rows');
END LOOP;
CLOSE products_data;
end_time := DBMS_UTILITY.get_time;
DBMS_OUTPUT.PUT_LINE('Bulk Collect: ‘||to_char(end_time-start_time));
Reading.
declare
infile text_io.file_type;
linebuf varchar2(5000);
begin
in_file := text_io.fopen(Rtrim(:control.imp_file),'R');
loop
text_io.get_line(in_file, linebuf);
end loop;
text_io.`(in_file);
end;
Miscellaneous Features
• If the TRUNCATE statement is issued against a temporary table, only the session specific
data is trucated. There is no affect on the data of other sessions.
• Data in temporary tables is automatically delete at the end of the database session, even if
it ends abnormally.
• Indexes can be created on temporary tables. The content of the index and the scope of the
index is that same as the database session.
• Views can be created against temporary tables and combinations of temporary and
permanent tables.
• Temporary tables can have triggers associated with them.
• Export and Import utilities can be used to transfer the table definitions, but no data rows
are processed.
• There are a number of restrictions related to temporary tables but these are version
specific.
----------
Form
Answer: How to get around the "can't use a restricted built-in in built-in XXX" message:
1. Create a TIMER at the point where you want the navigation to
occur.Eg.create_timer('TIMER_X', 5, NO_REPEAT);
2. Code a WHEN-TIMER-EXPIRED trigger to handle the navigation
DECLARE
tm_name VARCHAR2(20);
BEGIN
tm_name := Get_Application_Property(TIMER_NAME);
IF tm_name = 'TIMER_X' THEN
Go_Item('ITEM_X');
END IF;
END;
Delete all rowids that is BIGGER than the SMALLEST rowid value (for a given key):