Professional Documents
Culture Documents
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.
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;
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;
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)
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
MatVsSteamingMemUse.txt
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;