Persistent Stored Modules (Stored Procedures) : PSM



Similar documents
SQL/PSM. Outline. Database Application Development Oracle PL/SQL. Why Stored Procedures? Stored Procedures PL/SQL. Embedded SQL Dynamic SQL

Darshan Institute of Engineering & Technology PL_SQL

PL/SQL (Cont d) Let s start with the mail_order database, shown here:

An Introduction to PL/SQL. Mehdi Azarmi

ORACLE 9I / 10G / 11G / PL/SQL COURSE CONTENT

SQL Programming. CS145 Lecture Notes #10. Motivation. Oracle PL/SQL. Basics. Example schema:

1 Stored Procedures in PL/SQL 2 PL/SQL. 2.1 Variables. 2.2 PL/SQL Program Blocks

Oracle PL/SQL Language. CIS 331: Introduction to Database Systems

Introduction to PL/SQL Programming

Oracle Database: SQL and PL/SQL Fundamentals

DECLARATION SECTION. BODY STATEMENTS... Required

Oracle 10g PL/SQL Training

Oracle Database: SQL and PL/SQL Fundamentals NEW

EXEC SQL CONNECT :userid IDENTIFIED BY :passwd;

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

A basic create statement for a simple student table would look like the following.

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: Program with PL/SQL

PL/SQL: Introduction. Overview. Anonymous Blocks and Basic Language Features

Database Programming with PL/SQL: Learning Objectives

Real SQL Programming. Persistent Stored Modules (PSM) PL/SQL Embedded SQL

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Oracle Database: Program with PL/SQL

COMS20700 DATABASES 13 PL/SQL. COMS20700 Databases Dr. Essam Ghadafi

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Oracle Database: Program with PL/SQL

Oracle Database: Program with PL/SQL

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Oracle 11g PL/SQL training

Performance Implications of Various Cursor Types in Microsoft SQL Server. By: Edward Whalen Performance Tuning Corporation

Oracle Database: Develop PL/SQL Program Units

Oracle Database: Program with PL/SQL

CSC 443 Database Management Systems. The SQL Programming Language

Programming Database lectures for mathema

Oracle Database 10g: Program with PL/SQL

A Brief Introduction to MySQL

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

Oracle For Beginners Page : 1

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Training Guide. PL/SQL for Beginners. Workbook

Database programming 20/08/2015. DBprog news. Outline. Motivation for DB programming. Using SQL queries in a program. Using SQL queries in a program

How To Name A Program In Apl/Sql

Oracle PL/SQL Injection

STUDY OF PL/SQL BLOCK AIM: To Study about PL/SQL block.

PL/SQL Overview. Basic Structure and Syntax of PL/SQL

Intro to Embedded SQL Programming for ILE RPG Developers

Course -Oracle 10g SQL (Exam Code IZ0-047) Session number Module Topics 1 Retrieving Data Using the SQL SELECT Statement

PL / SQL Basics. Chapter 3

14 Triggers / Embedded SQL

Handling Exceptions. Copyright 2008, Oracle. All rights reserved.

Why programming extensions? to program complex update transactions. where referential integrity is not addressed by data definition

5.1 Database Schema Schema Generation in SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

PL/SQL. Database Procedural Programming PL/SQL and Embedded SQL. Procedures and Functions

Writing Control Structures

The Sins of SQL Programming that send the DB to Upgrade Purgatory Abel Macias. 1 Copyright 2011, Oracle and/or its affiliates. All rights reserved.

Developing SQL and PL/SQL with JDeveloper

Oracle Database 10g Express

Mul$media im Netz (Online Mul$media) Wintersemester 2014/15. Übung 03 (Nebenfach)

Topics Advanced PL/SQL, Integration with PROIV SuperLayer and use within Glovia

Duration Vendor Audience 5 Days Oracle Developers, Technical Consultants, Database Administrators and System Analysts

Instant SQL Programming

Oracle Database 11g: Program with PL/SQL

1 Structured Query Language: Again. 2 Joining Tables

CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University

Multimedia im Netz Online Multimedia Winter semester 2015/16

Tutorial on Operations on Database using JDeveloper

Real SQL Programming 1

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

3.GETTING STARTED WITH ORACLE8i

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

Triggers & Packages. {INSERT [OR] UPDATE [OR] DELETE}: This specifies the DML operation.

Oracle SQL. Course Summary. Duration. Objectives

Handling PL/SQL Errors

Database Query 1: SQL Basics

CS346: Database Programming.

Maintaining Stored Procedures in Database Application

USING FILERELICATIONPRO TO REPLICATE SQL SERVER

ISYS PL/SQL Basics. Week 03

Handling Exceptions. Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 8-1

Oracle For Beginners Page : 1

Package sjdbc. R topics documented: February 20, 2015

Oracle Database 12c: Introduction to SQL Ed 1.1

SQL. Short introduction

Answers to the Try It Yourself Sections

An Oracle White Paper June Migrating Applications and Databases with Oracle Database 12c

Handling Exceptions. Schedule: Timing Topic 45 minutes Lecture 20 minutes Practice 65 minutes Total

Migrate Topaz databases from One Server to Another

Database security tutorial. Part I

Oracle SQL Developer for Database Developers. An Oracle White Paper June 2007

How To Create A Table In Sql (Ahem)

Connecting to a Database Using PHP. Prof. Jim Whitehead CMPS 183, Spring 2006 May 15, 2006

Information Systems SQL. Nikolaj Popov

Chapter 39. PL/pgSQL - SQL Procedural Language

Oracle Database 10g: Introduction to SQL

Migrating from Sybase to SQL Server

MYSQL DATABASE ACCESS WITH PHP

Python. Data bases. Marcin Młotkowski. 8th May, 2013

Database Management Systems Comparative Study: Performances of Microsoft SQL Server Versus Oracle

Transcription:

Persistent Stored Modules (Stored Procedures) : PSM 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 etc through cursors Reuse the application logic 1

Stored Procedures in Oracle Oracle supports a slightly different version of PSM called PL/SQL mysql support is only in later versions Defining a stored procedure CREATE PROCEDURE <procedurename> [(<paramlist>)] <localdeclarations> <procedurebody>; A parameter in the paramlist is specified as: <name> <mode> <type> <mode> is one of {IN, OUT, INOUT} eg: val1 IN int You can drop procedure by DROP PROCEDURE <procedurename> In PL/SQL, you can replace procedure by CREATE OR REPLACE PROCEDURE <procedurename> 2

PL/SQL Engine 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; 3

More about Procedures If there is an error in your procedure, Oracle will give you a warning. Use command SHOW ERRORS to show the errors in your procedure. Calling Procedures call <procedurename> [(<paramlist>)]; Example CREATE PROCEDURE testprocedure (num IN int, name IN varchar) IS. BEGIN /* Insert values */ INSERT INTO Student VALUES (num, name); run; END; 4

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;. run; Other PSM features Assignment statements: PL/SQL <varname> := <expression> 5

Control Structures: IF THEN ELSE IF <condition> THEN <statementlist> ELSIF <condition> THEN <statementlist> ELSIF ELSE <statementlist> END IF; Loops LOOP <statementlist> END LOOP; To exit from a loop use EXIT; 6

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 > 15) THEN EXIT; END IF; END LOOP; END;. run; FOR Loops FOR i in [REVERSE] <lowerbound>.. <upperbound> LOOP <statementlist> END LOOP Example: FOR i in 1.. 5 LOOP INSERT INTO Student (snumber) values (10 + i); END LOOP; 7

WHILE LOOPS WHILE <condition> LOOP <statementlist> END LOOP; Functions CREATE FUNCTION <functionname> [(<paramlist>)] RETURNS type IS <localdeclarations> BEGIN <functionbody>; END; You can call a function as part of a sql expression Drop a function: drop function <functionname> 8

Functions: Example CREATE FUNCTION testfunction RETURN int IS num1 int; BEGIN SELECT MAX (snumber) INTO num1 FROM Student; RETURN num1; END;. run; SELECT * from Student where snumber = testfunction (); Other useful tips Oracle stores procedures and functions in catalog as relational tables. Check user_procedures Check user_functions You may run queries etc against them such as describe user_procedures; select object_name from user_procedures; 9

Cursors When we execute a statement, a relation is returned. It is stored in private work area for the statement. Cursor is a pointer to this area. To create a cursor CURSOR c_customers is SELECT * from CUSTOMERS; Cursors We can open the cursor. OPEN c_customers; We can select data from the cursor. FETCH c_customers into customers_rec; And we can close the cursor. CLOSE c_customers; 10

Implicit & Explicit Cursors Every SQL data manipulation statements including queries that return only one row is an implicit cursor. An explicit cursor is what we create. For queries that return more than one row, you must declare an explicit cursor CREATE OR REPLACE PROCEDURE copyprocedure IS stid INT; name VARCHAR (10); CURSOR mycursor IS SELECT * FROM STUDENT; BEGIN OPEN mycursor; LOOP FETCH mycursor INTO stid, name; EXIT WHEN mycursor%notfound; INSERT INTO newstudent VALUES (stid, name); END LOOP; CLOSE mycursor; END; 11

Cursor Attributes The SQL cursor attributes are :- %ROWCOUNT: The number of rows processed by a SQL statement. %FOUND : TRUE if at least one row was processed. %NOTFOUND : TRUE if no rows were processed. %ISOPEN : TRUE if cursor is open or FALSE if cursor has not been opened or has been closed. Only used with explicit cursors. Advanced Explicit Cursor Concepts 12

Cursor that uses parameters CURSOR c_students (p_department classes.department%type p_course classes.department%type ) IS SELECT * FROM classes WHERE department = p_department AND course = p_course; To call the cursor OPEN c_students('cs',101); Cursors for update The syntax for this parameter in the SELECT statement is: SELECT... FROM... FOR UPDATE [OF column_reference] [NOWAIT] where column_reference is a column in the table against which the query is performed. A list of columns can also be used. 13

Example for update DECLARE CURSOR c_allstudents IS SELECT * FROM students FOR UPDATE OF first_name, last_name; Or the cursor can select every column by not specifing a range DECLARE CURSOR c_allstudents IS SELECT * FROM students FOR UPDATE; NOWAIT If another session already has locks on the rows in the active set, then the SELECT FOR UPDATE will hang until the other session releases the lock. To handle this situation the parameter NOWAIT is available, which in case the rows are locked,open will return the error ORA-54 resource busy and acquire with NOWAIT specified 14