Академический Документы
Профессиональный Документы
Культура Документы
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 sequential statements. To overcome this limitation,
PL/SQL was created.
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. Some of the statements provided by
PL/SQL:
Conditional Control Statements:
IF ... THEN ... ELSIF ... THEN ... ELSE ... END IF;
CASE ... WHEN ... THEN ... ELSE ... END CASE;
Iterative Statements:
GOTO ...;
NULL;
SQL tells the database what to do (declarative), not how to do it. In contrast, PL/SQL
tell the database how to do things (procedural).
SQL is used to code queries, DML and DDL statements. PL/SQL is used to code
program blocks, triggers, functions, procedures and packages.
You can embed SQL in a PL/SQL program, but you cannot embed PL/SQL within a
SQL statement.
3.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. This often
leads to questions like "Which of the two is the best?" and "Will Oracle ever desupport
PL/SQL in favour 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 9i supports native
compilation of PL/SQL code to binaries. Not to mention the numerous PL/SQL
enhancements made in Oracle 10g and 11g.
PL/SQL and Java appeal to different people in different job roles. The following table briefly
describes the similarities and difference between these two language environments:
PL/SQL:
Java:
Incurs some data conversion overhead between the Database and Java type
Java is an Object Orientated language, and modules are structured into classes
PS: Starting with Oracle 10g, .NET procedures can also be stored within the database
(Windows only). Nevertheless, unlike PL/SQL and JAVA, .NET code is not usable on nonWindows systems.
PS: 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. However, it is still considered a best practice to put
as much of your program logic as possible into packages, rather than triggers.
4.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. One can detect code changes by looking at the TIMESTAMP and
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';
Note: If you recompile an object, the LAST_DDL_TIME column is updated, but the
TIMESTAMP column is not updated. If you modified the code, both the TIMESTAMP and
LAST_DDL_TIME columns are updated.
5.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, columns and
expressions are referenced in your PL/SQL source code.
The utility use human-readable PL/SQL source code as input, and writes out portable binary
object code (somewhat larger than the original). The binary code can be distributed without
fear of exposing your proprietary algorithms and methods. Oracle will still understand and
know how to execute the code. Just be careful, there is no "decode" command available. So,
don't lose your source!
The syntax is:
wrap iname=myscript.pls oname=xxxx.plb
Please note: there is no legal way to unwrap a *.plb binary file. You are supposed to backup
and keep your *.pls source files after wrapping them. However it is possible for skilled
hackers to unwrap your wrapped Oracle PL/SQL code.
8.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, name, type
from dba_source
where line = 1
and instr(text, ' wrapped'||chr(10))+instr(text, ' wrapped '||chr(10)) > 0
order by 1, 2, 3
/
9.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.
To display an empty line, it is better to use new_line procedure than put_line with an empty
string.
10.Can one read/write files from PL/SQL?
The UTL_FILE database package can be used to read and write operating system files.
A DBA user needs to grant you access to read from/ write to a specific directory before using
this package. Here is an example:
CONNECT / AS SYSDBA
CREATE OR REPLACE DIRECTORY mydir AS '/tmp';
GRANT read, write ON DIRECTORY mydir TO scott;
Provide user access to the UTL_FILE package (created by catproc.sql):
GRANT EXECUTE ON UTL_FILE TO scott;
Copy and paste these examples to get you started:
Write File
DECLARE
fHandler UTL_FILE.FILE_TYPE;
BEGIN
fHandler := UTL_FILE.FOPEN('MYDIR', 'myfile', 'w');
UTL_FILE.PUTF(fHandler, 'Look ma, Im writing to a file!!!\n');
UTL_FILE.FCLOSE(fHandler);
EXCEPTION
WHEN utl_file.invalid_path THEN
raise_application_error(-20000, 'Invalid path. Create directory or set UTL_FILE_DIR.');
END;
/
Read File
DECLARE
fHandler UTL_FILE.FILE_TYPE;
buf
varchar2(4000);
BEGIN
fHandler := UTL_FILE.FOPEN('MYDIR', 'myfile', 'r');
UTL_FILE.GET_LINE(fHandler, buf);
dbms_output.put_line('DATA FROM FILE: '||buf);
UTL_FILE.FCLOSE(fHandler);
EXCEPTION
WHEN utl_file.invalid_path THEN
raise_application_error(-20000, 'Invalid path. Create directory or set UTL_FILE_DIR.');
END;
/
NOTE: UTL_FILE was introduced with Oracle 7.3. Before Oracle 7.3 the only means of
writing a file was to use DBMS_OUTPUT with the SQL*Plus SPOOL command.
11.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 IMMEDIATE" statement (native SQL). Examples:
begin
EXECUTE IMMEDIATE 'CREATE TABLE X(A DATE)';
end;
begin execute Immediate 'TRUNCATE TABLE emp'; end;
DECLARE
var VARCHAR2(100);
BEGIN
var := 'CREATE TABLE temp1(col1 NUMBER(2))';
EXECUTE IMMEDIATE var;
END;
NOTE: The DDL statement in quotes should not be terminated with a semicolon.
Users running Oracle versions below Oracle 8i can look at the DBMS_SQL package (see
FAQ about Dynamic SQL).
12. 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 the statements within quotes are NOT semicolon terminated:
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;
/
/
%ROWTYPE
%ROWTYPE is used to declare a record with the same types as found in the specified
database table, view or cursor. Examples:
DECLARE
v_emp emp%ROWTYPE;
BEGIN
v_emp.empno := 10;
v_emp.ename := 'XXXXXXX';
END;
/
14.How does one get the value of a sequence into a PL/SQL variable?
Starting with 11g you can directly do the following:
i := sq_sequence.NEXTVAL;
On the previous versions you have to use embedded SQL statements to obtain sequence
values:
select sq_sequence.NEXTVAL into :i from dual;
15.Can one execute an operating system command from PL/SQL?
There is no direct way to execute operating system commands from PL/SQL. PL/SQL doesn't
have a "host" command, as with SQL*Plus, that allows users to call OS commands.
Nevertheless, the following workarounds can be used:
Database Pipes
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. For a Pro*C example, see
chapter 8 of the Oracle Application Developers Guide.
CREATE OR REPLACE FUNCTION host_command( cmd IN VARCHAR2 )
RETURN INTEGER IS
status NUMBER;
errormsg VARCHAR2(80);
pipe_name VARCHAR2(30);
BEGIN
pipe_name := 'HOST_PIPE';
dbms_pipe.pack_message( cmd );
status := dbms_pipe.send_message(pipe_name);
RETURN status;
END;
/
External Procedure Listeners:
From Oracle 8 one can call external 3GL code in a dynamically linked library (DLL or shared
object). One can 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.
Using Java
See example at http://www.orafaq.com/scripts/plsql/oscmd.txt
DBMS_SCHEDULER
In Oracle 10g and above, one can execute OS commands via
the DBMS_SCHEDULER package. Look at this example:
BEGIN
dbms_scheduler.create_job(job_name
=> 'myjob',
job_type
=> 'executable',
job_action
=> '/app/oracle/x.sh',
enabled
=> TRUE,
auto_drop
=> TRUE);
END;
/
exec dbms_scheduler.run_job('myjob');
16.How does one loop through tables in PL/SQL?
One can make use of cursors to loop through data within tables. Look at the following nested
loops 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
Observe that the update took more time than I have patience for ;). At 20 minutes I killed the
session. It is painfully slow and should never be done. Moreover, it chewed up an entire CPU
core for the duration. If youre only updating a few rows, why do it in PL/SQL at all? I like
Tom Kytes approach (paraphrasing):
1.
2.
3.
4.
Do it in SQL.
If SQL cant do it, do it in PL/SQL.
If PL/SQL cant do it, do it in Java.
If Java cant do it ask yourself if it needs to be done.
Grant direct access on the tables to your user. Do not use roles!
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.
20.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. Note that it
is not enough to just use "similar" 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;
/