- HubPages»
- Technology»
- Computers & Software»
- Computer Software
Oracle PL/SQL Basics
Introduction
Oracle PL/SQL is well known as procedural extension of SQL with Oracle. PL/SQL gives the power of programming which is a key feature of Oracle. Though, many RDBMS has this programming capability, ORACLE PL/SQL has its own trade mark of being reliable, efficient and powerful among other RDBMS based features.
PL/SQL in ORACLE is a programming unit which can be stored as a stored procedure (compiled code) in data base or you can store it as a text file in your hard drive (aka anonymous block). Oracle has a PL/SQL engine which compiles and executes our PL/SQL code.
PL/SQL Block
A simple PL/SQL block, also called as anonymous block will have declaration section followed by a code block enclosed in between begin and end. Below is the structure of a simple PL/SQL block.
DECLARE<<local variable declaration>>BEGIN<< Your code for manipulating data will be here>>EXCEPTION<<This place you catch run time errors>>END;/
Below is a simple example of a PL/SQL Block.
DECLAREa number;b number;c number;BEGINa := 5;b := 10;c := a + b;dbms_output.put_line(´Addition of ´ || a || ´ and ´ || b || ´ is : ´ || c);END;/
Please note, Each statement should end with semi colon (;) and the assignment operator is := (not just equal symbol).
How to execute?
How to execute PL/SQL Anonymous Block mentioned above?
To execute the above example from SQL Plus, give ed (or edit) command from SQL prompt.
SQL> EDIT (press enter key)
Editor will be opened (notepad in case of windows / vi editor in case of unix). Enter or copy the above example in the editor. Save your changes and exit from editor to return to SQL Prompt. When you are returned back to SQL prompt, execute the command list (or li) to make sure you content is there in the buffer. Now, give / and enter from SQL prompt to run your PL/SQL Block (or use RUN command). You would get below output.
Addition of 5 and 10 is: 15
You would be surprised to see that not getting output in SQL Plus. For the first time, you need to turn the out put on using below command.
set serveroutput on
In case of PL/SQL Developer or any other editor, it will be very simple. Just enter or copy the PL/SQL code in the editor and hit the run button (green/play button) in the tool bar. You will get the results in a output window. TOAD is the well known and best tool for working with Oracle PL/SQL.
Tips
When you have more output and if you face any problems with output due to buffer size, use below command before executing your PL/SQL block.
set serveroutput on size unlimited
If you want to avoid truncation of your output when your result/output text exceeds maximum line size, use below command. This will make your result text to wrap when it exceeds line size.
set serveroutput on format word_wrapped
To learn more about EXCEPTION section of PL/SQL block and oracle exceptions, please refer below chapter.
Oracle PL/SQL Exceptions