Introduction To PLSQL
Introduction To PLSQL
Introduction To PLSQL
Some PL/SQL
PL/SQL programs are organised in functions, procedures and packages
(somewhat similar to Java packages). There is a limited support for object-
oriented programming. PL/SQL is based on the Ada programming lan-
guage, and as such it shares many elements of its syntax with Pascal.
SQL> BEGIN
2 dbms_output.put_line(’Welcome to PL/SQL’);
3 END;
4 /
• the put line function (in the built-in package dbms output) displays
a string in the SQL*Plus console.
You are referred to Table 2 for a list of operators, and to Table 3 for some
useful built-in functions.
to see your errors listed. If yo do not understand the error message and
you are using Oracle on UNIX, you may be able to get a more detailed
description using the oerr utility, otherwise use Oracle’s documentation
(see References section). For example, if Oracle reports “error PLS-00103”,
you should type:
Next, we will rewrite the anonymous block above as a procedure. Note that
we now use the user function to greet the user.
Once you have compiled the procedure, execute it using the EXEC command.
Both procedures and functions should remind you of Java methods. The
similarities and differences between them are outlined in Table 1.
Function Procedure Java Method
Parameters input, output input, output input
Returns
value yes no optional
Can be called
within SQL yes no
2
Procedures and functions with no parameters are not decorated with empty brackets,
like in SQL.
END LOOP;
CLOSE CUR_REVS;
END;
/
3
Cursors are conceptually similar to iterators in Java.
Function Description
String Functions
upper(s), lower(s) convert string s to upper/lower-case
initcap(s) capitalise first letter of each word
ltrim(s), rtrim(s) remove blank char. from left/right
substr(s,start,len) sub-string of length len from position start
length(s) length of s
Date Functions
sysdate current date (on Oracle server)
to date(date, format) date formatting
Number Functions
round(x) round real number x to integer
mod(n,p) n modulus p
abs(x) absolute value of x
dbms random.random() generate a random integer
Type Conversion Functions
to char() convert to string
to date() convert to date
to number() convert to number
Miscellaneous Functions
user current Oracle user
Table 3: Some Oracle built-in functions. You are referred to Oracles’s doc-
umentation (see References section) for specific usage examples.
References
You can copy & paste the following URI (note that you will need a user-
name/password to access Oracle’s web site. You can use data@base.com/database):