Posts Tagged ‘Oracle PL/SQL FAQ’

Oracle – Frequently Asked Questions

Wednesday, April 29th, 2009

1. What is ORACLE?

Oracle is a Relational Database Management System (RDBMS) which is used widely for business applications.

 2. What is SGA?

The System Global Area (SGA) is a shared memory region allocated by ORACLE that contains data and control information for one ORACLE instance.

 3. What is the function of Checkpoint (CKPT)?

The Checkpoint (CKPT) process is responsible for signaling DBWR at checkpoints and updating all the data files and control files of the database.

The CKPT is also useful to get the point in time from where to begin the recovery in case of failure.

 Which background process is responsible for writing the dirty buffers from the write list to the data files during a checkpoint?

The DBWn is usually responsible for this.

 What are the components of SGA? 

The components of the SGA are Database buffers, Redo Log Buffer and the Shared Pool.

  4. What constitute an ORACLE Instance?

SGA and ORACLE background processes together constitute an ORACLE instance.

 What do Database Buffers contain?

Database Buffers store the most recently used blocks of database data. They can also contain modified data that has not yet been permanently written to disk.

 5. What do Redo Log Buffers contain?

Redo Log Buffers store redo entries or a log of changes made to the database.

  6. What is Shared Pool?

Shared pool is composed of two parts; Library Cache and Data Dictionary Cache.

The Library Cache contains the shared SQL areas, private SQL areas, PL/SQL procedures and packages, and control structures such as locks and Library Cache handles. The shared SQL area contains the parse tree and execution plan; whereas the private SQL area contains values for bind variables and runtime buffers. The data dictionary cache holds most recently used database dictionary information.

 

 7. What is the default undo management mode in ORACLE?

The default undo management mode is manual.

 8. How can you dynamically size the SGA?

You can do this using the Alter System command.

  9. What is the Multiple Block size?

We can assign different block sizes for each table space. This is called the Multiple Block size.

 

 10. Where are the compiled functions and procedures stored in memory?

These are stored in the Library Cache.

  11. What is SQL*PLUS?

It is a browser-based interface to communicate with the database.

  12. What are the advantages of creating clusters in ORACLE?

It improves the efficiency of join queries and they are storage efficient.

  13. What are Bind Variables?

A bind variable is a variable that you declare in a host environment. Bind variables can be used to pass run-time values, either number or character, into or out of one or more PL/SQL programs. The PL/SQL programs use bind variables as they would use any other variable. You can reference variables declared in the host or calling environment in PL/SQL statements, unless the statement is in a procedure, function, or package. This includes host language variables declared in precompiler programs, screen fields in Oracle Developer Forms applications, and iSQL*Plus bind variables.

  14. How to create Bind Variables?

To declare a bind variable in the iSQL*Plus environment, use the command VARIABLE. For example, you declare a variable of type NUMBER and VARCHAR2 as follows:

VARIABLE return_code NUMBER

VARIABLE return_msg  VARCHAR2(30)

Both SQL and iSQL*Plus can reference the bind variable, and iSQL*Plus can display its value through the iSQL*Plus PRINT command.

  15. Explain few programming guidelines for PL/SQL ?

To produce clear code and reduce maintenance when developing a PL/SQL block follow the below listed guidelines

-              Documenting code with comments

-              Developing a case convention for the code

-              Developing naming conventions for identifiers and other objects

-              Enhancing readability by indenting

 16. What are the advantages of using cursors with FOR LOOPS?

A cursor FOR loop is a shortcut because of the following reasons

-              the cursor is opened automatically when the for loop is encountered for the first time and fetches the first row

-              rows are fetched once for each iteration in the loop

-              The loop itself is terminated automatically at the end of the iteration where the last row is fetched.

  17. What is the use of NO WAIT option with SELECT…. FOR UPDATE clause?

The optional NOWAIT keyword tells Oracle not to wait if requested rows have been locked by another user. Control is immediately returned to your program so that it can do other work before trying again to acquire the lock. If you omit the NOWAIT keyword , Oracle waits until the rows are available.

 18. Can we use raise system defined exceptions  explicitly by issuing the RAISE statement?

Yes, you can. For example : RAISE NO_DATA_FOUND

  19. Can we handle the exception raised by RAISE_APPLICATION_ERROR?

Yes. For example, you can write “WHEN OTHERS” exception handler, and handler for this.

 20. Can we assign default values to all modes of parameters in a subprogram?

You can assign default values only to parameters of the IN mode. OUT and IN OUT parameters are not permitted to have default values.

  21. How Procedures are different from Functions?

You create a procedure to store a series of actions for later execution. A procedure can contain zero or more parameters that can be transferred to and from the calling environment, but a procedure does not have to return a value.

You create a function when you want to compute a value, which must be returned to the calling environment. A function can contain zero or more parameters that are transferred from the calling environment. Functions should return only a single value, and the value is returned through a RETURN statement. Functions used in SQL statements cannot have OUT or IN OUT mode parameters.

  22. What is the use of SHOW ERRORS command?

SHOW ERRORS command is used  at the SQL prompt to obtain compilation errors for the last object you compiled.

You can also use the command with a specific program unit. The syntax is as follows:

SHOW ERRORS [{FUNCTION|PROCEDURE|PACKAGE|PACKAGE BODY|TRIGGER|VIEW} [schema.]name]

Example : SHOW ERRORS PROCEDURE p1

Using the SHOW ERRORS command, you can view only the compilation errors that are generated by the latest statement that is used to create a subprogram. The USER_ERRORS data dictionary view stores all the compilation errors generated previously while creating subprograms.

  23. How to drop one member of a package?

You cannot drop members of a package using DROP command. When you drop the package, all the members of the package are automatically dropped. To drop or add any members of the package, rewrite the whole package again.

  24. What will happen to the dependent constructs of a package ,if we change the package specification?

Changes to the package body do not require recompilation of dependent constructs, whereas changes to the package specification require recompilation of every stored subprogram that references the package. To reduce the need for recompiling when code is changed, place as few constructs as possible in a package specification.

  25. Explain the firing sequence for a trigger on a table.

The sequence is as below :

a.            Before Statement Trigger

b.            Before Row Trigger

c.             After row trigger

d.            After statement Trigger

  26. Can we write INSTEAD OF triggers on tables?

No, it is written only on views

  27. What will happen to triggers when the table on which the trigger is written?

All triggers on a table are automatically dropped when the  table is dropped.

 

 

 

Oracle PL/SQL FAQ

Tuesday, March 10th, 2009

Source: http://www.orafaq.com

Web site has given permission to distribute this FAQ for non-profit purpose provided the ‘Disclaimer’ and ‘Copyright’ mentioned at the end of the document is included along with FAQ.

Oracle PL/SQL FAQ

Author: Frank Naudé

Topics

·  What is PL/SQL and what is it used for?

·  Should one use PL/SQL or Java to code procedures and triggers?

·  How can one see if somebody modified any code?

·  How can one search PL/SQL code for a string/key value?

·  How can one keep a history of PL/SQL code changes?

·  How can I protect my PL/SQL source code?

·  Can one print to the screen from PL/SQL?

·  Can one read/write files from PL/SQL?

·  Can one call DDL statements from PL/SQL?

·  Can one use dynamic SQL statements from PL/SQL?

·  What is the difference between %TYPE and %ROWTYPE?

·  What is the result of comparing NULL with NULL?

·  How does one get the value of a sequence into a PL/SQL variable?

·  Can one execute an operating system command from PL/SQL?

·  How does one loop through tables in PL/SQL?

·  How often should one COMMIT in a PL/SQL loop? / What is the best commit strategy?

·  I can SELECT from SQL*Plus but not from PL/SQL. What is wrong?

·  What is a mutating and constraining table?

·  Can one pass an object/table as an argument to a remote procedure?

·  Is it better to put code in triggers or procedures? What is the difference?

·  Is there a PL/SQL Engine in SQL*Plus?

·  Is there a limit on the size of a PL/SQL block?

-  Where can one find more info about PL/SQL?

 



What is PL/SQL and what is it used for?

PL/SQL is Oracle’s Procedural Language extension to SQL. PL/SQL’s language syntax, structure and data types are similar to that of ADA. The PL/SQL language includes object oriented programming techniques such as encapsulation, function overloading, information hiding (all but inheritance). PL/SQL is commonly used to write data-centric programs to manipulate data in an Oracle database.

·  Back to top of file



Should one use PL/SQL or Java to code procedures and triggers?

Internally the Oracle database supports two procedural languages, namely PL/SQL and Java. This leads to questions like “Which of the two is the best?” and “Will Oracle ever desupport PL/SQL in favor of Java?”

Many Oracle applications are based on PL/SQL and it would be difficult of Oracle to ever desupport PL/SQL. In fact, all indications are that PL/SQL still has a bright future ahead of it. Many enhancements are still being made to PL/SQL. For example, Oracle 9iDB supports native compilation of Pl/SQL code to binaries.

PL/SQL and Java appeal to different people in different job roles. The following table briefly describes the difference between these two language environments:

PL/SQL:

·  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

·  Easier to use than Java (depending on your background)

Java:

·  Open standard, not proprietary to Oracle

·  Incurs some data conversion overhead between the Database and Java type systems

·  Java is more difficult to use (depending on your background)

 

·  Back to top of file

 



How can one see if somebody modified any code?

Code for stored procedures, functions and packages are stored in the Oracle Data Dictionary. One can detect code changes by looking at the LAST_DDL_TIME column in the USER_OBJECTS dictionary view. Example:

        SELECT OBJECT_NAME,
               TO_CHAR (CREATED,       'DD-Mon-RR HH24: MI’) CREATE_TIME,
               TO_CHAR(LAST_DDL_TIME, 'DD-Mon-RR HH24:MI') MOD_TIME,
               STATUS
        FROM   USER_OBJECTS
        WHERE  LAST_DDL_TIME > '&CHECK_FROM_DATE';

 

·  Back to top of file

 



How can one search PL/SQL code for a string/ key value?

The following query is handy if you want to know where a certain table, field or expression is referenced in your PL/SQL source code.

        SELECT TYPE, NAME, LINE
        FROM   USER_SOURCE
        WHERE  UPPER(TEXT) LIKE '%&KEYWORD%';

 

·  Back to top of file

 



How can 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.1.7). This way one can easily revert to previous code should someone make any catastrophic changes. Look at this example:

        CREATE TABLE SOURCE_HIST                     -- Create history table
          AS SELECT SYSDATE CHANGE_DATE, USER_SOURCE.*
             FROM   USER_SOURCE WHERE 1=2;
 
        CREATE OR REPLACE TRIGGER change_hist        -- Store code in hist table
               AFTER CREATE ON SCOTT.SCHEMA          -- Change SCOTT to your schema name
        DECLARE
        BEGIN
          if DICTIONARY_OBJ_TYPE in ('PROCEDURE', 'FUNCTION',
                          'PACKAGE', 'PACKAGE BODY', 'TYPE') then
             -- Store old code in SOURCE_HIST table
             INSERT INTO SOURCE_HIST
                SELECT sysdate, user_source.* FROM USER_SOURCE
                WHERE  TYPE = DICTIONARY_OBJ_TYPE
                  AND  NAME = DICTIONARY_OBJ_NAME;
          end if;
        EXCEPTION
          WHEN OTHERS THEN
               raise_application_error(-20000, SQLERRM);
        END;
        /
        show errors

 

·  Back to top of file

 



How can I protect my PL/SQL source code?

PL/SQL V2.2, available with Oracle7.2, implements a binary wrapper for PL/SQL programs to protect the source code.

This is done via a standalone utility that transforms the PL/SQL source code into portable binary object code (somewhat larger than the original). This way you can distribute software without having to worry about exposing your proprietary algorithms and methods. SQL*Plus and SQL*DBA will still understand and know how to execute such scripts. Just be careful, there is no “decode” command available.

The syntax is:

               wrap iname=myscript.sql oname=xxxx.plb

 

·  Back to top of file

 



Can one print to the screen from PL/SQL?

One can use the DBMS_OUTPUT package to write information to an output buffer. This buffer can be displayed on the screen from SQL*Plus if you issue the SET SERVEROUTPUT ON; command. For example:

               set serveroutput on
               begin
                  dbms_output.put_line('Look Ma, I can print from PL/SQL!!!');
               end;
               /

DBMS_OUTPUT is useful for debugging PL/SQL programs. However, if you print too much, the output buffer will overflow. In that case, set the buffer size to a larger value, eg.: set serveroutput on size 200000

If you forget to set serveroutput on type SET SERVEROUTPUT ON once you remember, and then EXEC NULL;. If you haven’t cleared the DBMS_OUTPUT buffer with the disable or enable procedure, SQL*Plus will display the entire contents of the buffer when it executes this dummy PL/SQL block.

·  Back to top of file

 



Can one read/write files from PL/SQL?

Included in Oracle 7.3 is an UTL_FILE package that can read and write operating system files. The directory you intend writing to has to be in your INIT.ORA file (see UTL_FILE_DIR=… parameter). Before Oracle 7.3 the only means of writing a file was to use DBMS_OUTPUT with the SQL*Plus SPOOL command.

Copy this example to get started:

               DECLARE
                 fileHandler UTL_FILE.FILE_TYPE;
               BEGIN
                 fileHandler := UTL_FILE.FOPEN('/tmp', 'myfile', 'w');
                 UTL_FILE.PUTF(fileHandler, 'Look ma, I''m writing to a file!!!\n');
                 UTL_FILE.FCLOSE(fileHandler);
               EXCEPTION
                 WHEN utl_file.invalid_path THEN
                    raise_application_error(-20000, 'ERROR: Invalid path for file or path not in INIT.ORA.');
               END;
               /

 

·  Back to top of file

 



Can one call DDL statements from PL/SQL?

One can call DDL statements like CREATE, DROP, TRUNCATE, etc. from PL/SQL by using the “EXECUTE IMMEDATE” statement. Users running Oracle versions below 8i can look at the DBMS_SQL package (see FAQ about Dynamic SQL).

               begin
                  EXECUTE IMMEDIATE 'CREATE TABLE X(A DATE)';
               end;

NOTE: The DDL statement in quotes should not be terminated with a semicolon.

 

·  Back to top of file

 



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). Look at these examples. Note that statements are NOT terminated by semicolons:

               EXECUTE IMMEDIATE 'CREATE TABLE x (a NUMBER)';
 
               -- Using bind variables...
               sql_stmt := 'INSERT INTO dept VALUES (:1, :2, :3)';
               EXECUTE IMMEDIATE sql_stmt USING dept_id, dept_name, location;
 
        -- Returning a cursor...
               sql_stmt := 'SELECT * FROM emp WHERE empno = :id';
               EXECUTE IMMEDIATE sql_stmt INTO emp_rec USING emp_id;

One can also use the older DBMS_SQL package (V2.1 and above) to execute dynamic statements. Look at these examples:

               CREATE OR REPLACE PROCEDURE DYNSQL AS
                 cur integer;
                 rc  integer;
               BEGIN
                 cur := DBMS_SQL.OPEN_CURSOR;
                 DBMS_SQL.PARSE(cur, 'CREATE TABLE X (Y DATE)', DBMS_SQL.NATIVE);
                 rc := DBMS_SQL.EXECUTE(cur);
                 DBMS_SQL.CLOSE_CURSOR(cur);
               END;
               /

More complex DBMS_SQL example using bind variables:

               CREATE OR REPLACE PROCEDURE DEPARTMENTS(NO IN DEPT.DEPTNO%TYPE) AS
                 v_cursor integer;
                 v_dname  char(20);
                 v_rows   integer;
               BEGIN
                 v_cursor := DBMS_SQL.OPEN_CURSOR;
                 DBMS_SQL.PARSE(v_cursor, 'select dname from dept where deptno > :x ', DBMS_SQL.V7);
                 DBMS_SQL.BIND_VARIABLE(v_cursor, ':x', no);
                 DBMS_SQL.DEFINE_COLUMN_CHAR(v_cursor, 1, v_dname, 20);
                 v_rows := DBMS_SQL.EXECUTE(v_cursor);
                 loop
                   if DBMS_SQL.FETCH_ROWS(v_cursor) = 0 then
                      exit;
                   end if;
                   DBMS_SQL.COLUMN_VALUE_CHAR(v_cursor, 1, v_dname);
                   DBMS_OUTPUT.PUT_LINE('Deptartment name: '||v_dname);
                 end loop;
                 DBMS_SQL.CLOSE_CURSOR(v_cursor);
               EXCEPTION
                 when others then
                      DBMS_SQL.CLOSE_CURSOR(v_cursor);
                      raise_application_error(-20000, 'Unknown Exception Raised: '||sqlcode||' '||sqlerrm);
               END;
               /

 

·  Back to top of file

 



What is the difference between %TYPE and %ROWTYPE?

The %TYPE and %ROWTYPE constructs provide data independence, reduces maintenance costs, and allows programs to adapt as the database changes to meet new business needs.

%ROWTYPE is used to declare a record with the same types as found in the specified database table, view or cursor. Example:

               DECLARE
                  v_EmpRecord  emp%ROWTYPE;

%TYPE is used to declare a field with the same type as that of a specified table’s column. Example:

               DECLARE
                  v_EmpNo  emp.empno%TYPE;

 

·  Back to top of file

 



What is the result of comparing NULL with NULL?

NULL is neither equal to NULL, nor it is not equal to NULL. Any comparison to NULL is evaluated to NULL. Look at this code example to convince yourself.

               declare
                 a number := NULL;
                 b number := NULL;
               begin
                 if a=b then
                    dbms_output.put_line('True, NULL = NULL');
                 elsif a<>b then
                    dbms_output.put_line('False, NULL <> NULL');
                 else
                    dbms_output.put_line('Undefined NULL is neither = nor <> to NULL');
                 end if;
               end;

 

·  Back to top of file

 



How does one get the value of a sequence into a PL/SQL variable?

As you might know, one cannot use sequences directly from PL/SQL. Oracle prohibits this:

               i := sq_sequence.NEXTVAL;

However, one can use embedded SQL statements to obtain sequence values:

               select sq_sequence.NEXTVAL into :i from dual;

 

·  Back to top of file

 



Can one execute an operating system command from PL/SQL?

There is no direct way to execute operating system commands from PL/SQL in Oracle7. However, one can write an external program (using one of the precompiler languages, OCI or Perl with Oracle access modules) to act as a listener on a database pipe (SYS.DBMS_PIPE). Your PL/SQL program then put requests to run commands in the pipe, the listener picks it up and run the requests. Results are passed back on a different database pipe.

In Oracle8 one can call external 3GL code in a dynamically linked library (DLL or shared object). One just write a library in C/ C++ to do whatever is required. Defining this C/C++ function to PL/SQL makes it executable. Look at this External Procedure example.

·  Back to top of file

 



How does one loop through tables in PL/SQL?

Look at the following nested loop code example.

               DECLARE
                  CURSOR dept_cur IS
                  SELECT deptno
                    FROM dept
                   ORDER BY deptno;
                  -- Employee cursor all employees for a dept number
                  CURSOR emp_cur (v_dept_no DEPT.DEPTNO%TYPE) IS
                  SELECT ename
                    FROM emp
                   WHERE deptno = v_dept_no;
               BEGIN
                  FOR dept_rec IN dept_cur LOOP
                     dbms_output.put_line('Employees in Department '||TO_CHAR(dept_rec.deptno));
                     FOR emp_rec in emp_cur(dept_rec.deptno) LOOP
                        dbms_output.put_line('...Employee is '||emp_rec.ename);
                     END LOOP;
                 END LOOP;
               END;
               /

 

·  Back to top of file

 



How often should one COMMIT in a PL/SQL loop? / What is the best commit strategy?

Contrary to popular believe, one should COMMIT less frequently within a PL/SQL loop to prevent ORA-1555 (Snapshot too old) errors. The higher the frequency of commit, the sooner the extents in the rollback segments will be cleared for new transactions, causing ORA-1555 errors.

To fix this problem one can easily rewrite code like this:

               FOR records IN my_cursor LOOP
                  ...do some stuff...
                  COMMIT;
               END LOOP;

… to …

               FOR records IN my_cursor LOOP
                  ...do some stuff...
                  i := i+1;
                  IF mod(i, 10000) THEN    -- Commit every 10000 records
                     COMMIT;
                  END IF;
               END LOOP;

If you still get ORA-1555 errors, contact your DBA to increase the rollback segments.

NOTE: Although fetching across COMMITs work with Oracle, is not supported by the ANSI standard.

·  Back to top of file

 



I can SELECT from SQL*Plus but not from PL/SQL. What is wrong?

PL/SQL respect object privileges given directly to the user, but does not observe privileges given through roles. The consequence is that a SQL statement can work in SQL*Plus, but will give an error in PL/SQL. Choose one of the following solutions:

  • Grant direct access on the tables to your user. Do not use roles!
·         ·                         GRANT select ON scott.emp TO my_user;
    
  • Define your procedures with invoker rights (Oracle 8i and higher);
  • Move all the tables to one user/schema.

 

·  Back to top of file

 



What is a mutating and constraining table?

“Mutating” means “changing”. A mutating table is a table that is currently being modified by an update, delete, or insert statement. When a trigger tries to reference a table that is in state of flux (being changed), it is considered “mutating” and raises an error since Oracle should not return data that has not yet reached its final state.

Another way this error can occur is if the trigger has statements to change the primary, foreign or unique key columns of the table off which it fires. If you must have triggers on tables that have referential constraints, the workaround is to enforce the referential integrity through triggers as well.

There are several restrictions in Oracle regarding triggers:

  • A row-level trigger cannot query or modify a mutating table. (Of course, NEW and OLD still can be accessed by the trigger) .
  • A statement-level trigger cannot query or modify a mutating table if the trigger is fired as the result of a CASCADE delete.
  • Etc.

 

·  Back to top of file

 



Can one pass an object/table as an argument to a remote procedure?

The only way the same object type can be referenced between two databases is via a database link. Note that it is not enough to just use the same type definitions. Look at this example:

               -- Database A: receives a PL/SQL table from database B
               CREATE OR REPLACE PROCEDURE pcalled(TabX DBMS_SQL.VARCHAR2S) IS
               BEGIN
                  -- do something with TabX from database B
                  null;
               END;
               /
 
               -- Database B: sends a PL/SQL table to database A
               CREATE OR REPLACE PROCEDURE pcalling IS
                  TabX DBMS_SQL.VARCHAR2S@DBLINK2;
               BEGIN
                  pcalled@DBLINK2(TabX);
               END;
               /
 

·  Back to top of file

 



Is it better to put code in triggers or procedures? What is the difference?

In earlier releases of Oracle it was better to put as much code as possible in procedures rather than triggers. At that stage procedures executed faster than triggers as triggers had to be re-compiled every time before executed (unless cached). 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.

 

·  Back to top of file

 



Is there a PL/SQL Engine in SQL*Plus?

No. Unlike Oracle Forms, SQL*Plus does not have an embedded PL/SQL engine. Thus, all your PL/SQL code is sent directly to the database engine for execution. This makes it much more efficient as SQL statements are not stripped off and sent to the database individually.

 

·  Back to top of file

 



Is there a limit on the size of a PL/SQL block?

Yes, the max size is not an explicit byte limit, but related to the parse tree that is created when you compile the code. 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';

 

·  Back to top of file

 



Where can one find more info about PL/SQL?

 

·  Back to top of file

 



 

Disclaimer:

 

All information is offered in good faith and in the hope that it may be of use, but is not guaranteed to be correct, up to date or suitable for any particular purpose.

The author (and contributors) accepts no liability in respect of this information or its use.

This site is independent of and does not represent Oracle Corporation in any way. Oracle does not officially sponsor, approve, or endorse this site or its content.

 

 

Copyright:

 

This site and all content provided on it belongs to the Oracle Underground FAQ (http://www.ibi.co.za/frank/faq.htm) and is copyrighted by law.

Oracle is a registered trademark of the Oracle Corporation.

All material is, and will remain the property of the respective owners and contributors.