Sei sulla pagina 1di 4

1.

BASICS OF PL/SQL

SQL

PL/SQL can be used to perform processing on the server rather than the client.
PL/SQL can be used to encapsulate business rules and other complicated logic.
It provides for modularity and abstraction.
It is used in database triggers to code complex constraints, which enforce database
integrity, to log changes, and to replicate data.
PL/SQL can also be used with stored procedures and functions to provide
enhanced database security.
SQL has become the lingua franca of database access language.
It is a declarative language and is always executed on the database server.

Oracle Database Server

PL/SQL Engine

SQL Queries

SQL Statement Executor

SQL*Plus
Your PC
Relationship of SQL*Plus, PL/SQL, and Oracle
SQL*Plus is one of the most common front ends used to develop and create stored
PL/SQL procedures and functions.
In addition to Oracles tools, several third-party vendors also have tools that can be used
to develop PL/SQL code. Some of the major products in this space are:

SQL-Programmer by Sylvain Faust Inc. www.sfi-software.com


SQL-Station by Platinum Technology Inc. www.platinum.com
SQL-Navigator by Quest Software. www.quest.com
Tool for Oracle Application Developers (TOAD) by Quest Software.
www.quest.com

Some of the Oracle-supplied packages are:


DBMS_OUTPUT
DBMS_SQL
UTL_FILE
DBMS_PIPE
DBMS_ALERT

You can verify the existence of these packages by logging on as the user SYSTEM and
issuing the following query:
SELECT object_name
FROM dba_objects
WHERE owner = 'SYS'
AND object_type = 'PACKAGE';

The resulting list will show you all packages in the database owned by the user SYS.
The DBMS_OUTPUT package is the most essential to display results.
PL/SQL is referred to as a block structured language. A PL/SQL block is a syntactical
unit that might contain program code, variable declarations, error handlers, functions, and
even other PL/SQL blocks. The fundamental basis of all PL/SQL codes is the PL/SQL
block.
The Syntax for a PL/SQL Block :
DECLARE
variable_declarations
BEGIN
program_code
EXCEPTION
exception_handlers
END;

Note : When youre using PL/SQL functions, procedures, and triggers, the keyword
DECLARE is not used. When defining a function, the function specification, or function
header as it is sometimes called, begins the block. Similarly, procedure and trigger
specifications begin procedure and trigger blocks.
The semicolon at the end of the block, and at the end of each statement, is the PL/SQL
statement terminator, and signifies the end of the block.

PL/SQL blocks can be nested. One block can contain another block as in the following
example:
DECLARE
variable declarations go here
BEGIN
some program code
BEGIN
code in a nested block
EXCEPTION
exception_handling_code
END;
more program code
END;

Nesting of blocks is often done for error-handling purposes.


PL/SQL is not a standalone language. It is always used in conjunction with some other
program or tool that handles the input, output, and other user interaction.
Oracle now includes the DBMS_OUTPUT package with PL/SQL, which provides you with
some limited output capabilities. The dbms_output.put_line ( ) procedure takes exactly one
argument and generates a line of text as output from the database server. In order to see
that line of text in SQL*Plus, we execute the following command only once per session:
SQL> SET SERVEROUTPUT ON
SET SERVEROUTPUTOFF

command is used to turn off output when you dont want to see

it.
A Simple Function to return value of X :
CREATE OR REPLACE FUNCTION ss_thresh
RETURN NUMBER AS
X NUMBER;
BEGIN
X := 700;
RETURN X;
END;
/

OUTPUT
Function created

The keyword DECLARE has been replaced by the words CREATE or REPLACE FUNCTION
ss_thresh RETURN NUMBER AS. Also notice that the calls to dbms_output.put_line ( ) has been
replaced by the RETURN command, which returns the value of variable X to the caller.

The only output is a confirmation that the function has been successfully created. Typing
the slash character ( / ) on a line by itself immediately following the end of the PL/SQL
block tells SQL*Plus to send the PL/SQL code to the Oracle database for execution.
Unlike most compilers, which will display a listing of errors found in source code, Oracle
stores any errors it finds in a database table named USER_ERRORS. If you want to see the
specific details, and you may well need to retrieve the error listing, then use the
SQL*Plus command SHOW ERRORS.
Displaying the Functions Return Value :
The ss_thresh function does not have any parameters, so be sure not to add any parentheses
when you call it i.e. ss_thresh ( ) will return an error.
The table DUAL is a special Oracle table that always exists, always has exactly one row,
and always has exactly one column. Selecting the function from DUAL table causes the
function result to be displayed.
SELECT ss_thresh FROM DUAL

The advantages of pushing program logic up to the server level are :


It gives a central point of control.
Intermediate data does not have to be brought down to the client.

Potrebbero piacerti anche