Stored Procedures
Definition a stored procedure is a set of Structured Query Language (SQL) statements with an assigned name that's stored in the database in compiled form so that it can be shared by a number of programs.SQL Reference: Author Unknown (2003). What is a Stored Procedure. Retrieved on February 3, 2010 from ed-procedure
Basic Syntax CREATE OR REPLACE PROCEDURE procedure_name IS BEGIN PL/SQL BODY END;
Step Through CREATE OR REPLACE PROCEDURE procedure_name CREATE OR REPLACE PROCEDURE is an SQL statement that instructs Oracle to create a procedure called “whatever name we give it”, and to overwrite it if it exists.
Next Step IS The IS keyword signals that a PL/SQL body will follow.
Next Step BEGIN The BEGIN keyword signals the start of a PL/SQL body.
Next Step SQL BODY At least one statement is required in a PL/SQL body.
Next Step END; The END keyword signals the end of the PL/SQL block.
Example 1 CREATE OR REPLACE PROCEDURE skeleton 2 IS 3 BEGIN 4 NULL; 5* END; SQL> This does not really do anything. But, remember at least one statement is required in the Block.
When Executing from SQL Plus SQL*Plus loads the contents of your skeleton.sql file into the SQL*Plus buffer or memory area and presents the SQL*Plus command prompt: Type / - Execute the contents of the SQL*Plus buffer. Type a front slash and press
SQL*Plus informs you the procedure has been created successfully and presents the SQL command prompt: Procedure created.
Run a Procedure in SQL Plus SQL> EXECUTE procedure_name; SQL*Plus assures you the procedure executed successfully: PL/SQL procedure successfully completed. For the example: EXECUTE skeleton;
Another method to run it in SQL Plus You can also run your procedure from within an unnamed PL/SQL block. At the SQL*Plus command prompt, it looks like this: SQL> BEGIN 2 SKELETON; 3 END; 4 / PL/SQL procedure successfully completed.
UPDATE a Procedure CREATE OR REPLACE PROCEDURE skeleton IS BEGIN DBMS_OUTPUT.PUT_LINE('Hello World!'); END;
Save as.sql file Save your file as skeleton.sql. From SQL*Plus, open your skeleton.sql file.
Load into Buffer and Execute SQL*Plus loads the contents of your skeleton.sql file into the SQL*Plus buffer or memory area and presents the SQL*Plus command prompt: Execute the contents of the SQL*Plus buffer. Type a front slash and press like this: SQL> /
Message Returned SQL*Plus assures you the procedure executed successfully: PL/SQL procedure successfully completed.
In order for it to Print Output A SET command is needed before output is shown from the DBMS_OUTPUT.PUT_LINE procedure. So let's do that. At the SQL*Plus command prompt, type: SQL> SET SERVEROUTPUT ON
Execute Again Execute your procedure again. From the SQL*Plus command prompt, type: SQL> EXECUTE skeleton;
Output printed to Screen Hello World! PL/SQL procedure successfully completed.
Users of Oracle Apex Apex does not allow the use of SET SERVER OUTPUT ON Begin with the DECLARE statement for PL/SQL Block
Apex Users Note Because Exec is a SQL*Plus command you have to wrap the call to the stored procedure in a Begin command like the following if using Apex: Begin Procedure_name; end;