Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Stored Procedures

2 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 http://searchoracle.techtarget.com/definition/stor ed-procedure

3 Basic Syntax CREATE OR REPLACE PROCEDURE procedure_name IS BEGIN PL/SQL BODY END;

4 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.

5 Next Step IS The IS keyword signals that a PL/SQL body will follow.

6 Next Step BEGIN The BEGIN keyword signals the start of a PL/SQL body.

7 Next Step SQL BODY At least one statement is required in a PL/SQL body.

8 Next Step END; The END keyword signals the end of the PL/SQL block.

9 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.

10 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

11 SQL*Plus informs you the procedure has been created successfully and presents the SQL command prompt: Procedure created.

12 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;

13 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.

14 UPDATE a Procedure CREATE OR REPLACE PROCEDURE skeleton IS BEGIN DBMS_OUTPUT.PUT_LINE('Hello World!'); END;

15 Save as.sql file Save your file as skeleton.sql. From SQL*Plus, open your skeleton.sql file.

16 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> /

17 Message Returned SQL*Plus assures you the procedure executed successfully: PL/SQL procedure successfully completed.

18 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

19 Execute Again Execute your procedure again. From the SQL*Plus command prompt, type: SQL> EXECUTE skeleton;

20 Output printed to Screen Hello World! PL/SQL procedure successfully completed.

21 Users of Oracle Apex Apex does not allow the use of SET SERVER OUTPUT ON Begin with the DECLARE statement for PL/SQL Block

22 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;


Download ppt "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."

Similar presentations


Ads by Google