2009-07-02

Basic PL/SQL: Part 1 (Anonymous)

SQL is only used to interact with database, and itself cannot be coded in procedural structure (which means no alternate or looping). To solve this weakness, Oracle provides another language called PL/SQL (Procedural Language/Structured Query Language). 4 situations that we can use PL/SQL:
  1. Anonymous, it is written in script form and saved as file, not a database object and cannot pass in any parameters or return any value.
  2. Procedure
  3. Function
  4. Trigger
Basic syntax to write an anonymous PL/SQL:

declare
[variable name] [data type] := [default value];
begin
[action to be performed];
exception
while others then
[exception handler];
end;
/

For example:

declare
v_name varchar(50) := '';
begin
select name into v_name
from emp
where id = 1;
exception
while others then
raise;
end;
/

0 comments: