Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.

Slides:



Advertisements
Similar presentations
® IBM Software Group © IBM Corporation QUY Thai Duy – ITFac DLU Lesson 12: SQL PL Stored Procedures.
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
Virtual training week 4 structured query language (SQL)
Stored Procedure Language Stored Procedure Overview Stored Procedure is a function in a shared library accessible to the database server can also write.
CSC343 – Introduction to databases – A. Vaisman1 Database Application Development.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Jump-Start Embedded SQL into RPG Presented by: Robert Arce.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Agenda Journalling More Embedded SQL. Journalling.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
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.
Dinamic SQL & Cursor. Why Dinamic SQL ? Sometimes there is a need to dynamically create a SQL statement on the fly and then run that command. This can.
Stored Procedures, Transactions, and Error-Handling
Module 1: Introduction to Transact-SQL
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CSC 2720 Building Web Applications Database and SQL.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
1. 1. Which type of argument passes a value from a procedure to the calling program? A. VARCHAR2 B. BOOLEAN C. OUT D. IN 2.
Lecture 8 Creating Stored Functions. Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of.
Advanced SQL: Cursors & Stored Procedures
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Stored Procedure James Wang.
PRACTICE OVERVIEW PL/SQL Part Examine this package specification and body: Which statement about the V_TOTAL_BUDGET variable is true? A. It must.
Advanced SQL Instructor: Mohamed Eltabakh 1 Part II.
ICS 321 Fall 2010 SQL in a Server Environment (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 11/1/20101Lipyeow.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
CMPE 226 Database Systems October 7 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak
Dr Gordon Russell, Napier University Unit Embedde SQL - V2.0 1 Embedded SQL Unit 5.1.
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
SQL:2003 An Introduction to Some New Features. New Data Types BIT and BIT VARYING removed BIGINT, MULTISET and XML introduced –First class data types:
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Function, Trigger used in PosgreSQL.
1 Announcements Reading for next week: Chapter 4 Your first homework will be assigned as soon as your database accounts have been set up.  Expect an .
Visual Basic CDA College Limassol Campus COM123 Visual Programming 1 Semester B Lecture:Pelekanou Olga Week 5: Useful Functions and Procedures.
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Views, Sequence, and Stored Procedure used in PosgreSQL.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts - 6 th Edition Procedural Constructs in SQL Chapter 5.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Constraints Review. What is a constraint? Unique – forbids duplicate values Referencial – Foreign key Check Constraint – sets restrictions on data added.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
1 Stored Procedures in MySQL Part I. 2 Objectives SQL Vs. MySQL SP MySQL SP Parameters MySQL SP Control Structures.
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
1. Advanced SQL Functions Procedural Constructs Triggers.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Chapter 4 An Introduction to SQL.
Views, Stored Procedures, Functions, and Triggers
Chapter 5: Advanced SQL Database System concepts,6th Ed.
Database Application Development
Introduction to Oracle9i: SQL
CPSC-310 Database Systems
Database Application Development
The Basics of Data Manipulation
مقدمة في قواعد البيانات
SQL .. An overview lecture3.
PRACTICE OVERVIEW PL/SQL Part - 1.
Information Management
Chapter 8 Advanced SQL Pearson Education © 2009.
SQL Transactions 4/23/2019 See scm-intranet.
Stored Procedure Language
Presentation transcript:

Stored Procedures Week 9

Test Details

Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has its own language called SQL Procedural Language (SPL) An SPL procedure can call another SPL procedure Execute using the CALL statement in Embedded SQL

Stored Procedures With Stored Procedures, native iSeries code can be executed from any SQL machine on the network Reduce, Reuse, Recycle!

Using an HLL program as a Stored Procedure Must execute a Create Procedure Statement Create Procedure MyProcX ( In Parm1 Int, InOut Parm2 Char(10), Out RtmMsgId Char(7))) Language RPGLE Specific MyProcXParam3 Reads SQL Data External name MyPgmX Parameter Style General This statement creates a stored procedure from the RPG program, MyPgmX. MyPgmX has 3 parameters

Language language Indicates which language the stored procedure division is written in. Possible values: –RPGLE –CobolLE –C –SQL

Specific specific-name A unique name within the collection Some procedures may have the same name, but a different number of parameters –Specific name is used in this case to differentiate these procedures

[Not] Deterministic Not deterministic –For programs that may return different results for the same argument values. Use this if you’re not sure. –The default value Deterministic –For programs that return the same results for the same argument values and that have no side effects

Reads SQL Data, Modifies SQL, Contains SQL, No SQL Reads SQL Data –Program may use SQL to read data. Cannot change data –Cannot use Delete, Insert, Update, Commit, Rollback, Set Transaction, Create, Drop, Alter Table, Rename, Comment on, Label on, Grant, Revoke, Connect, Disconnect, Release, or Set Connections

Reads SQL Data, Modifies SQL, Contains SQL, No SQL Modifies SQL Data –Program may use SQL to read and/or modify data –Cannot use Commit, Rollback, Set Transaction, Connect, Disconnect, Release or Set Connection Contains SQL –Program can contain SPL statements No SQL –Program should not contain any SQL statements

External Name Hll-program-name Specifies the name of the HLL program name If no program name is given, then the default is the procedure name

Parameter Style style Possible values: –General One parameter for each parameter in the procedure’s parameter list

Parameter Style style –SQL Used for programs that are to be called from SQL, JDBC or ODBC Following parameters are passed: –One parameter for each parameter in the parameter list –One indicator variable (InOut SmallInt) for each parameter in the list –InOut CHAR (5) – SQLState –In VarChar (517) – Fully qualified procedure name –In VarChar (128) – Specific name of the procedure –InOut VarChar(70) – message text

Remove a procedure definition from the SQL catalog Drop Procedure AppDta.MyProcX If more than one procedure has the same name, then: –Drop Procedure AppDta.MyProcX (Int, Char(10), Char(7) Or –Drop specific procedure AppDta.MyProcXparam3 You must drop a procedure before you can recreate it

Create a procedure to call CC100C Visual Basic?

SPL

SPL - Declarations SPL supports all data types available in the Create Table statement SPL does not support User Defined Types SPL does not support Arrays Declare QtyToXfr Int Default 0; Creates a variable called QtyToXfr as an integer and assigns 0 as the default value

SPL - Assignment Set XfrQtyActual = 0; Test a local variable or parameter If QtyToXfr is not Null then…

SPL – Data definition and Manipulation Statement Use SQL data definition and manipulation statements similar to embedded SQL. You don’t need to use Exec SQL and End- Exec. You don’t need to prefix variables with an ‘:’.

SPL – Conditional (Selection) Statements If statements If XfrPartId = 123 then set QtyToXfr = 1; ElseIf XfrToRqs > 100 then set QtyToXfr = 100; Else set QtyToXfr = XfrQtyRqs; End if; The above statement has 3 possible conditions –If the part number = 123 –If the XfrToRqs > 100 –otherwise

SPL – Conditional (Selection) Statements Case Statements Case PartId When 123 Set QtrToXfr = 1; When 234 Set QtrToXfr = 10; Else Set QtyToXfr = XfrQtyRqs; End Case; This statement has 3 conditions –If PartId = 123 –If PartId = 234 –otherwise

SPL – Conditional (Selection) Statements Case Statements Case When XfrPartId = 123 then Set QtyToXfr = 1; When XfrPartId = 234 then Set QtyToXfr = 100; Else Set QtyToXfr = XfrQtyRqs; End Case; This condition has 3 conditions –If XfrPartId = 123 –If XfrpartId = 234 –Otherwise

SPL – Loop Control Structures (Iteration) 4 loop structures –Loop infinite loop (use the Leave statement to exit) –While Test is at the beginning of the loop –Repeat Test is at the end of the loop –For Iterate over a set of rows

SPL – Loop Control Structures (Iteration) Loop Read: Loop If EOF = ‘Y’ then Leave Read; End If; End Loop Read;

SPL – Loop Control Structures (Iteration) While While (EOF = ‘N’) Do End While;

SPL – Loop Control Structures (Iteration) Repeat Until (EOF = ‘Y’) End Repeat;

SPL – Loop Control Structures (Iteration) For For InvRow as InvCursor for Select Qty From Inventory Where PartId = CurPartId Do End For; Builds a cursor full of data, reads a record from the cursor, process it, and then reads the next record. This loop continues until all the records in the cursor are processed.