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



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

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

Persistent Stored Modules (Stored Procedures) : PSM

Darshan Institute of Engineering & Technology PL_SQL

Oracle For Beginners Page : 1

When an exception occur a message which explains its cause is received. PL/SQL Exception message consists of three parts.

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

An Introduction to PL/SQL. Mehdi Azarmi

Chapter 13. Introduction to SQL Programming Techniques. Database Programming: Techniques and Issues. SQL Programming. Database applications

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

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

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

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

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

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

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

CS346: Database Programming.

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

Chapter 9, More SQL: Assertions, Views, and Programming Techniques

Chapter 8: Introduction to PL/SQL 1

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

Oracle Database: SQL and PL/SQL Fundamentals

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

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

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

Creating PL/SQL Blocks. Copyright 2007, Oracle. All rights reserved.

Oracle Database: SQL and PL/SQL Fundamentals NEW

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

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

Database Programming. Week *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

Oracle Database: SQL and PL/SQL Fundamentals

Handling PL/SQL Errors

Oracle Database 10g: Program with PL/SQL

Oracle For Beginners Page : 1

Oracle Database: Program with PL/SQL

DECLARATION SECTION. BODY STATEMENTS... Required

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

Oracle Database: Develop PL/SQL Program Units

Database Programming with PL/SQL: Learning Objectives

Training Guide. PL/SQL for Beginners. Workbook

Oracle Database: Program with PL/SQL

Introduction to PL/SQL Programming

Oracle Database: Program with PL/SQL

Oracle Database: Program with PL/SQL

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

Writing Control Structures

Oracle Database 11g Express Edition PL/SQL and Database Administration Concepts -II

Oracle 11g PL/SQL training

Oracle Database: Program with PL/SQL

Oracle 10g PL/SQL Training

14 Triggers / Embedded SQL

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

CSC 443 Database Management Systems. The SQL Programming Language

PL/SQL MOCK TEST PL/SQL MOCK TEST I

Why Is This Important? Database Application Development. SQL in Application Code. Overview. SQL in Application Code (Contd.

SQL: Programming. Introduction to Databases CompSci 316 Fall 2014

SQL: Queries, Programming, Triggers

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

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

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

Answers to the Try It Yourself Sections

PL/SQL Programming Concepts: Review. Copyright 2004, Oracle. All rights reserved.

Oracle8/ SQLJ Programming

Oracle PL/SQL Injection

More SQL: Assertions, Views, and Programming Techniques

Programming Database lectures for mathema

Chapter 39. PL/pgSQL - SQL Procedural Language

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

Oracle Database: SQL and PL/SQL Fundamentals NEW

Handling PL/SQL Errors

Maintaining Stored Procedures in Database Application

Oracle Database 11g: Program with PL/SQL

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

Oracle SQL. Course Summary. Duration. Objectives

T-SQL STANDARD ELEMENTS

Query-by-Example (QBE)

SQL Server Database Coding Standards and Guidelines

Intro to Embedded SQL Programming for ILE RPG Developers

SQL and Programming Languages. SQL in Programming Languages. Applications. Approaches

Querying Microsoft SQL Server

Overview of PL/SQL. About PL/SQL

Oracle Database 10g: Introduction to SQL

Oracle Database 12c: Introduction to SQL Ed 1.1

What is ODBC? Database Connectivity ODBC, JDBC and SQLJ. ODBC Architecture. More on ODBC. JDBC vs ODBC. What is JDBC?

Week 1 Part 1: An Introduction to Database Systems. Databases and DBMSs. Why Use a DBMS? Why Study Databases??

Introduction to Oracle PL/SQL Programming V2.1 - Lessons 11-End

Best Practices for Dynamic SQL

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

Stored Routines in Transactional Context

Instant SQL Programming

Course ID#: W 35 Hrs. Course Content

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

Querying Microsoft SQL Server 20461C; 5 days

Developing SQL and PL/SQL with JDeveloper

Relational Algebra and SQL

Database Application Development. Overview. SQL in Application Code. Chapter 6

Chapter 5. SQL: Queries, Constraints, Triggers

Advanced SQL. Jim Mason. Web solutions for iseries engineer, build, deploy, support, train

Transcription:

Outline Embedded SQL Dynamic SQL Many host languages: C, Cobol, Pascal, etc. JDBC (API) SQLJ (Embedded) Java Database Application Development Oracle PL/SQL Stored procedures CS430/630 Lecture 15 Slides based on Database Management Systems 3 rd ed, Ramakrishnan and Gehrke Why Stored Procedures? Stored Procedures So far, all data processing is done at the client Lots of data may have to be transferred Functionality (code) replicated at each client Lots of state (e.g., locks, transaction data) at the DBMS While client processes the data Stored procedures execute in same process space as DBMS Encapsulates application logic and is close to the data Reuse of common functionality by different clients Vendors introduced their own procedural extensions e.g., Oracle s PL/SQL SQL/PSM SQL Persistent Stored Modules SQL standard for stored procedures, available in SQL:2003 Commercial vendors may offer own extensions of PSM Standard language for stored procedures Supports both procedures and functions Functions can return results through RETURN statement Procedures can return results in parameters PL/SQL In this course we focus on Oracle PL/SQL

PL/SQL (Procedural Language SQL) Procedural extension to SQL developed by Oracle Most prominent DBMS procedural language Another language is T-SQL from Microsoft (MS SQL) Only DML allowed in PL/SQL DDL such as creating or dropping tables NOT allowed Basic program structure is a block There can be nested blocks PL/SQL Program Structure variable_declarations procedural_code EXCEPTION error_handling PL/SQL syntax is not case sensitive (variable names as well) PL/SQL in SQL Plus Ensure output goes to screen SET SERVEROUTPUT ON Executing PL/SQL in command line / DBMS_OUTPUT.PUT_LINE( Hello World ); The / must be by itself on separate line DBMS_OUTPUT.PUT_LINE equivalent of printf() in C or System.out.println() in Java Data Types It is possible to use ORACLE SQL types NUMBER, VARCHAR, etc PL/SQL allows directly referring to a column type tablename.columnname%type e.g, SAILORS.SNAME%TYPE Also possible to define a row type (e.g., tuple) tablename%rowtype Declaring a variable: <var_name> <TYPE>; sailor_rec SAILORS%ROWTYPE; Can later refer to individual fields using column names DBMS_OUTPUT.PUT_LINE( Name: sailor_rec.name Age: sailor_rec.age); means string concatenation (like + in Java) Assignments and Branches Assignment A := B + C; Branch IF condition THEN ; ELSIF (condition) ; ELSIF ELSE ; Branch Example A NUMBER(6) := 10; B NUMBER(6); A := 23; B := A * 5; IF A < B THEN DBMS_OUTPUT.PUT_LINE(A is less than B); ELSE DBMS_OUTPUT.PUT_LINE(B is less-or-equal than A); Output is: 23 is less than 115

Branch Example (2) NGRADE NUMBER; LGRADE CHAR(2); NGRADE := 82.5; IF NGRADE > 95 THEN LGRADE := A+ ; ELSIF NGRADE > 90 THEN LGRADE := A ; ELSIF NGRADE > 85 THEN LGRADE := B+ ; ELSIF NGRADE > 80 THEN LGRADE := B ; ELSE LGRADE := F ; Loops IF condition THEN EXIT; EXIT WHEN condition; Loop Example Loop Variants J NUMBER(6); J := 1; DBMS_OUTPUT.PUT_LINE( J= J); J := J + 1; EXIT WHEN J > 5; DBMS_OUTPUT.PUT_LINE( J= J); Output =? WHILE condition various_ FOR counter IN startvalue.. endvalue various_ For Loop Example FOR K IN 1..5 DBMS_OUTPUT.PUT_LINE( K= K); SQL Statements Data can be manipulated (DML) from PL/SQL SELECT must have INTO when cursors not used SID NUMBER(6); SID := 20; INSERT INTO Sailors (sid, name) VALUES (SID, Rusty ); SID := SID + 1; INSERT INTO Sailors (sid, name) VALUES (SID, Yuppy );

SQL Statements retrieving data As before, there are two cases 1. Single-tuple result (the easy case) SELECT selectfields INTO declared_variables FROM table_list WHERE search_criteria; VAR_NAME Sailors.name%TYPE; VAR_AGE Sailors.age%TYPE; SELECT name, age INTO VAR_NAME, VAR_AGE FROM Sailors WHERE SID = 10; DBMS_OUTPUT.PUT_LINE( Age of VAR_NAME is VAR_AGE); SQL Statements retrieving data 2. Multiple-tuples result: cursors are needed CURSOR cursorname IS SELECT_statement; OPEN cursorname; FETCH cursorname INTO variable_list; CLOSE cursorname; Cursor Example S Sailors%ROWTYPE; CURSOR SAILORCURSOR IS SELECT * FROM Sailors; OPEN SAILORCURSOR; FETCH SAILORCURSOR INTO S; EXIT WHEN SAILORCURSOR %NOTFOUND; DBMS_OUTPUT.PUT_LINE( AGE OF S.sname IS S.age); CLOSE SAILORCURSOR ; Cursor Attributes %NOTFOUND: Evaluates to TRUE when cursor has no more rows to read. FALSE otherwise %FOUND: Evaluates to TRUE if last FETCH was successful and FALSE otherwise %ROWCOUNT: Returns the number of rows that the cursor has already fetched from the database %ISOPEN: Returns TRUE if this cursor is already open, and FALSE otherwise Declaring a Procedure PROCEDURE procedure_name ( parameters ) IS variable declarations procedure_body Parameters can be IN, OUT or INOUT, default is IN PROCEDURE SUM_AB (A INT, B INT, C OUT INT) IS C := A + B; Declaring a Function FUNCTION function_name (function_params) RETURN return_type IS variable declarations function_body RETURN something_of_return_type; Example FUNCTION ADD_TWO (A INT, B INT) RETURN INT IS RETURN (A + B);

Exceptions Exceptions defined per block (similar to Java) Each END has its own exception handling If blocks are nested, exceptions are handled in an inside to outside fashion If no block in the nesting handles the exception, a runtime error occurs There are multiple types of exceptions Named system exceptions (most frequent) we only cover these Unnamed system exceptions User-defined exceptions Exceptions EXCEPTION WHEN ex_name1 THEN error handling WHEN ex_name2 THEN error handling WHEN Others THEN error handling Named System Exceptions Exception Name Reason Error Number CURSOR_ALREADY_OPEN When you open a cursor that is already open. INVALID_CURSOR When you perform an invalid operation on a cursor like closing a cursor or fetch data from a cursor that is not opened. ORA-06511 ORA-01001 NO_DATA_FOUND TOO_MANY_ROWS ZERO_DIVIDE When a SELECT...INTO clause does not return any row from a table. When you SELECT or fetch more than one row into a record or variable. When you attempt to divide a number by zero. ORA-01403 ORA-01422 ORA-01476