Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.

Slides:



Advertisements
Similar presentations
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
Advertisements

SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Chapter 4B: More Advanced PL/SQL Programming
Chapter 9: Advanced SQL and PL/SQL Topics Guide to Oracle 10g.
Distributed Application Development B. Ramamurthy.
1 JDBC Java Database Connectivity. 2 c.pdf
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
PL/SQL Agenda: Basic PL/SQL block structure
Advanced SQL: Stored Procedures Instructor: Mohamed Eltabakh 1.
Bordoloi and Bock PROCEDURES, FUNCTIONS & TRIGGERS.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
Advanced Databases Advanced PL/SQL Programming: Procedure, Function and Package.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Announcements Read JDBC Project Step 5, due Monday.
© D. Wong  Indexes  JDBC  JDBC in J2EE (Java 2 Enterprise Edition)
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
Object-Oriented Software How does it differ from procedural? How is it similar to procedural? Why has it become so popular? Does it replace or supplement.
JDBC Java and Databases, including Postgress. JDBC l Developed by Industry leaders l Three main goals: –JDBC should be an SQL-level API –JDBC should capitalize.
JDBC  The JDBC (Java Database Connectivity) API helps a Java program to access a database in a standard way  JDBC is a specification that tells the.
Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9.
Advanced SQL: Cursors & Stored Procedures
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Database Technology Jing Shen.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
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.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
Using Oracle-Supplied Packages. 2 home back first prev next last What Will I Learn? Describe two common uses for the DBMS_OUTPUT server-supplied package.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
1 Session 2 Module 3: Scrollable Resultset and Rowsets.
DAT602 Database Application Development Lecture 8 Advanced JDBC.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
A procedure is a module performing one or more actions; it does not need to return any values. The syntax for creating a procedure is as follows: CREATE.
Advanced Databases More Advanced PL/SQL Programing 1.
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Triggers, Procedures, PL/SQL. u Read Sections , 8.1, 8.5. Next u Transaction concepts, security.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and.
Stored Procedures and Functions Pemrograman Basis Data MI2183.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
JDBC Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java program to connect to any database.
JDBC.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
Lec - 14.
JDBC – Java Database Connectivity
Web Technologies IT230 Dr Mohamed Habib.
Difference between Oracle PL/SQL and MySQL
UNIT - V STORED PROCEDURE.
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
PL/SQL Scripting in Oracle:
Oracle Stored Procedures and Functions
CPSC-310 Database Systems
Advanced PL/SQL Programing
Information Management
Java API for Database Connectivity
JDBC Example.
Procedures Oracle & MySQL
Presentation transcript:

Stored procedures1 Stored procedures and functions Procedures and functions stored in the database

Stored procedures2 Layered models vs. stored procedures Ordinary layered model –user interface –functions –model –data Layered model with stored procedures –user interface –functions in DB –model + data in DB

Stored procedures3 Stored procedures / functions 313 Stored procedures /functions are –Named blocks of PL/SQL SQL DDL Assignments, if statements, loops, etc. –Syntax checked and compiled into p-code The p-code is stored in the database Stored procedures’ syntax –General syntax, page 314 –Example, page 315 Stored functions’ syntax –General syntax, page 319 –Example fig. 14-6, page 321

Stored procedures4 3 types of parameters, 314 Procedures can have 3 types of parameters –IN used for input –OUT used for output side effects  hard to read / debug the code –INOUT used for input + output –Examples scottTigerStoredProcedures.sql Functions –standard: only IN parameters –Oracle: all kinds of parameters Don’t use OUT and INOUT with functions!

Stored procedures5 Calling a stored procedure, 314 Syntax –procedureName(formalParameter1, formalParameter2, …) –Examples page ways to link formal and actual parameters –Position Like Java: 1 st parameter formal parameter linked to 1 st actual parameter, etc. Examples: –Figure 14-2, page 316 –calling insertDept in scottTigerStoredProcedures.sql –Named Syntax: formalParameterName => value Example: calling insertDept in scottTigerStoredProcedures.sql

Stored procedures6 Some PL/SQL to use in the body of stored procedures and functions call pName(parameters) –call another procedure return value –return from a function variable := value –assignment begin … end –statement group if condition then statements else statements end if –Example page 329 For loop While loop General loop –Inner exit statement

Stored procedures7 SQL statements Stored procedures / functions can contain SQL statements –select, insert, update, delete Select syntax [result: one value] –select attr into variable from … Example: searchDept, figure 14-4, page 318 Insert example –insertDept (my own example)

Stored procedures8 Cursors, 268 Cursor points to the current row. –Very much like JDBC Example. totalSalary, fig. 14-3, page 317 DECLARE cName CURSOR FOR select statement –declares the select statement –JDBC statement object OPEN cName –Executes the select statement –JDBC ResultSet rs = statement.executeQuery(…)

Stored procedures9 Exception handling Stored procedures can handles exception –Similar to Java try … catch … –Syntax page 314 –Example search_emp + searchDept, fig. 14-4, page 318 –Fig. 12-7, page 282 Predefined/named system exceptions –When others Catches exceptions not already caught –General strategy Don’t catch exceptions if you don’t know how to handle them properly Writing to the screen is usually not enough

Stored procedures10 Calling a function, 320 Functions can be called from –PL/SQL block (like the body of another procedure / function) Example fig. 14-7, page 321 –SQL statement Example page 323

Stored procedures11 Compiling and recompiling stored procedures, 317 Stored procedures / functions are automatically compiled when recreated. If one of the tables used in a procedures is altered the procedure / function must be recompiled –Alter procedure procedureName compile;

Stored procedures12 Packages, 323 A packages groups a set of logically connected stored procedures, functions, etc. –Kind of module Built-in packages in Oracle –STANDARD Many functions used in Oracle –DBMS_OUTPUT Put_line and other procedures Example fig. 14-7, page 321 You can create your own packages!

Stored procedures13 Package structure, 324 Package specification –Specification of procedures, functions, etc. Public part of the package Syntax page 324 Example fig , page 325 Package body –Implementation of procedures, functions, etc. Private part of the package Syntax page 325 Example fig , page 327

Stored procedures14 Calling a procedure / function in a package, 326 Name of procedure / function must be prefixed with the name of the package –PackageName.ProcedureName(…) –DBMS_OUTPUT.PUT_LINE(…) –myPackage.myProcedure(…) –Example fig , page 328

Stored procedures15 Java JDBC API Pakken java.sql –interface CallableStatement –"factoried" by a connection object CallableStatement prepareCall(String sql)CallableStatementString cst = prepareCall("{call insertDept(?, ?, ?)}"); –CallableStatement extends PreparedStatement PreparedStatement extends Statement

Stored procedures16 JDBC examples Stored procedure with IN parameters Stored procedure with IN and OUT parameters Stored function –CallableStatementExample.java

Stored procedures17 Handling parameters IN parameters –handled like parameters to prepared statements cstm.setString(1, 'Anders'); cstm.setXxx(position, value); OUT parameters + results from functions –register type before executing the call cstm.registerOutParameter(position, type) –results can be obtained after executing the call value = cstm.getXxx(position)