You are on page 1of 6

PLSQL

1. Invoker/Definer rights. 2. Pragma- compiler directives 3. Instead-of triggers 4. What is Forward declaration 5. Difference between host and bind variables 6. What are collections and different types of collections 7. If autonomous transaction is not committed what will happen. 8. What are table functions and pipelined table functions 9. About Forall exception handling. 10. Refcursor. 11. What is autonomous txn and when it is used. Explain with a scenario. 12. How do you debug PLSQL code? Is there any DBMS package available for it 13. How to debug plsql 14. DBMS_XML 15. Can we commit inside a trigger? 16.

What are invoker right definer rights, how to define (authid)


A routine stored in the database by default, is executed with the definer rights (owner of the routine), depending on the user who calls it. create or replace procedure pr_invker_definer authid definer is -- default option begin .... create or replace procedure pr_invker_definer authid CURRENT_USER is begin ....

What is pragma/What is compiler directive?


A>Pragma is a keyword in Oracle PL/SQL that is used to provide an instruction to the compiler so that it changes the regular behaviour for the particular procedure. Following pragmas are available: AUTONOMOUS_TRANSACTION :- Commits the current procedure alone. EXCEPTION_INIT :- The pragma EXCEPTION_INIT associates an userdefined exception name with an Oracle error number. You can intercept any ORAerror and write a specific handler for it instead of using the OTHERS handler. RESTRICT_REFERENCES :- To be callable from SQL statements, a stored function must obey certain purity rules, which control side-effects. If any SQL statement inside the function body violates a rule, you get an error at run time (when the statement is parsed). To check for violations of the rules at compile time, you can use the compiler directive PRAGMA RESTRICT_REFERENCES. With this pragma defined in the spec for the particular function the function compilation will fail if it violates any rule defined in the pragma. Following rules are available: RNDS Read No Data State(does not query database tables) RNPS - Read No Package State(does not reference package variables) WNDS Write No Data State WNPS Write No Package State TRUST - Asserts that the subprogram can be trusted not to violate one or more rules. Syntax- PRAGMA RESTRICT_REFERENCES(<function_name>, WNDS, WNPS); (In spec) SERIALLY_REUSABLE - Indicates that the package state is needed only for the duration of one call to the server (for example, a PL/SQL anonymous block). After this call, the storage for the package variables can be reused

Example: CREATE PACKAGE debugging AS PRAGMA SERIALLY_REUSABLE; FUNCTION log_msg (msg VARCHAR2) RETURN VARCHAR2; PRAGMA RESTRICT_REFERENCES(log_msg, WNDS, RNDS); END debugging; CREATE OR REPLACE PACKAGE BODY debugging AS FUNCTION log_msg (msg VARCHAR2) RETURN VARCHAR2 IS PRAGMA AUTONOMOUS_TRANSACTION; I_GIVE_UP EXCEPTION; PRAGMA EXCEPTION_INIT(I_give_up, -20000); -- Tells the compiler to attach error code -20000 to exception name I_give_up BEGIN -- the following insert does not violate the constraint -- WNDS because this is an autonomous routine INSERT INTO debug_output VALUES (msg); COMMIT; RETURN msg; END; END debugging;

Correlated sub-query, subquery, inline view


A> Inline- view When we user subquery in FROM clause its inline. Correlated subquery This subquery references at least one column of the outerquery. Subquery A query within a query.

Instead-of triggers
An INSTEAD OF trigger is a DML trigger created on a noneditioning view, or on a nested table column of a noneditioning view. The database fires the INSTEAD OF trigger instead of running the triggering DML statement. For more information, see "INSTEAD OF Triggers".

What is Cast function. Difference between cast and to_char, to_number etc,
A> CAST is a function used to convert 1 datatype to another. It can be used to convert complex datatypes as well like collections. Even for converting number to charcter it can be used if we want to mention precision as well. create table t as 2 select dummy 3 ,cast(null as number(5)) some_number 4 ,cast(null as varchar2(3)) some_varchar 5 from dual;

What is Forward declaration Difference between host and bind variables

What are collections and different types of collections


A>

Nested table Variable length and needs to be initialized before we can use. Also need to increase the cells using EXTEND before populating new cells.

DECLARE TYPE number_table_type IS TABLE OF NUMBER ; my_tab number_table_type ; BEGIN my_tab := number_table_type(); my_tab.EXTEND(2); my_tab(1) := 21; DBMS_OUTPUT.PUT_LINE('my_tab(1) is '||my_tab(1)); END; Varray Same as Nested table but will have maximum number of elements defined during declaration. TYPE phone_no_tab IS VARRAY(6) OF VARCHAR2(20); phone_nos phone_no_tab() ; Index by table or associative array Variable length array which can be used without initializing and can also have characters as index. TYPE phone_no_tab IS TABLE OF VARCHAR2 (20)INDEX BY BINARY_INTEGER; phone_nos phone_no_tab;

Difference index type fixed size declared spare index allowed store in database access individual elements in stored collection initialization required ordered array? (when stored in table)

Associate Array Number or String No Yes No n/a No n/a

Nested Table Number No Yes Yes Yes Yes No

VARRAY Number Yes No Yes No Yes Yes

Can we have order by in subquery


A>No

If autonomous transaction is not committed what will happen.


If any table data is changed during the autonomous transaction and if it is not committed or rollback back then transaction will be rolled back and exception is raised.

What are table functions and pipelined table functions


Table Functions Table functions are used to return PL/SQL collections that mimic tables. They can be queried like a regular table by using the TABLE function in the FROM clause. Regular table functions require collections to be fully populated before they are returned. As a technique for processing large volumes of data, "table functions" are limited due to their potentially large memory footprints.

In Pipeline Table Functions:


data is piped (or streamed) rather than fully materialised as with original table functions; Pipelined functions can be parallelised by Oracle. Can be based on user defined package types also. Table functions can only be based on types created at schema level ( as objects).

In a pipelined function, the PIPE ROW statement is responsible for "returning" data and not the RETURN statexment.
CREATE FUNCTION parallel_pipelined_function(cursor_in IN SYS_REFCURSOR -- Cursor variable is required for parallel execution only) RETURN all_source_ntt PIPELINED -- pipelined keyword to make function PARALLEL_ENABLE(PARTITION cursor_in BY ANY) -- To enable the function to execute in parallel mode. IS BEGIN LOOP FETCH cursor_in BULK COLLECT INTO v_incoming LIMIT 500; LOOP -- Some Processing PIPE ROW(v_outgoing); -- To return the rows immediately END LOOP; EXIT WHEN cursor_in%NOTFOUND; END LOOP; CLOSE cursor_in; RETURN; -- RETURN doesnt return anything even though its a function. PIPE ROW returns the value END parallel_pipelined_function;

streaming vs materialising(pipelined table function vs table function): a visual example pipelined table function - Rows are returned as available table function- Rows are returned only when all rows are available for display

MatVsSteaming.txt

streaming vs materialising: memory usage

MatVsSteamingMemUse.txt

About Forall exception handling.


-- create a temporary table for this example CREATE TABLE emp_temp AS SELECT * FROM employees; DECLARE TYPE empid_tab IS TABLE OF employees.employee_id%TYPE;

emp_sr empid_tab; -- create an exception handler for ORA-24381 errors NUMBER; dml_errors EXCEPTION; PRAGMA EXCEPTION_INIT(dml_errors, -24381); BEGIN SELECT employee_id BULK COLLECT INTO emp_sr FROM emp_temp WHERE hire_date < '30-DEC-94'; -- add '_SR' to the job_id of the most senior employees FORALL i IN emp_sr.FIRST..emp_sr.LAST SAVE EXCEPTIONS UPDATE emp_temp SET job_id = job_id || '_SR' WHERE emp_sr(i) = emp_temp.employee_id; -- If any errors occurred during the FORALL SAVE EXCEPTIONS, -- a single exception is raised when the statement completes. EXCEPTION WHEN dml_errors THEN -- Now we figure out what failed and why. errors := SQL%BULK_EXCEPTIONS.COUNT; DBMS_OUTPUT.PUT_LINE('Number of statements that failed: ' || errors); FOR i IN 1..errors LOOP DBMS_OUTPUT.PUT_LINE('Error #' || i || ' occurred during '|| 'iteration #' || SQL%BULK_EXCEPTIONS(i).ERROR_INDEX); DBMS_OUTPUT.PUT_LINE('Error message is ' || SQLERRM(-SQL%BULK_EXCEPTIONS(i).ERROR_CODE)); END LOOP; END; / DROP TABLE emp_temp;

How to debug plsql


DBMS_OUTPUT at different places. Writing debug information to OS files using utl_file dbms_debug - http://www.adp-gmbh.ch/ora/plsql/debug.html dbms_profiler - The dbms_profiler package can aid in PL/SQL debugging. The profiler gathers information at the PL/SQL virtual machine level. This information includes the total number of times each line has been executed, the total amount of time that has been spent executing that line, and the minimum and maximum times that have been spent on a particular execution of that line. Conditional Compilation - http://www.oracle-base.com/articles/10g/conditional-compilation10gr2.php Debugger available in Toad, plsql developer tools (step-in, step-out, step-over)

Can we commit inside a trigger


No, we will get below error. To avoid this error we can declare the trigger as pragma autonomous_transaction.

You might also like