PL/SQL FAQ - Oracle FAQ

http://www.orafaq.com/wiki/PL/SQL_FAQ

PL/SQL FAQ
From Oracle FAQ
PL/SQL FAQ - Oracle's Procedural Language extension to SQL:

Contents
1 What is PL/SQL and what is it used for? 2 What is the difference between SQL and PL/SQL? 3 Should one use PL/SQL or Java to code procedures and triggers? 4 How can one see if somebody modified any code? 5 How can one search PL/SQL code for a string/ key value? 6 How does one keep a history of PL/SQL code changes? 7 How can I protect my PL/SQL source code? 8 How can I know which stored PL/SQL code is wrapped? 9 Can one print to the screen from PL/SQL? 10 Can one read/write files from PL/SQL? 11 Can one call DDL statements from PL/SQL? 12 Can one use dynamic SQL statements from PL/SQL? 13 What is the difference between %TYPE and %ROWTYPE? 14 How does one get the value of a sequence into a PL/SQL variable? 15 Can one execute an operating system command from PL/SQL? 16 How does one loop through tables in PL/SQL? 17 How often should one COMMIT in a PL/SQL loop? / What is the best commit strategy? 18 I can SELECT from SQL*Plus but not from PL/SQL. What is wrong? 19 What is a mutating and constraining table? 20 Can one pass an object/table as an argument to a remote procedure? 21 What is the difference between stored procedures and functions? 22 Is there a PL/SQL Engine in SQL*Plus? 23 Is there a limit on the size of a PL/SQL block? 24 What are the PL/SQL compiler limits for block, record, subquery and label nesting? 25 Can one COMMIT/ ROLLBACK from within a trigger?

What is PL/SQL and what is it used for?
SQL is a declarative language that allows database programmers to write a SQL declaration and hand it to the database for execution. As such, SQL cannot be used to execute procedural code with conditional, iterative and

1 of 15

30/05/2011 14:49

.... PL/SQL is commonly used to write data-centric programs to manipulate data in an Oracle database. However. it has one thing that other programming languages don't have: the ability to easily integrate with SQL.. END LOOP. manipulate objects (DDL) and data (DML) with SQL. PL/SQL's language syntax.. END LOOP. PL/SQL is a normal programming language that includes all the features of most other programming languages..column1 || '....... NULL.column2)...orafaq....PUT_LINE('Column 1 = ' || cursor1. LOOP . END LOOP. END IF. information hiding (all but inheritance).THEN.A PL/SQL cursor FOR cursor1 IN (SELECT * FROM table1) -. function overloading. Sequential Control Statements: GOTO . Column 2 = ' || cursor1.ELSE statements. END LOOP.. END CASE. You can write queries (SELECT)..This is an embedded SQL statement LOOP DBMS_OUTPUT..Oracle FAQ http://www.PUT_LINE('Hello World').. / What is the difference between SQL and PL/SQL? Both SQL and PL/SQL are languages used to access data within Oracle databases. PL/SQL was created. SQL doesn't include all the things that normal programming languages have. END. SQL is a limited language that allows you to directly interact with the database. structure and data types are similar to that of Ada.. To overcome this limitation.. THEN .com/wiki/PL/SQL_FAQ sequential statements.. ELSE . WHILE . such as loops and IF.PL/SQL FAQ .. WHEN . ELSIF . THEN . LOOP . CASE .. END. But.... FOR . / BEGIN -. Some of the statements provided by PL/SQL: Conditional Control Statements: IF . PL/SQL is Oracle's Procedural Language extension to SQL. The PL/SQL language includes object oriented programming techniques such as encapsulation. IN [REVERSE] . Example PL/SQL blocks: /* Remember to SET SERVEROUTPUT ON to see the output */ BEGIN DBMS_OUTPUT... 2 of 15 30/05/2011 14:49 ... ELSE .. Iterative Statements: LOOP .

procedures and triggers Data centric and tightly integrated into the database Proprietary to Oracle and difficult to port to other database systems Data manipulation is slightly faster in PL/SQL than in Java PL/SQL is a traditional procedural programming language Java: Can be used to create Oracle packages. procedures and triggers Open standard. How can one see if somebody modified any code? The source code for stored procedures. functions and packages are stored in the Oracle Data Dictionary. triggers. Nevertheless. it is still considered a best practice to put as much of your program logic as possible into packages. not how to do it. SQL is used to code queries. However. DML and DDL statements. PL/SQL is executed as a block of code. Oracle 9i supports native compilation of Pl/SQL code to binaries. . functions. Not to mention the numerous PL/SQL enhancements made in Oracle 10g and 11g.NET procedures can also be stored within the database (Windows only). procedures and packages. This often leads to questions like "Which of the two is the best?" and "Will Oracle ever desupport PL/SQL in favour of Java?".Oracle FAQ http://www. In more recent releases both triggers and procedures are compiled when created (stored p-code) and one can add as much code as one likes in either procedures or triggers.NET code is not usable on non-Windows systems. but you cannot embed PL/SQL within a SQL statement. Should one use PL/SQL or Java to code procedures and triggers? Both PL/SQL and Java can be used to create Oracle stored procedures and triggers. Many enhancements are still being made to PL/SQL. For example. SQL tells the database what to do (declarative). all indications are that PL/SQL still has a bright future ahead of it.orafaq. and modules are structured into classes Java can be used to produce complete applications PS: Starting with Oracle 10g. PL/SQL is used to code program blocks. In contrast. unlike PL/SQL and JAVA. You can embed SQL in a PL/SQL program. Many Oracle applications are based on PL/SQL and it would be difficult of Oracle to ever desupport PL/SQL. . In fact.PL/SQL FAQ . PS: In earlier releases of Oracle it was better to put as much code as possible in procedures rather than triggers. PL/SQL tell the database how to do things (procedural). PL/SQL and Java appeal to different people in different job roles. One 3 of 15 30/05/2011 14:49 .com/wiki/PL/SQL_FAQ Some of the differences: SQL is executed one statement at a time. not proprietary to Oracle Incurs some data conversion overhead between the Database and Java type Java is an Object Orientated language. rather than triggers. At that stage procedures executed faster than triggers as triggers had to be re-compiled every time before executed (unless cached). The following table briefly describes the similarities and difference between these two language environments: PL/SQL: Can be used to create Oracle packages.

columns and expressions are referenced in your PL/SQL source code.* FROM ALL_SOURCE WHERE 1=2. CREATE OR REPLACE TRIGGER change_hist -. Note: If you recompile an object. TO_CHAR(LAST_DDL_TIME. 'DD-Mon-RR HH24:MI') CREATE_TIME. Example: SELECT OBJECT_NAME. EXCEPTION WHEN OTHERS THEN raise_application_error(-20000. 'TYPE'. 'DD-Mon-RR HH24:MI') MOD_TIME.Store code in hist table AFTER CREATE ON SCOTT. STATUS FROM USER_OBJECTS WHERE LAST_DDL_TIME > '&CHECK_FROM_DATE'.1. SELECT type.Change SCOTT to your schema name DECLARE BEGIN IF ORA_DICT_OBJ_TYPE in ('PROCEDURE'. SQLERRM).Create history table AS SELECT SYSDATE CHANGE_DATE. enter the string you are searching for when prompted for KEYWORD.orafaq. / show errors A better approach is to create an external CVS or SVN repository for the scripts that install the PL/SQL code. This will allow you to easily revert to previous code should someone make any catastrophic changes.DICTIONARY_OBJ_TYPE IN 8i AND NAME = ORA_DICT_OBJ_NAME. ALL_SOURCE.SCHEMA -. replace &KEYWORD with the string you are searching for. END.Store old code in SOURCE_HIST table INSERT INTO SOURCE_HIST SELECT sysdate. 'FUNCTION'.PL/SQL FAQ . both the TIMESTAMP and LAST_DDL_TIME columns are updated.* FROM ALL_SOURCE WHERE TYPE = ORA_DICT_OBJ_TYPE -. 'TYPE BODY') THEN -. the LAST_DDL_TIME column is updated. If not. If you run the above query from SQL*Plus. all_source. How can one search PL/SQL code for a string/ key value? The following query is handy if you want to know where certain tables. How does one keep a history of PL/SQL code changes? One can build a history of PL/SQL code changes by setting up an AFTER CREATE schema (or database) level trigger (available from Oracle 8. -. 'PACKAGE'. but the TIMESTAMP column is not updated. If you modified the code.DICTIONARY_OBJ_NAME IN 8i END IF. The canonical version of what's in the database must match the latest CVS/SVN version or else someone would 4 of 15 30/05/2011 14:49 .com/wiki/PL/SQL_FAQ can detect code changes by looking at the TIMESTAMP and LAST_DDL_TIME column in the USER_OBJECTS dictionary view.Oracle FAQ http://www. TO_CHAR(CREATED. line FROM user_source WHERE UPPER(text) LIKE UPPER('%&KEYWORD%'). Look at this example: CREATE TABLE SOURCE_HIST -. name. 'PACKAGE BODY'.7).

SQL*Plus will display the entire contents of the buffer when it executes this dummy PL/SQL block.plb binary file.pls source files after wrapping them. To display an empty line. How can I know which stored PL/SQL code is wrapped? The following query gives the list of all wrapped PL/SQL code: select owner. eg.pls oname=xxxx. ' wrapped'||chr(10))+instr(text. if you print too much.2) and is located in the ORACLE_HOME/bin directory. How can I protect my PL/SQL source code? Oracle provides a binary wrapper utility that can be used to scramble PL/SQL source code.com/wiki/PL/SQL_FAQ be cheating. You are supposed to backup and keep your *. This utility was introduced in Oracle7. the output buffer will overflow.orafaq.. 5 of 15 30/05/2011 14:49 . However it is possible for skilled hackers to unwrap your wrapped Oracle PL/SQL code. The utility use human-readable PL/SQL source code as input. and then EXEC NULL.put_line('Look Ma.PL/SQL FAQ . don't lose your source! The syntax is: wrap iname=myscript. set the buffer size to a larger value. However. there is no "decode" command available.plb Please note: there is no legal way to unwrap a *. For example: set serveroutput on begin dbms_output. Just be careful. command. end.2 (PL/SQL V2. 2. it is better to use new_line procedure than put_line with an empty string. type from dba_source where line = 1 and instr(text.Oracle FAQ http://www.: set serveroutput on size 200000 If you forget to set serveroutput on type SET SERVEROUTPUT ON once you remember. This buffer can be displayed on the screen from SQL*Plus if you issue the SET SERVEROUTPUT ON. and writes out portable binary object code (somewhat larger than the original). ' wrapped '||chr(10)) > 0 order by 1. name. Oracle will still understand and know how to execute the code. The binary code can be distributed without fear of exposing your proprietary algorithms and methods. If you haven't cleared the DBMS_OUTPUT buffer with the disable or enable procedure. 3 / Can one print to the screen from PL/SQL? One can use the DBMS_OUTPUT package to write information to an output buffer. I can print from PL/SQL!!!'). So. In that case. / DBMS_OUTPUT is useful for debugging PL/SQL programs.

from PL/SQL by using the "EXECUTE IMMEDIATE" statement (native SQL). 'Invalid path. DROP.3 the only means of writing a file was to use DBMS_OUTPUT with the SQL*Plus SPOOL command.FILE_TYPE.Oracle FAQ http://www. buf varchar2(4000). Here is an example: CONNECT / AS SYSDBA CREATE OR REPLACE DIRECTORY mydir AS '/tmp'. 'Look ma. END. EXCEPTION WHEN utl_file.FCLOSE(fHandler).GET_LINE(fHandler.FILE_TYPE.').invalid_path THEN raise_application_error(-20000. UTL_FILE. / NOTE: UTL_FILE was introduced with Oracle 7.com/wiki/PL/SQL_FAQ Can one read/write files from PL/SQL? The UTL_FILE database package can be used to read and write operating system files.').FCLOSE(fHandler).put_line('DATA FROM FILE: '||buf). 'myfile'.FOPEN('MYDIR'. Before Oracle 7.invalid_path THEN raise_application_error(-20000. END. TRUNCATE.orafaq. dbms_output. Copy and paste these examples to get you started: Write File DECLARE fHandler UTL_FILE. Provide user access to the UTL_FILE package (created by catproc. 'r'). Create directory or set UTL_FILE_DIR. BEGIN fHandler := UTL_FILE. Can one call DDL statements from PL/SQL? One can call DDL statements like CREATE. BEGIN fHandler := UTL_FILE.PUTF(fHandler. Examples: 6 of 15 30/05/2011 14:49 .FOPEN('MYDIR'. 'w'). UTL_FILE. A DBA user needs to grant you access to read from/ write to a specific directory before using this package. etc. GRANT read. Create directory or set UTL_FILE_DIR. write ON DIRECTORY mydir TO scott. UTL_FILE.PL/SQL FAQ .3. 'myfile'. EXCEPTION WHEN utl_file. buf).sql): GRANT EXECUTE ON UTL_FILE TO scott. 'Invalid path. UTL_FILE. Im writing to a file!!!\n'). / Read File DECLARE fHandler UTL_FILE.

. / More complex DBMS_SQL example using bind variables: 7 of 15 30/05/2011 14:49 . -. :2. One can also use the older DBMS_SQL package (V2.' sql_stmt := 'INSERT INTO dept VALUES (:1. NOTE: The DDL statement in quotes should not be terminated with a semicolon..PL/SQL FAQ . 'CREATE TABLE X (Y DATE)'. :3)'.orafaq. location..PARSE(cur. rc := DBMS_SQL. end. EXECUTE IMMEDIATE sql_stmt USING dept_id. BEGIN cur := DBMS_SQL. dept_name.Oracle FAQ http://www. Note that the statements within quotes are NOT semicolon terminated: EXECUTE IMMEDIATE 'CREATE TABLE x (a NUMBER)'. DBMS_SQL. END.com/wiki/PL/SQL_FAQ begin EXECUTE IMMEDIATE 'CREATE TABLE X(A DATE)'. Users running Oracle versions below Oracle 8i can look at the DBMS_SQL package (see FAQ about Dynamic SQL).1 and above) to execute dynamic statements. EXECUTE IMMEDIATE var. END. rc integer. sql_stmt := 'SELECT * FROM emp WHERE empno = :id'.Using bind variables.Returning a cursor.. BEGIN var := 'CREATE TABLE temp1(col1 NUMBER(2))'. Look at these examples: CREATE OR REPLACE PROCEDURE DYNSQL AS cur integer. EXECUTE IMMEDIATE sql_stmt INTO emp_rec USING emp_id.NATIVE). Look at these examples.CLOSE_CURSOR(cur). DBMS_SQL. end. -. begin execute Immediate 'TRUNCATE TABLE emp'.EXECUTE(cur). DBMS_SQL.OPEN_CURSOR. DECLARE var VARCHAR2(100). Can one use dynamic SQL statements from PL/SQL? Starting from Oracle8i one can use the "EXECUTE IMMEDIATE" statement to execute dynamic SQL and PL/SQL statements (statements created at run-time).

END. / How does one get the value of a sequence into a PL/SQL variable? 8 of 15 30/05/2011 14:49 . BEGIN v_emp. BEGIN v_cursor := DBMS_SQL.PUT_LINE('Deptartment name: '||v_dname). Example: DECLARE v_EmpName emp.CLOSE_CURSOR(v_cursor).V7). DBMS_OUTPUT. 1.DEFINE_COLUMN_CHAR(v_cursor. end loop.empno := 10. v_emp.ename := 'XXXXXXX'. view or cursor. no). The %TYPE and %ROWTYPE constructs provide data independence.ename%TYPE. 'Unknown Exception Raised: '||sqlcode||' '||sqlerrm).OPEN_CURSOR. 20). END. 1. / What is the difference between %TYPE and %ROWTYPE? Both %TYPE and %ROWTYPE are used to define variables in PL/SQL as it is defined within the database. loop if DBMS_SQL. the program automatically picks up the new definition from the database without having to make any code changes.PARSE(v_cursor. v_rows integer. DBMS_SQL. DBMS_SQL.com/wiki/PL/SQL_FAQ CREATE OR REPLACE PROCEDURE DEPARTMENTS(NO IN DEPT. 'select dname from dept where deptno > :x'. DBMS_SQL. DBMS_SQL.Oracle FAQ http://www. If the datatype or precision of a column changes.PUT_LINE('Name = ' || v_EmpName).FETCH_ROWS(v_cursor) = 0 then exit. raise_application_error(-20000. / %ROWTYPE %ROWTYPE is used to declare a record with the same types as found in the specified database table.DEPTNO%TYPE) AS v_cursor integer. DBMS_OUTPUT. v_dname. v_rows := DBMS_SQL. Examples: DECLARE v_emp emp%ROWTYPE. EXCEPTION when others then DBMS_SQL. ':x'. DBMS_SQL. end if. and allows programs to adapt as the database changes to meet new business needs.PL/SQL FAQ . reduces maintenance costs.CLOSE_CURSOR(v_cursor). v_dname char(20). BEGIN SELECT ename INTO v_EmpName FROM emp WHERE ROWNUM = 1.COLUMN_VALUE_CHAR(v_cursor. v_dname).BIND_VARIABLE(v_cursor.orafaq. DBMS_SQL. %TYPE %TYPE is used to declare a field with the same type as that of a specified table's column. END.EXECUTE(v_cursor).

Look at this External Procedure (http://www.orafaq. Defining this C/C++ function to PL/SQL makes it executable. status := dbms_pipe. that allows users to call OS commands.com/wiki/PL/SQL_FAQ As you might know.com/scripts/c_src/extproc. the following workarounds can be used: Database Pipes Write an external program (using one of the precompiler languages. one cannot use sequences directly from PL/SQL.PL/SQL FAQ .txt) example.send_message(pipe_name). dbms_pipe.com/scripts/plsql/oscmd. OCI or Perl with Oracle access modules) to act as a listener on a database pipe (SYS. CREATE OR REPLACE FUNCTION host_command( cmd IN VARCHAR2 ) RETURN INTEGER IS status NUMBER. pipe_name VARCHAR2(30).NEXTVAL. Results are passed back on a different database pipe. the listener picks it up and run the requests. RETURN status.Oracle FAQ http://www. BEGIN pipe_name := 'HOST_PIPE'. Can one execute an operating system command from PL/SQL? There is no direct way to execute operating system commands from PL/SQL. Oracle prohibits this: i := sq_sequence. as with SQL*Plus. However.NEXTVAL into :i from dual.txt DBMS_SCHEDULER In Oracle 10g and above.orafaq.orafaq. END. one can execute OS commands via the DBMS_SCHEDULER package. / External Procedure Listeners: From Oracle 8 one can call external 3GL code in a dynamically linked library (DLL or shared object).pack_message( cmd ). errormsg VARCHAR2(80). Look at this example: 9 of 15 30/05/2011 14:49 . One can write a library in C/ C++ to do whatever is required.DBMS_PIPE). Your PL/SQL program then put requests to run commands in the pipe. Using Java See example at http://www. Note: This restriction has been removed in oracle 11g and the former syntax (direct assignment) can be used. PL/SQL doesn't have a "host" command. see chapter 8 of the Oracle Application Developers Guide. Nevertheless. one can use embedded SQL statements to obtain sequence values: select sq_sequence. For a Pro*C example.

one should COMMIT less frequently within a PL/SQL loop to prevent ORA-1555 (Snapshot too old) errors.PL/SQL FAQ . END LOOP. The higher the frequency of commit. / exec dbms_scheduler. to . END LOOP. BEGIN FOR dept_rec IN dept_cur LOOP dbms_output. How does one loop through tables in PL/SQL? One can make use of cursors to loop through data within tables.create_job(job_name job_type job_action enabled auto_drop END.com/wiki/PL/SQL_FAQ BEGIN dbms_scheduler. 10 of 15 30/05/2011 14:49 ..ename)..sh'. To fix this problem one can easily rewrite code like this: FOR records IN my_cursor LOOP . / How often should one COMMIT in a PL/SQL loop? / What is the best commit strategy? Contrary to popular belief.run_job('myjob'). COMMIT.orafaq..Employee cursor all employees for a dept number CURSOR emp_cur (v_dept_no DEPT. END LOOP. DECLARE CURSOR SELECT FROM ORDER dept_cur IS deptno dept BY deptno.. 'executable'. -.. END. TRUE.put_line('.. the sooner the extents in the undo/ rollback segments will be cleared for new transactions. FOR emp_rec in emp_cur(dept_rec.do some stuff.deptno) LOOP dbms_output... .. Look at the following nested loops code example.Employee is '||emp_rec. COMMIT. causing ORA-1555 errors..Oracle FAQ http://www. TRUE).deptno)). '/app/oracle/x. => => => => => 'myjob'.DEPTNO%TYPE) IS SELECT ename FROM emp WHERE deptno = v_dept_no.put_line('Employees in Department '||TO_CHAR(dept_rec.

begin for r in c loop update big_employee_table set salary = salary * 1. bad. 1000 ) = 0 then commit. if mod ( r. The following block updates 100. At 20 minutes I killed the session.eid.com/wiki/PL/SQL_FAQ FOR records IN my_cursor LOOP .eid. end if. It is painfully slow and should never be done.2: Updating serially SQL> 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 declare cursor c is select * from big_employee_table where rownum <= 100000. BAD! It’s the WORST thing you can do… just don’t do it! In the following example I will create around 7 million rows and then attempt to update a portion of them serially. end. NOTE: Although fetching across COMMITs work with Oracle. That’s 7. END IF. is not supported by the ANSI standard. end loop. it chewed up an entire CPU core for the duration.000 rows.orafaq. notice that Oracle’s “Create Table As” (CTAS) method blazed thru table creation. In addition. committing every 1000 rows: Example 1. Sometimes. 10000) = 0 THEN COMMIT.03 where eid = r. this is the very best method of updating large data sets. Example 1.1: Creating a somewhat large table SQL> create table big_employee_table 2 as 3 select rownum as eid 4 . i := i+1.. contact your DBA to increase the undo/ rollback segments.23 seconds. I will issue a commit every thousandth row.Commit every 10000 records If you still get ORA-1555 errors. / Observe that the update took more time than I have patience for . -. Moreover.11 Before I go on. serially. Table created.. Issuing frequent commits is bad. Elapsed: 00:00:12.Oracle FAQ http://www.. COMMIT. COUNT(*) ---------7838713 Elapsed: 00:00:08.do some stuff. IF mod(i. e.* 5 from hr.). END LOOP.PL/SQL FAQ ..84 Million rows in 12.employees e 6 . dba_objects do. If you’re 11 of 15 30/05/2011 14:49 .23 SQL> select count(*) 2 from big_employee_table.

The point is that the update was incredibly fast and chewed up only 10% of one core. commit. The following block does the same work in bulk: Example 1. lr_rowid. cursor c is select rowid rid .count loop lr_salary ( a ) := lr_salary ( a ) * 1. lr_salary limit 500.. dbms_sql. lr_rowid.PL/SQL FAQ . close c.there! not in the loop exception when others then rollback. Do If If If it in SQL. in answer to the question of “how often should I commit?” I say don’t until you absolutely have to. 4. but does not observe privileges given through roles. salary from big_employee_table where rownum <= 100000.. exit when c%notfound.Oracle FAQ http://www.orafaq. -. for a in 1 .count update big_employee_table set salary = lr_salary ( b ) where rowid in ( lr_rowid ( b )). dbms_output. lr_rowid lr_salary obj_rowid. Choose one 12 of 15 30/05/2011 14:49 .put_line ( sqlerrm ).03. PL/SQL can’t do it. SQL can’t do it. So. begin open c.com/wiki/PL/SQL_FAQ only updating a few rows. end loop. 3. 2.number_table. do it in PL/SQL. do it in Java. end loop. Java can’t do it ask yourself if it needs to be done.11 SQL> Notice that the update completed in 2 seconds! I’ve seen faster but my two-gerbil sandbox machine doesn’t have the power that our newer servers do. why do it in PL/SQL at all? I like Tom Kyte’s approach (paraphrasing): 1. I can SELECT from SQL*Plus but not from PL/SQL. What is wrong? PL/SQL respect object privileges given directly to the user. end.3: Updating in bulk and committing at the end SQL> 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 declare type obj_rowid is table of rowid index by pls_integer. forall b in 1 . Elapsed: 00:00:02. but will give an error in PL/SQL. / PL/SQL procedure successfully completed. loop fetch c bulk collect into lr_rowid . The consequence is that a SQL statement can work in SQL*Plus.

A mutating table is a table that is currently being modified by an update. create or replace procedure proc1 authid current_user is begin . Note that it is not enough to just use "similar" type definitions. There are several restrictions in Oracle regarding triggers: A row-level trigger cannot query or modify a mutating table. A statement-level trigger cannot query or modify a mutating table if the trigger is fired as the result of a CASCADE delete. foreign or unique key columns of the table off which it fires. Move all the tables to one user/schema.VARCHAR2S) IS BEGIN -.. Etc. Another way this error can occur is if the trigger has statements to change the primary. NEW and OLD still can be accessed by the trigger).PL/SQL FAQ .orafaq.com/wiki/PL/SQL_FAQ of the following solutions: Grant direct access on the tables to your user. (Of course. What is a mutating and constraining table? "Mutating" means "changing". Look at this example: -. / 13 of 15 30/05/2011 14:49 . Define your procedures with invoker rights (Oracle 8i and higher).Oracle FAQ http://www. END. the workaround is to enforce the referential integrity through triggers as well. Can one pass an object/table as an argument to a remote procedure? The only way to reference an object type between databases is via a database link.Database A: receives a PL/SQL table from database B CREATE OR REPLACE PROCEDURE pcalled(TabX DBMS_SQL. When a trigger tries to reference a table that is in state of flux (being changed). Do not use roles! GRANT select ON scott. If you must have triggers on tables that have referential constraints. it is considered "mutating" and raises an error since Oracle should not return data that has not yet reached its final state.. delete.do something with TabX from database B null. or insert statement.emp TO my_user.

You cannot call a procedure in a SQL query. all your PL/SQL code is sent directly to the database engine for execution.Oracle FAQ http://www. / What is the difference between stored procedures and functions? Functions MUST return a value.VARCHAR2S@DBLINK2.update. END. the max size is not an explicit byte limit.orafaq. subquery and label nesting? The following limits apply: Level of Block Nesting: 255 Level of Record Nesting: 32 Level of Subquery Nesting: 254 Level of Label Nesting: 98 Can one COMMIT/ ROLLBACK from within a trigger? A commit inside a trigger would defeat the basic definition of an atomic transaction (see ACID). For example. SQL*Plus does not have an embedded PL/SQL engine. What are the PL/SQL compiler limits for block. you cannot call such a function in a SQL query. but related to the parse tree that is created when you compile the code. . However an autonomous transaction function can. This makes it much more efficient as SQL statements are not stripped off and sent to the database individually. if you have a function that is updating a table. Is there a limit on the size of a PL/SQL block? Yes. But. You can run the following select statement to query the size of an existing package or procedure: SQL> select * from dba_object_size where name = 'procedure_name'. you cannot call that function from a SQL query. Is there a PL/SQL Engine in SQL*Plus? No.select myFunction(field) from sometable.Database B: sends a PL/SQL table to database A CREATE OR REPLACE PROCEDURE pcalling IS TabX DBMS_SQL. procedures don't need to. Thus. record.PL/SQL FAQ .com/wiki/PL/SQL_FAQ -. BEGIN pcalled@DBLINK2(TabX). delete) statements in a function. You can have DML (insert. will throw error. Unlike Oracle Forms. Trigger logic is 14 of 15 30/05/2011 14:49 .

6 END. Table created. SQL> INSERT INTO tab1 VALUES (1). 7 COMMIT. Table created. 7 / Trigger created.even if the main transaction is rolled-back! Remember that an "autonomous_transaction" procedure/function/trigger is a whole transaction in itself and so it must end with a commit or a rollback statement.Oracle FAQ http://www. Unlike regular triggers.com/wiki/PL/SQL_FAQ by definition an extension of the original DML operation. 9 / Trigger created. Here is an example of what will happen when they do: SQL> CREATE TABLE tab1 (col1 NUMBER). .:: Wiki Home :: Blogger Home :: Forum Home :: Contact :: Privacy ::. INSERT INTO tab1 VALUES (1) * ERROR at line 1: ORA-04092: cannot COMMIT in a trigger ORA-06512: at "SCOTT. Example: SQL> CREATE OR REPLACE TRIGGER tab1_trig 2 AFTER insert ON tab1 3 DECLARE 4 PRAGMA AUTONOMOUS_TRANSACTION.com/wiki/PL/SQL_FAQ" Category: Frequently Asked Questions This page was last modified on 26 May 2011. 'Insert on TAB1'). SQL> CREATE TRIGGER tab1_trig 2 AFTER insert ON tab1 3 BEGIN 4 INSERT INTO log VALUES (SYSDATE.orafaq. operation VARCHAR2(2000)).TAB1_TRIG' Autonomous transactions: As workaround. 5 BEGIN 6 INSERT INTO log VALUES (SYSDATE. 'Insert on TAB1'). -. one can use autonomous transactions. Autonomous transactions execute separate from the current transaction. Retrieved from "http://www. 15 of 15 30/05/2011 14:49 .PL/SQL FAQ .only allowed in autonomous triggers 8 END. For this reason.orafaq. triggers are NOT allowed to execute COMMIT or ROLLBACK statements (with the exception of autonomous triggers).TAB1_TRIG". Note that with the above example will insert and commit log entries . 1 row created. at 15:30. 5 COMMIT. SQL> INSERT INTO tab1 VALUES (1). Changes made within triggers should thus be committed or rolled back as part of the transaction in which they execute. autonomous triggers can contain COMMIT and ROLLBACK statements. line 3 ORA-04088: error during execution of trigger 'SCOTT. SQL> CREATE TABLE log (timestamp DATE.

Sign up to vote on this title
UsefulNot useful