Index   Search   Add FAQ   Ask Question  

Oracle PL/SQL FAQ

$Date: 05-Jan-2000 $
$Revision: 1.50a $
$Author: Frank Naudé $

Topics

  • What is PL/SQL and what is it good for?
  • 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 use dynamic SQL within PL/SQL?
  • 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?
  • Can one pass an object/table as arguments to remote procedure?
  • Is there a PL/SQL Engine in SQL*Plus?
  • Is there a limit on the size of a PL/SQL block?
  • Where can I find more info about PL/SQL?

  • Back to Oracle FAQ Index

    What is PL/SQL and what is it good 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 language includes object oriented programming techniques such as encapsulation, function overloading, information hiding (all but inheritance) and is commonly used to write data-centric programs to manipulate Oracle data.

  • 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:

  • 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:
       begin
          dbms_output.put_line('Look Ma, I can print from PL/SQL!!!');
       end;
       /
    
    But what if you forget to set serveroutput on? No problem, just 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 use dynamic SQL within PL/SQL?

    From PL/SQL V2.1 one can use the DBMS_SQL package to execute dynamic SQL statements. Eg:
    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;
    /
    
    Another example:
    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

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

    As you might know, oracle prohibits this: (for some silly reason). But you can do this:

    Thanks to Ronald van Woensel

  • Back to top of file

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

    In Oracle7 there is no direct way to execute an operating system command from PL/SQL. 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 DBMS_PIPE. You PL/SQL then places requests to run commands in the pipe, the listener picks it up and runs them. Results are passes back on a different pipe. For an Pro*C example, see chapter 8 of the Oracle Application Developers Guide. This example is also on the Support Notes CD-ROM that all supported customers should be getting quarterly. Just search on "DBMS_PIPE".
    In Oracle8 you can call external 3GL code in a dynamically linked
    library (DLL or shared object). So you just write a library in C doing
    what you want, ie in your case a host function taking the command line
    as input argument.  And that function will be callable from PL/SQL.
    

    So what you have to do is more or less:

    1. Write C code in host.c: int host(char *command) { ... }. 2. Compile C code to DLL or shared object, eg c:\winnt\system32\host.dll. 3. "create or replace library host as 'c:\winnt\system32\host.dll';" 4. "create or replace function host(command in varchar2) return pls_integer is external library host name "host" language c calling standard pascal parameters (host string, return long);"

    Let's say I had a PL/SQL block and I wanted to do a "ls -l" to get a directory listing. Is there any way to do that?

    In C Language, I can do { x = system("ls -l"); }

    The way most people do that is to use the pipe facility of the DBMS kernel: set up a pipe for use by a sender (the PL/SQL program that needs to invoke a command) and a receiver, written in Pro*C, responsible for executing the command piped by the sender. Maybe there are some tools around so one doesn't need to code this, as it involves a listener and multiple server processes on your machine.

    Alternatively I have a more complex solution, which uses a table to pass arguments and the command to execute - just as the DBMS_PIPE package does internally. You would insert a row into the table and the listener would execute a command, passing back succession status and indicating "in progress" on long-running commands. This tools allows for non-blocking execution of commands.

  • 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

  • Can one pass an object/table as arguments to remote procedure?

    The only way the same object type can be referenced between two databases is via a database link. Note that 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
       null;
    END;
    /
    
    -- Database B: send 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 there a PL/SQL Engine in SQL*Plus?

    No. Unlike Oracle Forms, SQL*Plus does not have a PL/SQL engine. Thus, all your PL/SQL are send directly to the database engine for execution. This makes it much more efficient as SQL statements are not stripped off and send 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 I find more info about PL/SQL?

  • Back to top of file

  • General: Home | Index | Preamble | Glossary | OraCorp | Papers | Fun | News | Events | Y2000 | Books | Links | Forums
    Products: SQL | Plus | Loader | PL/SQL | PreComp | OPO | OMO | OO4OLE | DBA | PQO | PSO | OCO | Net | ODBC | WebServer | Des2k | Dev2k
    Systems: MVS | Unix | Windows | WindowsNT | NetWare | VMS