Introduction to PL/SQL

Slides:



Advertisements
Similar presentations
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  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.
PL/SQL.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
PL/SQL (Procedural Language extensions to SQL) Prepared by: Manoj Kathpalia Edited by: M V Ramakrishna.
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
Chapter 4B: More Advanced PL/SQL Programming
Chapter 4: Introduction to PL/SQL. 2 Lesson A Objectives After completing this lesson, you should be able to: Describe the fundamentals of the PL/SQL.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
Introduction to PL/SQL
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
1 Introduction to PL/SQL. 2  Procedural programming language  Uses detailed instructions  Processes statements sequentially  Combines SQL commands.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Chapter 4 Cursors and Exception Handling Oracle10g Developer:
PL/SQL Anonymous Blocks
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Oracle10g Developer: PL/SQL Programming1 Objectives Programming fundamentals The PL/SQL block Define and declare variables Initialize variables The NOT.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
Chapter 4: Introduction to PL/SQL
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
LECTURE 1 INTRODUCTION TO PL/SQL Tasneem Ghnaimat.
Oracle 11g DATABASE DEVELOPMENT LAB1. Introduction  Oracle 11g Database:-  Oracle 11g database is designed for some features, which helps to the organizations.
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
PL/SQL Chapter 8.
Oracle PL/SQL. PL/SQL Originally modeled after ADA Originally modeled after ADA Created for Dept. of DefenseCreated for Dept. of Defense Allows expanded.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
Trapping Oracle Server Exceptions. 2 home back first prev next last What Will I Learn? Describe and provide an example of an error defined by the Oracle.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
WRITING CONTROL STRUCTURES (CONDITIONAL CONTROL).
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
1 Handling Exceptions Part F. 2 Handling Exceptions with PL/SQL What is an exception? Identifier in PL/SQL that is raised during execution What is an.
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
1 Writing Control Structures Part D. 2 PL/SQL Decision Control Structures Sequential processing Sequential processing Processes statements one after another.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
implicit and an explicit cursor
1 ITBIS373 Database Development Lecture 2 – Chapter 4B Introduction to PL/SQL.
1 Stored Procedures in MySQL Part I. 2 Objectives SQL Vs. MySQL SP MySQL SP Parameters MySQL SP Control Structures.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
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.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
Pl/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-3
A Guide to SQL, Seventh Edition
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Interacting with the Oracle Server
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
Oracle9i Developer: PL/SQL Programming Chapter 3 PL/SQL Processing.
Introduction to C++ Programming
Chapter 4: Introduction to PL/SQL
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
PL/SQL week10.
Chapter 8 Advanced SQL.
Presentation transcript:

Introduction to PL/SQL Chapter 4 A Guide to Oracle9i

Lesson A Objectives Learn the fundamentals of the PL/SQL programming language Write and execute PL/SQL programs in SQL*Plus Understand PL/SQL data type conversion functions Manipulate character strings in PL/SQL programs Learn how to debug PL/SQL programs A Guide to Oracle9i

Fundamentals of PL/SQL Full-featured programming language An interpreted language Type in editor, execute in SQL*Plus A Guide to Oracle9i

Variables and Data Types Used to store numbers, character strings, dates, and other data values Avoid using keywords, table names and column names as variable names Must be declared with data type before use: variable_name data_type_declaration; A Guide to Oracle9i

Scalar Data Types Represent a single value A Guide to Oracle9i

Scalar Data Types A Guide to Oracle9i

Composite and Reference Variables Composite variables RECORD: contains multiple scalar values, similar to a table record TABLE: tabular structure with multiple columns and rows VARRAY: variable-sized array Reference variables Directly reference a specific database field or record and assume the data type of the associated field or record %TYPE: same data type as a database field %ROWTYPE: same data type as a database record A Guide to Oracle9i

PL/SQL Program Blocks Comments: Not executed by interpreter Enclosed between /* and */ On one line beginning with -- A Guide to Oracle9i

Arithmetic Operators A Guide to Oracle9i

Assignment Statements Assigns a value to a variable variable_name := value; Value can be a literal: current_s_first_name := 'John'; Value can be another variable: current_s_first_name := s_first_name; A Guide to Oracle9i

Executing a PL/SQL Program in SQL*Plus Create program in text editor Paste into SQL*Plus window Press Enter, type / then enter to execute A Guide to Oracle9i

PL/SQL Data Conversion Functions A Guide to Oracle9i

Manipulating Character Strings with PL/SQL To concatenate two strings in PL/SQL, you use the double bar (||) operator: new_string := string1 || string2; To remove blank leading spaces use the LTRIM function: string := LTRIM(string_variable_name); To remove blank trailing spaces use the RTRIM function: string := RTRIM(string_variable_name); To find the number of characters in a character string use the LENGTH function: string_length := LENGTH(string_variable_name); A Guide to Oracle9i

Manipulating Character Strings with PL/SQL To change case, use UPPER, LOWER, INITCAP INSTR function searches a string for a specific substring: start_position := INSTR(original_string, substring); SUBSTR function extracts a specific number of characters from a character string, starting at a given point: extracted_string := SUBSTR(string_variable, starting_point, number_of_characters); A Guide to Oracle9i

Debugging PL/SQL Programs Syntax error: Command does not follow the guidelines of the programming language Generates compiler or interpreter error messages Logic error: Program runs but results in an incorrect result Caused by mistake in program A Guide to Oracle9i

Finding and Fixing Syntax Errors Interpreter flags the line number and character location of syntax errors If error message appears and the flagged line appears correct, the error usually occurs on program lines preceding the flagged line Comment out program lines to look for hidden errors One error (such as missing semicolon) may cause more – fix one error at a time A Guide to Oracle9i

Finding and Fixing Logic Errors Locate logic errors by viewing variable values during program execution There is no SQL*Plus debugger Use DBMS_OUTPUT statements to print variable values A Guide to Oracle9i

Lesson B Objectives Create PL/SQL decision control structures Use SQL queries in PL/SQL programs Create loops in PL/SQL programs Create PL/SQL tables and tables of records Use cursors to retrieve database data into PL/SQL programs Use the exception section to handle errors in PL/SQL programs A Guide to Oracle9i

PL/SQL Decision Control Structures Use IF/THEN structure to execute code if condition is true IF condition THEN commands that execute if condition is TRUE; END IF; If condition evaluates to NULL it is considered false Use IF/THEN/ELSE to execute code if condition is true or false ELSE commands that execute if condition is FALSE; Can be nested – be sure to end nested statements A Guide to Oracle9i

PL/SQL Decision Control Structures Use IF/ELSIF to evaluate many conditions: IF condition1 THEN commands that execute if condition1 is TRUE; ELSIF condition2 THEN commands that execute if condition2 is TRUE; ELSIF condition3 THEN commands that execute if condition3 is TRUE; ... ELSE commands that execute if none of the conditions are TRUE; END IF; A Guide to Oracle9i

IF/ELSIF Example A Guide to Oracle9i

Complex Conditions Created with logical operators AND, OR and NOT AND is evaluated before OR Use () to set precedence A Guide to Oracle9i

Using SQL Queries in PL/SQL Programs Action queries can be used as in SQL*Plus May use variables in action queries DDL commands may not be used in PL/SQL A Guide to Oracle9i

Loops Program structure that executes a series of program statements, and periodically evaluates an exit condition to determine if the loop should repeat or exit Pretest loop: evaluates the exit condition before any program commands execute Posttest loop: executes one or more program commands before the loop evaluates the exit condition for the first time PL/SQL has 5 loop structures A Guide to Oracle9i

The LOOP...EXIT Loop LOOP [program statements] IF condition THEN EXIT; END IF; [additional program statements] END LOOP A Guide to Oracle9i

The LOOP...EXIT WHEN Loop LOOP program statements EXIT WHEN condition; END LOOP; A Guide to Oracle9i

The WHILE...LOOP WHILE condition LOOP program statements END LOOP; A Guide to Oracle9i

The Numeric FOR Loop FOR counter_variable IN start_value .. end_value program statements END LOOP; A Guide to Oracle9i

Cursors Pointer to a memory location that the DBMS uses to process a SQL query Use to retrieve and manipulate database data A Guide to Oracle9i

Implicit Cursor A Guide to Oracle9i

Using an Implicit Cursor Executing a SELECT query creates an implicit cursor To retrieve it into a variable use INTO: SELECT field1, field2, ... INTO variable1, variable2, ... FROM table1, table2, ... WHERE join_ conditions AND search_condition_to_retrieve_1_record; Can only be used with queries that return exactly one record A Guide to Oracle9i

Explicit Cursor Use for queries that return multiple records or no records Must be explicitly declared and used A Guide to Oracle9i

Using an Explicit Cursor Declare the cursor CURSOR cursor_name IS select_query; Open the cursor OPEN cursor_name; Fetch the data rows LOOP FETCH cursor_name INTO variable_name(s); EXIT WHEN cursor_name%NOTFOUND; Close the cursor CLOSE cursor_name; A Guide to Oracle9i

Explicit Cursor with %ROWTYPE A Guide to Oracle9i

Cursor FOR Loop Automatically opens the cursor, fetches the records, then closes the cursor FOR variable_name(s) IN cursor_name LOOP processing commands END LOOP; Cursor variables cannot be used outside loop A Guide to Oracle9i

Using Cursor FOR Loop A Guide to Oracle9i

Handling Runtime Errors in PL/SQL Programs Runtime errors cause exceptions Exception handlers exist to deal with different error situations Exceptions cause program control to fall to exception section where exception is handled A Guide to Oracle9i

Predefined Exceptions A Guide to Oracle9i

Undefined Exceptions Less common errors Do not have predefined names Must declare your own name for the exception code in the declaration section DECLARE e_exception_name EXCEPTION; PRAGMA EXCEPTION_INIT(e_exception_name, -Oracle_error_code); A Guide to Oracle9i

User-Defined Exceptions Not a real Oracle error Use to enforce business rules A Guide to Oracle9i

Summary PL/SQL is a programming language for working with an Oracle database Scalar, composite and reference variables can be used The IF/THEN/ELSE decision control structure allows branching logic Five loop constructs allow repeating code Cursors are returned from queries and can be explicitly iterated over Exception handling is performed in the exception section. User defined exceptions help to enforce business logic A Guide to Oracle9i