Persistent Stored Modules (Stored Procedures) : PSM - PowerPoint PPT Presentation

About This Presentation
Title:

Persistent Stored Modules (Stored Procedures) : PSM

Description:

Persistent Stored Modules (Stored Procedures) : PSM – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 18
Provided by: pcguest
Learn more at: http://web.cs.wpi.edu
Category:

less

Transcript and Presenter's Notes

Title: Persistent Stored Modules (Stored Procedures) : PSM


1
Persistent Stored Modules (Stored Procedures)
PSM
2
Stored Procedures
  • What is stored procedure?
  • SQL allows you to define procedures and functions
    and store them in the database server
  • Executed by the database server
  • Advantages
  • Complex application logic executed while close
    to the data usually implies efficiency
  • Contrast with tuple-at-a time processing by JDBC
    through cursors
  • Reuse the application logic

3
Stored Procedures in Oracle
  • Oracle supports a slightly different version of
    PSM called PL/SQL
  • mySQL support is only in later versions

4
Defining a stored procedure
  • CREATE PROCEDURE ltprocedureNamegt (ltparamListgt)
  • ltlocalDeclarationsgt
  • ltprocedureBodygt
  • A parameter in the paramList is specified as
  • ltnamegt ltmodegt lttypegt
  • ltmodegt is one of IN, OUT, INOUT
  • eg val1 IN int
  • You can drop procedure by
  • DROP PROCEDURE ltprocedureNamegt
  • In PL/SQL, you can replace procedure by
  • CREATE OR REPLACE PROCEDURE ltprocedureNamegt

5

PL/SQL Engine

6
Example Procedure in PSM
  • CREATE PROCEDURE testProcedure
  • BEGIN
  • INSERT INTO Student VALUES (5, Joe)
  • END
  • Oracle PL/SQL
  • CREATE PROCEDURE testProcedure IS
  • BEGIN
  • INSERT INTO Student VALUES (5, Joe)
  • END
  • .
  • run

7
More about Procedures
  • Calling Procedures
  • call ltprocedureNamegt (ltparamListgt)

8
Example
  • CREATE PROCEDURE testProcedure (num IN int, name
    IN varchar) IS
  • BEGIN
  • / Insert values /
  • INSERT INTO Student VALUES (num, name)
  • END

9
Local Declarations
  • Example
  • CREATE PROCEDURE testProcedure (num IN int, name
    IN varchar) IS
  • num1 int -- local variable
  • BEGIN
  • num1 10
  • INSERT INTO Student VALUES (num1, name)
  • END

10
Other PSM features
  • Assignment statements PL/SQL
  • ltvarNamegt ltexpressiongt

11
Control Structures IF THEN ELSE
  • IF ltconditiongt THEN
  • ltstatementListgt
  • ELSIF ltconditiongt THEN
  • ltstatementListgt
  • ELSIF
  • ELSE ltstatementListgt
  • END IF

12
Loops
  • LOOP
  • ltstatementListgt
  • END LOOP
  • To exit from a loop use
  • EXIT

13
Loops Example
  • CREATE PROCEDURE testProcedure (num IN int, name
    IN varchar) IS
  • num1 int
  • BEGIN
  • num1 10
  • LOOP
  • INSERT INTO Student VALUES (num1, name)
  • num1 num1 1
  • IF (num1 gt 15) THEN EXIT END IF
  • END LOOP
  • END

14
FOR Loops
  • FOR i in REVERSE ltlowerBoundgt .. ltupperBoundgt
    LOOP
  • ltstatementListgt
  • END LOOP
  • Example
  • FOR i in 1 .. 5 LOOP
  • INSERT INTO Student (sNumber) values (10 i)
  • END LOOP

15
WHILE LOOPS
  • WHILE ltconditiongt LOOP
  • ltstatementListgt
  • END LOOP

16
Functions
  • CREATE FUNCTION ltfunctionNamegt (ltparamListgt)
    RETURNS type IS
  • ltlocalDeclarationsgt
  • BEGIN ltfunctionBodygt END
  • You can call a function as part of an sql
    expression
  • Drop a function
  • drop function ltfunctionNamegt

17
Functions Example
  • CREATE FUNCTION testFunction RETURN int IS
  • num1 int
  • BEGIN
  • SELECT MAX (sNumber) INTO num1 FROM Student
  • RETURN num1
  • END
  • SELECT from Student where sNumber
    testFunction ()

18
Other useful tips
  • Oracle stores procedures and functions in catalog
    as relational tables.
  • Check user_procedures
  • Check user_functions
  • You may run queries against them such as
  • describe user_procedures
  • select object_name from user_procedures

19
Summary Stored Procedures
  • Used stand-alone on explicit call
  • Used functions in WHERE clause of SQL statement
  • Used in body of triggers
Write a Comment
User Comments (0)
About PowerShow.com