Advanced SQL: Stored Procedures - PowerPoint PPT Presentation

About This Presentation
Title:

Advanced SQL: Stored Procedures

Description:

Advanced SQL: Stored Procedures Instructor: Mohamed Eltabakh meltabakh_at_cs.wpi.edu * Today s Roadmap Views Triggers Assertions Cursors Stored Procedures Stored ... – PowerPoint PPT presentation

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

less

Transcript and Presenter's Notes

Title: Advanced SQL: Stored Procedures


1
Advanced SQL Stored Procedures
  • Instructor Mohamed Eltabakh
  • meltabakh_at_cs.wpi.edu

2
Todays Roadmap
  • Views
  • Triggers
  • Assertions
  • Cursors
  • Stored Procedures

3
Stored Procedures Functions
Views
Way to register queries inside DBMS
4
Stored Procedures Functions
  • What is stored procedure?
  • Piece of code stored inside the DBMS
  • SQL allows you to define procedures and functions
    and store them inside DBMS
  • Advantages
  • Reusability do not need to write the code again
    and again
  • Programming language-like environment
  • Assignment, Loop, For, IF statements
  • Call it whenever needed
  • From select statement, another procedure, or
    another function

5
Stored Procedures in Oracle
  • Stored procedures in Oracle follow a language
    called PL/SQL
  • PL/SQL Procedural Language SQL
  • Same language used inside DB triggers

6
Creating A Stored Procedure
CREATE OR REPLACE PROCEDURE ltprocedureNamegt
(ltparamListgt) IS AS ltlocalDeclarationsgt Begin
ltprocedureBodygt End /
7
General Structure
CREATE OR REPLACE PROCEDURE procedure_name
(parameter ,parameter) IS AS
declaration_section BEGIN
executable_section EXCEPTION
exception_section END procedure_name
Optional section for exception handling
8
Example I
9
Example II
10
Calling a Stored Procedure
  • SQLgt exec ltprocedureNamegt (ltparamListgt)

SQL gt exec remove_emp (10)
11
Printing From Stored Procedures
Taking three parameters
Printing lines to output screen
12
Features in Stored Procedures
Create Procedure profiler_control(start_stop IN
VARCHAR2,
run_comm IN VARCHAR2,

ret OUT number) AS ret_code
INTEGER BEGIN  ret_code 10   IF start_stop
NOT IN ('START','STOP') THEN    ret 0  
ELSIF start_stop 'START' THEN    ret
1    ELSE     ret ret_code   END IF END
profiler_control /
13
More Features LOOP Statement
  • CREATE PROCEDURE testProcedure (name varchar2)
    AS
  • credit_rating NUMBER 0
  • BEGIN
  • LOOP
  • credit_rating credit_rating 1
  • IF credit_rating gt 3 THEN
  • EXIT
  • END IF
  • END LOOP
  • -- control resumes here
  • IF name gt abc THEN
  • RETURN
  • END IF
  • DBMS_OUTPUT.PUT_LINE ('Credit rating '
    TO_CHAR(credit_rating))
  • END
  • /

The Loop statement
14
More Features CURSOR FOR Statement
Create Procedure OpeningBal (p_type IN string)
AS cursor C1 Is Select
productId, name, price From
products where type p_type
Begin For rec in C1 Loop Insert into
Temp values (rec.productId, rec.name, rec.price)
End Loop End /
15
Return Value
  • Stored procedures can set output variables
  • Stored procedures do not return values
  • Stored functions differ from procedure in that
    they return values

16
Stored Functions
  • Similar to stored procedures except that they
    return value

CREATE OR REPLACE FUNCTION ltfunctionNamegt

RETURN lttypegt (ltparamListgt) AS ltlocalDeclaratio
nsgt ltfunctionBodygt
17
Stored Functions
  • All features in stored procedures are valid in in
    stored functions
  • Functions have an extra Return statement

18
Using Stored Procedures or Functions
  • Stored Procedures
  • Called from other procedures, functions,
    triggers, or standalone
  • Stored Functions
  • In addition to above, can be used inside SELECT
    statement
  • In WHERE, HAVING, or projection list

19
Example I
CREATE FUNCTION MaxNum() RETURN number AS
num1 number BEGIN SELECT MAX (sNumber) INTO
num1 FROM Student RETURN num1 END /
SQLgt Select from Student where sNumber
MaxNum()
20
Example II
CREATE FUNCTION MaxNum(lastName_in varchar2)
RETURN number AS num1
number BEGIN SELECT MAX (sNumber) INTO num1
FROM Student Where lastName lastName_in RETU
RN num1 END /
21
Example III
CREATE FUNCTION MaxNum(lastName_in varchar2)
RETURN number AS num1
number BEGIN SELECT MAX (sNumber) INTO num1
FROM Student Where lastName lastName_in RETU
RN num1 END /
SQLgt Select MaxNum(S.lastName) from Student S
Calling the function in the projection list
22
Summary of Stored Procedures/Functions
  • Code modules that are stored inside the DBMS
  • Used and called repeatedly
  • Powerful programing language style
  • Can be called from other procedures, functions,
    triggers, or from select statement (only
    functions)

23
End of Advanced SQL
  • Views
  • Triggers
  • Assertions
  • Cursors
  • Stored Procedures/Functions
Write a Comment
User Comments (0)
About PowerShow.com