Introduction to PL/SQL Francis Thottungal. The outline The basic PL/SQL code structure is : DECLARE -- optional, which declares and define variables,

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

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.
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.
PL/SQL (Embedded SQL) Introduction Benefits Basic Constructs
Oracle PL/SQL Eyad Husni Elshami. Why PL/SQL Block Structures: – PL/SQL consists of blocks of code, which can be nested within each other. Each block.
Programming in Oracle with PL/SQL
Introduction to PL/SQL
Cursors in Pl/SQL Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
PL/SQL Bulk Collections in Oracle 9i and 10g Kent Crotty Burleson Consulting October 13, 2006.
Dr. James Dullea, CSC8490 Introduction to PL/SQLSlide 1 of 36 7From Prof. Dullea CSC8490 Introduction to PL/SQL Module 01-9 Revised: June 12, 2005 Dr.
PL/SQL and the Table API. Benefits of Server-Side Code Speedy Pizza MENU NAPOLITAINE PIZZA Reduced network traffic Maintainability Data integrity Triggers.
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:
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.
L/O/G/O Introduction to PL/SQL Oracle Database 10g Develop PLSQL Program Units.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Program with PL/SQL. Interacting with the Oracle Server.
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.
PL SQL Block Structures. What is PL SQL A good way to get acquainted with PL/SQL is to look at a sample program. PL/SQL combines the data manipulating.
LECTURE 1 INTRODUCTION TO PL/SQL Tasneem Ghnaimat.
PL / SQL By Mohammed Baihan. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural.
1 CursorsCursors. 2 SQL Cursor A cursor is a private SQL work area. A cursor is a private SQL work area. There are two types of cursors: There are two.
PL/SQL Procedural Language / Structured Query Language.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to 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.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
PL/SQL Declaring Variables PL/SQL Block Structure DECLARE (Optional) Variables, cursors, user-defined exceptions BEGIN (Mandatory) - SQL statements -
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Database Application Development using PL/SQL Programming.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
1 PL/SQLPL/SQL Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements.
Copyright  Oracle Corporation, All rights reserved. 18 Interacting with the Oracle Server.
Copyright  Oracle Corporation, All rights reserved. 16 Declaring Variables.
Using SQL in PL/SQL ITEC 224 Database Programming.
ITEC 224 Database Programming PL/SQL Lab Cursors.
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 
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
1 PL/SQL Part C Scope and Interacting with the Oracle Server.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
PL/SQL Writing Executable Statements. PL/SQL Block Syntax and Guidelines Statement can be split across lines, but keywords must not be split Lexical units.
3 Copyright © 2004, Oracle. All rights reserved. Writing Executable Statements.
ITEC 224 Database Programming
CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.
CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.
Interacting with the Oracle8 Server
Interacting with the Oracle Server
Interacting with the Oracle Server
Creating Stored Procedures and Functions
Difference between Oracle PL/SQL and MySQL
Interacting with the Oracle Server
PL/SQL.
Database Management Systems 2
Interacting with the Oracle Server
Chapter 4: Introduction to PL/SQL
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Cursors.
PL/SQL Declaring Variables.
Presentation transcript:

Introduction to PL/SQL Francis Thottungal

The outline The basic PL/SQL code structure is : DECLARE -- optional, which declares and define variables, cursors and user-defined exceptions. BEGIN -- mandatory - SQL statements - PL/SQL statements EXCEPTION -- optional, which specifies what actions to take when error occurs. END; -- mandatory

Hello World DECLARE v1 Varchar2(50); BEGIN v1 := ‘Hello World’; DBMS_OUTPUT.PUT_LINE('v1=' || v1); END;

SQL Plus Usage At command prompt: C:>SQL SET SERVEROUTPUT ON DECLARE v1 Varchar2(50); BEGIN v1 := ‘Hello World’; DBMS_OUTPUT.PUT_LINE('v1=' || v1); END; / Note that a PL/SQL block is terminated by a slash / or a line byitself.

EXAMPLE-2 DECLARE v1 NUMBER(3); BEGIN v1 := 3; DBMS_OUTPUT.PUT_LINE('v1=' || v1); END;

Handling Variables Variables must be declared first before the usage. The PL/SQL variables can be a scalar type such as DATE, NUMBER, VARCHAR(2), DATE, BOOLEAN, LONG and CHAR, or a composite type, such array type VARRAY. Only TRUE and FALSE can be assigned to BOOLEAN type of variables. AND, OR, NOT operators can be used to connect BOOLEAN values. % TYPE attribute can be used to define a variable which is of type the same as a database column's type definition. Users can customize the variable types by using TYPE... IS... statement.

The following code block illustrates the use of TYPE..IS... and VARRAY. In this sample, a type v_arr is defined as an variable array of maximum 25 elements which are of type NUMBER(3). Then a variable v1 is defined as type v_arr. This sample code also demonstrates the use of %TYPE attribute. DECLARE TYPE v_arr IS VARRAY(25) of NUMBER(3); v1 v_arr; v_empno employee.empno%TYPE; BEGIN v1(2) := 3; DBMS_OUTPUT.PUT_LINE('The Value of v1(2) = ' || v1(2)); v_empno := 4; END;

Programming Guidelines Single-line comments are prefixed with two dashes --. Multiple-line comments can be enclosed with the symbols /* and */. Variables and function identifiers can contain up to 30 characters, and should not have the same name as a database column name. Identifiers must begin with an alphanumerical character. SQL functions can be used in PL/SQL. Code blocks can be nested and unqualified variables can locally scoped. It is recommended that variable names are prefixed by v_, and parameter names in procedures/functions are prefixed by _p.

DECLARE v_sal employee.sal%TYPE; BEGIN INSERT INTO employee VALUES (6, 'TOM LEE', 10000); UPDATE employee SET sal = sal WHERE empno = 6; SELECT sal INTO v_sal FROM employee WHERE empno = 6; DBMS_OUTPUT.PUT_LINE('Salary increased to ' || v_sal); DELETE FROM employee WHERE empno = 6; COMMIT; END; /

A SQL cursor is a private Oracle SQL working area. There are two types of SQL cursor: implicit or explicit cursor. The implicit cursor is used by Oracle server to test and parse the SQL statements and the explicit cursors are declared by the programmers. Using the implicit cursor, we can test the outcome of SQL statements in PL/SQL. For example, SQL%ROWCOUNT, return the number of rows affected; SQL%FOUND, a BOOLEAN attribute indicating whether the recent SQL statement matches to any row; SQL%NOTFOUND, a BOOLEAN attribute indicating whether the recent SQL statement does not match to any row; SQL%ISOPEN, a BOOLEAN attribute and always evaluated as FALSE immediately after the SQL statement is executed.

DECLARE CURSOR csr_ac (p_name VARCHAR2) IS SELECT empno, name, sal FROM employee WHERE name LIKE '%p_name%'; BEGIN FOR rec_ac IN csr_ac ('LE') LOOP DBMS_OUTPUT.PUT_LINE(rec_ac.empno || ' ' ||rec_ac.name || ' '||v_sal); END LOOP ; CLOSE csr_ac; END; /