PLSQL plsql Complete

download PLSQL plsql Complete

of 343

Transcript of PLSQL plsql Complete

  • 7/21/2019 PLSQL plsql Complete

    1/342

    SQL Star International Ltd.

    Oracle10g PLSQL

    This book belongs to

    Name : ______________________________________

    Batch : ______________________________________

    SQL STAR INTERNATIONAL LTD.

    SQL Star House,No. 8-2-293/174/A 25,

    Road No. 14, Banjara Hills,

    Hyderabad - 500 034,

    Andhra Pradesh, INDIA

  • 7/21/2019 PLSQL plsql Complete

    2/342

    Copyright 2008

    Second Edition

    SQL STAR INTERNATIONAL LIMITED

    SQL Star House,8-2-293/174/A25, Road No.14,

    Banjara Hills, Hyderabad - 500 034.Tel. No. 91- 40-23101600(30 lines)

    Fax No. 23101663Toll Free No: 1800 425 2944

    Email: [email protected]

    No part of this publication may be reproduced (incl. photocopying) in any way, without prioragreement and written permission of SQL Star International Ltd., Hyderabad. SQL StarInternational Ltd., Hyderabad assumes no responsibility for its use, nor for any infringementsof patents or other rights of third parties which could result.

  • 7/21/2019 PLSQL plsql Complete

    3/342

    - 3 -

    SQL Star International Ltd.

    Table of Contents

    10g PL /SQL

    CHAPTER

    No.

    CHAPTER TITLE PAGE

    NO

    1. Introduction to PL/SQL 1-25

    2. DMLs in PL/SQL 26-38

    3. Control Structure 39-59

    4. Composite DataTypes 60-104

    5. Cursors and advanced cursors 105-128

    6. Handling exception in PL/SQL 129-146

    7. Creating subprograms 147-170

    8. Managing subPrograms 171-184

    9. Creating Packages 185-204

    10. Using More Package Concepts 205-223

    11. Oracle Supplied Packages 224-250

    12. PL/SQL Performance Enhancements 251-277

    13. Large Objects 278-295

    14. Database Triggers 296-320

    15. Creating advanced Database Triggers 321-339

  • 7/21/2019 PLSQL plsql Complete

    4/342

    SQL Star International Ltd.1

    Chapter 1

    Introduction to PL/SQL

    Need for PL/SQLBenefits of Using PL/SQL

    PL/SQL Block Types and ConstructsPL/SQL Block Structure

    Operators and SQL Functions in PL/SQLVariables

    Nested Blocks

  • 7/21/2019 PLSQL plsql Complete

    5/342

    SQL Star International Ltd.2

    Objectives

    At the end of this chapter, you will be able to:

    State the need for a procedural language in Oracle

    Create PL/SQL blocks

    Write nested blocks

  • 7/21/2019 PLSQL plsql Complete

    6/342

    SQL Star International Ltd.3

    Introducing PL/SQLIn the journey so far, you have learnt about the Structured Query Language (SQL).The Oracle software provides a language, Procedural Language/SQL (PL/SQL), whichis an extension of SQL.

    PL/SQL implements modularity in the codes you write. It allows you to performiterations and handle errors. Features like data hiding and error handling makePL/SQL a state-of-the-art language for databases.

    PL/SQL also allows data manipulation and SQL query statements to be includedwithin procedural units of code. This makes PL/SQL a powerful transaction processingmechanism.

    Need for PL/SQL

    The requirement for PL/SQL was mainly due to the need to centralize automatedbusiness tasks. For example, if each employee in an organization maintains separateprograms to manage their tasks and updates them at their discretion, it could create

    confusions, such as:

    New employees or existing employees who are handed over someone elses

    work, would have a problem understanding the process followed by

    the employee vis--vis the organization

    Any change in business policy or functionality would have to be updated by all

    the employees individually in all relevant programs.

    In a centralized functionality this is avoided because all changes need to be made atone place. Changes will get reflected in the relevant codes and all users can access

    updated information.

    Benefits of Using PL/SQL

    The reasons for using PL/SQL are as follows:

    Integrating with the Oracle server and Oracle development tools

    Implementing performance enhancements in an application

    Modularizing the development of programs

    Implementing Portability

    Declaration of variables Programming with procedural language control structures

    Handling errors

  • 7/21/2019 PLSQL plsql Complete

    7/342

    SQL Star International Ltd.4

    Integration

    PL/SQL plays a key role in:

    Oracle server, through stored procedures and functions, database triggers andpackages

    Oracle development tools, through Oracle Developer component triggers

    PL/SQL supports the use of SQL datatypes. With direct access provided by SQL,these shared datatypes integrate PL/SQL with the Oracle server data dictionary.Hence, PL/SQL successfully bridges the gap between access to database technologyand the need for procedural language capabilities.

    Most applications such as Oracle Forms, Oracle Reports and Oracle Graphics, useshared libraries (that hold code), which can be accessed locally or remotely. Toexecute these stored codes, the Oracle tools have their own PL/SQL engine(independent of the engine present in the Oracle server). The engine first filters outthe SQL statements to send them individually to the SQL statement executor in the

    Oracle server. It then processes the remaining procedural statements in theprocedural statement executor in the PL/SQL engine. The procedural statementexecutor processes data, which is already inside the client environment and not inthe database. This reduces the workload on the Oracle server and also the amount ofmemory required.

    Performance Enhancements of Applications

    When SQL statements are sent to the Oracle server one at a time, each statementresults in a call to the Oracle server, leading to performance overhead and networktraffic. If your application is SQL intensive, then instead of sending SQL statementsindividually to the server, you can put them into one block using PL/SQL and send

    the entire block to the server at one time. This reduces network traffic.

    PL/SQL also operates with Oracle development tools, thereby adding proceduralprocessing power to these tools and enhancing performance.

  • 7/21/2019 PLSQL plsql Complete

    8/342

    SQL Star International Ltd.5

    Modularized Program Development

    PL/SQL programs are made up of one or more blocks. These blocks may beindividual ones or may be nested within another. That is, a block may represent a

    small part of another block, which may in turn be part of a whole unit of code. Theunits (procedures, functions or anonymous blocks) making up a PL/SQL program arecalled logical blocks.

    A diagrammatic representation of modularization is:

    The advantages associated with modularized development of programs are:

    Logical groupings of related statements within blocks

    Nesting blocks within larger blocks help build powerful programs

    Breaking down complex problems into manageable sets of logical, well-definedmodules, which can be implemented within blocks

    Portability

    PL/SQL is portable. That means, PL/SQL programs can be run wherever the Oracleserver exists. There is no need to tailor them to suit each new environment. This isachieved because PL/SQL is native to the Oracle server, and therefore can be movedto any environment (operating system or platform) that supports the Oracle server.

    PL/SQL code can also be moved between the Oracle server and Oracle Developerapplications by writing programs and creating libraries in different environments.

  • 7/21/2019 PLSQL plsql Complete

    9/342

    SQL Star International Ltd.6

    Variable Declaration

    In PL/SQL, you can declare variables:

    To use them in SQL and procedural statements

    Belonging to different data types

    Dynamically based on the structure of tables and columns in the database

    Programming with Procedural Language Control Structures

    PL/SQL allows the usage of control structures, which enable you to execute:

    Sequence of statements conditionally Sequence of statements iteratively in a loop Individually the rows returned by multiple-row query

    Handling Errors

    PL/SQL implements error handling functionality by:

    Processing Oracle server errors with error handling routines Declaring your own error conditions and process them with error handlers

    PL/SQL Block Types and Constructs

    There are two block types and different types of constructs in PL/SQL. A block is aPL/SQL code. A construct is the way in which a block is written to implementdifferent functionality.

    Block Types

    Logical blocks are the basic units of code that make up a PL/SQL program. The twokinds of PL/SQL blocks are:

    Anonymousblocks Named blocks or Subprograms

    Anonymous Blocks

    Anonymous blocks are declared at that point in an application from where they areto be executed and are passed to the PL/SQL engine for execution at runtime. These

    blocks are unnamed blocks. They can be embedded in the iSQL*Plus environment.An application trigger consists of these blocks.

    Subprograms

    Unlike anonymous blocks, named blocks or subprograms are given a name. Theseblocks can be invoked for execution and they can also accept parameters.

  • 7/21/2019 PLSQL plsql Complete

    10/342

    SQL Star International Ltd.7

    Subprograms can be declared as Procedures or Functions. You declare a subprogramas a Procedure to perform an action, and you declare a subprogram as a Function tocompute a value. Subprograms can be written and stored either at the server side orat the client side.

    Constructs

    There are various PL/SQL program constructs available that use the basic PL/SQLblock. The availability of program constructs depends on the environment in whichthey are executed.

    The different program constructs are given below.

  • 7/21/2019 PLSQL plsql Complete

    11/342

    SQL Star International Ltd.8

    PL/SQL Block Structure

    To write a PL/SQL block, you need to know the different parts of a PL/SQL block andwhat each part should hold. The structure of all PL/SQL blocks is the same. The only

    difference is that, if it is an anonymous block it is not given a name.

    A PL/SQL block has three sections . They are :

    Declarative section Executable section Exception handling section

    The declarativesection is where the variables and constants used in the body of theblock are declared. Any cursors or user-defined error handlers that are used in thebody are declared here. This section is optional.

    The executablesection holds the set of statements or the logic of the tasks that areto be performed. You can include SQL statements to make changes to the databaseand PL/SQL statements to manipulate data in the block. The statements in this blockare enclosed within the keywords BEGIN and END. This section is mandatory.

    The last section of a block is the exception section. Here a set of statements iswritten to handle any errors that might occur when the statements in the executablesection are being executed. This section is optional .

  • 7/21/2019 PLSQL plsql Complete

    12/342

    SQL Star International Ltd.9

    The diagramatic expression of PL/SQL block structure is given below.

    The syntax for writing a PL/SQL block is:

    DECLARE

    datatype(size);

    BEGIN

    SQL statements;

    PL/SQL statements;

    EXCEPTION

    WHEN THEN

    END;

    /

    Some points that will help you write a block:

    A line of PL/SQL text which contains group of characters known as lexicalunits. These units are classified as follows:

    Delimitersare simple or compound symbols that have a special meaning toPL/SQL. The following table presents both the simple as well as the compoundsymbols:

  • 7/21/2019 PLSQL plsql Complete

    13/342

    SQL Star International Ltd.10

    Identifiersare used to name PL/SQL program constructs such as Constants,Variables and Exceptions. The following points need to be kept in mind whileusing identifiers:

    Identifiers can be up to 30 characters in length, but ensure they start

    with an alphabet.

    Do not give the same name for the identifiers as the name of columns

    in a table used in the block. If so, then the Oracle server assumes that

    the table column is being referred.

    An identifier consists of a letter, followed (optional) by other letters,

    numerals, underscores, dollar signs and number signs. The following

    characters are however illegal:Abc&efg illegal ampersandAbc-efg illegal hyphenAbc/efg illegal slashAbc efg illegal space

    Identifiers should not be reserved words except when they are used

    within double quotes, for instance INSERT.

    Literals are made up of definite values such as character, numeric, string orBoolean values, not represented by identifiers. These are case sensitive.

  • 7/21/2019 PLSQL plsql Complete

    14/342

    SQL Star International Ltd.11

    Literals are of two types:

    Character literals: are all the printable characters such as letters,

    numerals, spaces, and special symbols. Specify character literals in

    single quotes.

    Numeric literals: are represented either by a simple value such as 12

    or by a scientific notation such as 3E6.

    Comments are extra information given by the programmer to improvereadability in a code, to enabledocumenting in each phase and debugging.

    PL/SQL code can be commented in two ways:

    Single line comment represented by --

    Multi line comment represented by /* */

    Use a semicolon (;) at the end of all SQL and PL/SQL control statements andthe

    END keyword.

    Do not use semicolons after the keywords DECLARE, BEGIN and EXCEPTION.

    Increase the readability of the block by writing the keywords in uppercase.

    Use a slash (/) to terminate a PL/SQL block on a line by itself.

    Using Quotes in LiteralsOracle 10g allows you to define your own string delimiters to remove the need todouble up any single quotes.

    SET SERVEROUTPUT ON

    BEGIN

    --original code

    DBMS_OUTPUT.PUT_LINE(He is New Jersey Library

    s Member!);

    -- using quote symbol

    DBMS_OUTPUT.PUT_LINE(q# He is New Jersey Librarys

    Member!#);

    DBMS_OUTPUT.PUT_LINE(q[ He is New Jersey

    Librarys

    Member !]);

  • 7/21/2019 PLSQL plsql Complete

    15/342

    SQL Star International Ltd.12

    END;

    /

    He is New Jersey Librarys Member!

    He is New Jersey Librarys Member!

    He is New Jersey Librarys Member !

    PL/SQL procedure successfully completed

    Operators in PL/SQL

    The following operators are supported in PL/SQL: Arithmetic Logical Relational or Comparison Concatenation

    Exponentiation [Represented by (**)]

    SQL Functions in PL/SQL Statements

    All the SQL functions can also be used in procedural statements in a block.

    These include:

    Single-row number and character functions

    Datatype conversion functions

    Date functions

    Timestamp functions

    GREATEST and LEAST functions

    The functions that cannot be used in procedural statements are:

    DECODE Group functions like AVG, MIN, MAX, COUNT, SUM, STDDEV and

    VARIANCE. These functions work only on a group of rows in a table andhence, they can be used only with the SQL statements in a PL/SQL block.

    Variables

    A variable are named memory locations used to store data temporarily. The datastored in variables is used in the blocks and then processed. When the processing iscompleted, the data held in the variables may be written to the database or simplyerased in case of session wide variables.

    Why is a Variable used?

    A Variable stores data temporarily. It manipulates the stored data and performscalculations with the data without accessing the database. Once declared, variablescan be used repeatedly in an application by referencing them in other statements inthe block.

  • 7/21/2019 PLSQL plsql Complete

    16/342

    SQL Star International Ltd.13

    When variables are declared using %TYPE and %ROWTYPE (more information isprovided later in the chapter), you are infact basing the variable declaration on thecolumn definition of a table. In this case, if the column definition changes, then thevariable declarations also changes accordingly. This helps in data independence,reduces maintenance cost and allows the program to adjust to the new business

    logic.

    How to handle variables in PL/SQL?

    In a PL/SQL block, variables are:

    Declared and initialized in the declarative section of the block.

    Assigned new values in the executable section. On doing so, the existingvalue is replaced with the newly assigned value. Care must be taken to seethat a variable being referred to is already declared in the declarative section.Forward references cannot be made.

    Types of VariablesVariables are of two types. They are:

    PL/SQL variables

    Non-PL/SQL variables

    PL/SQL Variables

    PL/SQL variables have a data type that specifies a storage format, constraints andalso valid range of values.The data types used to declare PL/SQL variables are:

    Scalar data typesare those that correspond to database column types. Thesedata types hold only a single value. The base scalar data types include:

    CHARVARCHAR2LONGLONG RAWNUMBERBINARY_INTEGERPLS_INTEGER

    BINARY_FLOATBINARY_DOUBLEBOOLEANDATETIMESTAMPTIMESTAMP WITH TIMEZONETIMESTAMP WITH LOCAL TIMEZONEINTERVAL YEAR TO MONTHINTERVAL DAY TO SECOND

    Binary_Float and Binary_Double are the two new Datatypes introduced in

    Oracle10g.They represent floating point numbers in IEEE 754 format (Institute of

  • 7/21/2019 PLSQL plsql Complete

    17/342

    SQL Star International Ltd.14

    Electrical and Electronic Engineers) and require 5 byte and 9 bytes to store the

    values respectively. IEEE format s supported by most of the computer system

    operating through native processor instructions thereby helping us to carry out

    complex computations using floating point data.

    Composite data typesare those that are defined by users. They enable you tomanipulate groups of data in PL/SQL blocks

    Reference data typesare those that hold values pointing to other objects.These are also known aspointers.

    LOB (large object) data types: are those that hold values, which specify thelocation of large objects such as graphic images. These values are known as locators.Large objects are stored in the same database as the table but not within the table.LOB data type allows you to store unstructured data of a maximum of 8-128terabytes. This data could be a movie clip or a graphic image or a sound wave form.LOBs are further classified into:

    CLOB (Character Large Objects) is used to store large blocks of characterdata of a single byte.

    BLOB (Binary Large Object) is used to store large binary objects within thedatabase.

    BFILE (Binary File) is used to store large binary objects that are in theoperating system files outside the database.

    NCLOB (National Language Character Large Objects), are used to store large

    blocks of NCHAR data that may be single-byte or fixed-width multiple bytes

    The syntax for declaring PL/SQL variables is:

    identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr];

    Where,

    identifieris the name assigned to the variable declared.

    CONSTANT specifies a constraint that the value of the variable cannot change.Constant variables must be initialized. While declaring a variable as a constant, theCONSTANT keyword must precede the datatype specification.

    DATATYPE specifies the type of data the variable can hold. It could be scalar,composite, reference or LOB datatype.

    NOT NULLspecifies a constraint that the variable must contain a value. Therefore,NOT NULL variables must be initialized.

    :=is the assignment operator used to assign an expression to a variable. Instead ofthe assignment operator, the DEFAULT expr (expression) can be used to assignvalues to the variables.

    By default, all variables are initialized to NULL. To prevent null values, variables areinitialized using the DEFAULT keyword.

  • 7/21/2019 PLSQL plsql Complete

    18/342

    SQL Star International Ltd.15

    The following code snippet shows how PL/SQL variables are declared in thedeclarative section of a block:

    DECLARE

    FirstName CHAR(20);

    BranchID CHAR(7) NOT NULL: = 09RANNJ;

    FeeAmt CONSTANT NUMBER(2): = 15;

    CatgName CHAR(15) DEFAULT Fiction;

    % TYPE Attribute

    If you need to store a database column value in a variable or write a value from avariable to a database column, then the data type of the variable needs to be thesame as that of the database column. You can use the %TYPE attribute to declare avariable to be of the same data type as that of a previously declared variable ordatabase column.

    Incorrect variable data types generate PL/SQL errors during execution. When youwant to declare a variable using a table attribute instead of the datatype the syntaxis

    table.columnname%TYPE

    For example, in case you want to declare a variable that will store the address of alibrary member, you will write in the following syntax:

    DECLARE

    Address VARCHAR2(45);

    But, the above declaration will result in an error when an attempt is made topopulate the variable with address details of members from the database table. Thisis because there is a mismatch in type specification of the variable. The datatypewidth of the vAddresscolumn in table Memberis 50, but you have declared it as45. To overcome this, declare the variable using %TYPE attribute as follows:

    DECLARE

    Address Member.vAddress%TYPE;

    This statement declares a variable whose datatype and width is based on thevAddresscolumn.

    If you want to declare a variable of the same type as a previously declared variablethen the syntax is

    variable_name%TYPE

    Using %TYPE attribute to declare a variable based on a previously declared variable,is illustrated in the section dealing with the iSQL*Plus variables within PL/SQL blocks.

  • 7/21/2019 PLSQL plsql Complete

    19/342

    SQL Star International Ltd.16

    Datatype and Variable size is determined when the block is compiled. So even ifthere is a change in the database column datatype the code manages the changeddatatype information.

    Data Conversion Functions

    In any programming language generally, we have to deal with different datatypes

    simultaneously or receive data which is not in the default format. In such cases,Oracle server implicitly converts data into valid datatypes wherever feasible. Explicit

    conversions come into the scenario where automatic conversions are not possible.

    Oracle Server takes care of implicit conversion between

    1. Character and Number

    2. Character and Date

    But how is it done? Let us take an example.

    DECLARE

    cons_nfine NUMBER(3):=50;

    cons_extra_fine VARCHAR2(20):=5';tot_fine Transaction.nfine%TYPE;

    BEGIN

    tot_fine:= cons_nfine+cons_extra_fine;

    DBMS_OUTPUT.PUT_LINE(The total fine payable is

    Rs.||tot_fine);

    END;

    /

    So, did you notice something?

    Variable cons_nfine is of number datatype and cons_extra_fine is of VARCHAR2

    datatype. While assigning the result to tot_fine variable, cons_extra_fine is

    converted to number by PL/SQL executer and then the operation is performed.

  • 7/21/2019 PLSQL plsql Complete

    20/342

    SQL Star International Ltd.17

    To perform explicit conversion, following built in functions can be used.

    to_char()

    to_number()

    to_date()

    to_Binary_float()

    to_Binary_double()

    DECLARE

    v_Date date:= to_Date( April 04 2007,Month dd YYYY);

    BEGIN

    DBMS_OUTPUT.PUT_LINE(You have entered ||v_Date || as

    input);

    END;

    /

    Example to show the usage of new datatypes.

    DECLARE

    l_binary_float BINARY_FLOAT;

    l_binary_double BINARY_DOUBLE;

    BEGIN

    l_binary_float := 2.1f;

    l_binary_double := 2.00001d;

    DBMS_OUTPUT.PUT_LINE(l_binary_double);

    DBMS_OUTPUT.PUT_LINE(l_binary_float);

    l_binary_float := TO_BINARY_FLOAT(2.1);

    l_binary_double := TO_BINARY_DOUBLE(2.00001);

    DBMS_OUTPUT.PUT_LINE(l_binary_double);

    DBMS_OUTPUT.PUT_LINE(l_binary_float);

    END;

    /

  • 7/21/2019 PLSQL plsql Complete

    21/342

    SQL Star International Ltd.18

    Non-PL/SQL Variables

    Since PL/SQL has neither input nor output capabilities, it relies on the environment inwhich it is executed in order to pass values into and out of a PL/SQL block.

    The following non-PL/SQL variables can be used within PL/SQL blocks:

    Substitution variables Host variables

    Substitution variables are those that you can use to pass values to a PL/SQL block atruntime. To reference a substitution variable in a block, prefix it with an ampersand(&). Before the block is executed the values for the variables are substituted with thevalues passed.

    Hence, you cannot input different values for the substitution variables using a loop.The substitution variable can be replaced only by one value.Here is an example showing the use of substitution variables within PL/SQL blocks.Suppose, the library wants to calculate its quarterly income based on its annual

    income. To do this, it declares a substitution variable, which would prompt the userto enter the figure of annual income. Based on the value entered, the quarterlyincome would be calculated.

    DECLARE

    AnnualIncome NUMBER (7): = &annualinc;

    QuarterlyInc AnnualIncome%TYPE;

    -declaring variable based on previously declared variable

    using --%TYPE attribute.

    BEGIN

    QuarterlyInc:= AnnualIncome/4;

    DBMS_OUTPUT.PUT_LINE (The quarterly income of the library

    is: || QuarterlyInc);

    END;

    /

    In the above block, to display the quarterly income calculated, you can specify theDBMS_OUTPUT.PUT_LINE in the PL/SQL block. DBMS_OUTPUT is an Oracle suppliedpackage and PUT_LINE is a procedure within it. To use this you need to specify theinformation you want printed, in parentheses, following theDBMS_OUTPUT.PUT_LINE command as shown in the above code:

    DBMS_OUTPUT.PUT_LINE (The quarterly income of the library

    is: || QuarterlyInc);

    For DBMS_OUTPUT.PUT_LINE to work, you need to run the iSQL*Plus commandSET SERVEROUTPUT ON.

    iSQL*Plus host variables (also known as bind variables) are used to pass runtimevalues from the PL/SQL block back to the iSQL*Plus environment. These variablescan be referenced in a PL/SQL block by placing a colon(:) before the variable.

    The keyword VARIABLE is used to declare a bind variable. The syntax is:

  • 7/21/2019 PLSQL plsql Complete

    22/342

    SQL Star International Ltd.19

    VARIABLE datatype

    The syntax to display the variable value is:

    PRINT

    Bind variables cannot be referred within the PL/SQL block of a function, procedure ora package.

    In a PL/SQL block, to differentiate between host variables and declared PL/SQLvariables, prefix the former with a colon(:).

    The code wherein you had calculated the quarterly income using substitutionvariables can be re-written using host variables as follows:

    VARIABLE QuarterlyInc NUMBER

    DECLARE

    AnnualIncome NUMBER(7):= &AnnInc;

    BEGIN

    :QuarterlyInc:= AnnualIncome/4;

    END;

    /

    old 2: AnnualIncome NUMBER(7):= &AnnInc;

    new 2: AnnualIncome NUMBER(7):= 80000;

    PL/SQL procedure successfully completed.

    PRINT QuarterlyInc

    QUARTERLYINC

    ------------

    20000

    The above example shows the Host variable being assigned a value inside a PL/SQLblock. To assign a value to a host variable outside the Pl/SQL block following can be

    done:-

    SQL > VARIABLE QuarterlyInc NUMBER

    SQL > Exec :QuarterlyInc := 20000SQL> PRINT QuarterlyInc

    QUARTERLYINC

    ------------

    20000

    Where,

  • 7/21/2019 PLSQL plsql Complete

    23/342

    SQL Star International Ltd.20

    Exec stand for Execute privilege.

    Nested Blocks

    PL/SQL allows blocks to be nested wherever you can have an executable statement.[This makes the nested block a statement.] Hence, the executable part of a blockcan be broken down into smaller blocks. Even the exception section can containnested blocks.

    Variable Scope

    Issues that concern references to identifiers can be resolved taking into account theirscopeand visibility.

    By scope, we mean that region of a program unit from which an identifier can bereferenced. For instance, a variable in the declarative section can be referenced fromthe exception section of that block.

    The diagram shown below illustrates the concept of nested blocks and variablescope.

  • 7/21/2019 PLSQL plsql Complete

    24/342

    SQL Star International Ltd.21

    By visibility, we mean the regions from which an identifier can be referenced withoutusing a qualified name.

    The following code snippet shows how to qualify identifiers:

    --Block label

    DECLARE --This is the parent block

    JoiningDt DATE;

    BEGIN

    DECLARE --his is the child block

    JoiningDt DATE;

    BEGIN

    outer_blk.JoiningDt :=TO_DATE (20- JAN-1998, DD-MON-

    YY);

    END;

    END;

    /

    In the code snippet, a variable with the same name as the variable declared in theouter block is declared in the inner block. To reference the outer block variable in theinner block, the variable is qualified by prefixing it with the block name.Identifiers are considered local to the PL/SQL block in which they are declared, andare considered global to all its sub-blocks. Within the sub-block, only local identifiersare visible because to reference the global identifiers you must use a qualified name.If a block cannot find the identifier declared locally, it will look up to declarativesection of the enclosing block (parent block). However, the block will never lookdownto the enclosed blocks (child blocks).

    Look at the following code and determine the variable scope:

    DECLARE

    vSal NUMBER(8,2) := 50000;

    vComm NUMBER(8,2) := vSal * 0.10;

    vNote VARCHAR2(200) := Eligible for commission;

    BEGIN

    DECLARE

    vSal NUMBER(8,2) := 90000;

    vComm NUMBER (4) := 0;

    vAnnualComp NUMBER(8,2) := vSal + vComm;

    BEGIN

    vNote := Manager not||vNote;

    OUTER_BLK.vComm := vSal * 0.20;

    END;

    vNote := Salesman||vNote;

    END;

  • 7/21/2019 PLSQL plsql Complete

    25/342

    SQL Star International Ltd.22

    /

    Based on the rules of scoping, determine values of:

    vNoteat position 1vAnnualCompat position 2vCommat position 1OUTER_BLK.vCommat position 1vCommat position 2vNoteat position 2

    Guidelines for Writing PL/SQL Code

    Programs can be indented using carriage return,tabs and aligning keywords in thesame line at different levels.Writing so, the task of debugging is made simpler.While writing programs we can follow a case convention, though PL/SQL is not casesensitive. For instance:-

    All keywords should be written in uppercase. They must be aligned in the same line. Table names and column names are to be in Initcap and lower case

    respectively.

    Indented programs improve the performance in those cases where similarstatements are to be issued repeatedly because there is no need to parse thestatements again.

    Let us write a program following all the rules stated above.

    DECLARE

    AnnualIncome NUMBER(7):= 60000;QuarterlyInc NUMBER(8,2);

    BEGIN

    QuarterlyInc:= AnnualIncome/4;

    DBMS_OUTPUT.PUT_LINE(The Quarterly income is

    ||QuarterlyInc);

    END;

    /

    Here, the keywords are aligned in the same line but at different levels. Thisimproves readability of the code.

  • 7/21/2019 PLSQL plsql Complete

    26/342

    SQL Star International Ltd.23

    Summary

    In this chapter, you have learnt that:

    PL/SQL bridges gap between SQL and a procedural language.

    Two types of PL/SQL blocks are:

    1. Anonymous or unnamed blocks which are not stored in database andcompiled each time they are executed.

    2. Named Blocks which are compiled only once and stored in the database.

    The three sections of a PL/SQL Block are:

    1. Declarative Section, where all the variables used in the program aredeclared. This is optional. Keyword: DECLARE

    2. Executable Section where actual logic of the program lies. Keyword: BEGIN

    3. Exception Section where all the errors are handled. This section is optional.Keyword: EXCEPTION

    4. END to end the PL/SQL program.

    PL/SQL variables are declared and are accessible only within that PL/SQL block.Non- PL/SQL variables are declared outside the PL/SQL block and are availablethroughout the session.

    Two new datatypes FLOAT and DOUBLE introduced in this release help users incomputing complex calculations.

    Nesting of blocks is allowed to have good control on the scope of the variables.Nested Blocks can also have exception sections.

  • 7/21/2019 PLSQL plsql Complete

    27/342

    SQL Star International Ltd.24

    Lab Exercises

    1. Identify whether the following declarations are correct or incorrect:

    a) DECLAREempID NUMBER(5);

    b) DECLAREstr1, str2, str3 VARCHAR2(20);

    c) DECLAREhiredate DATE NOT NULL;

    d) DECLAREon BOOLEAN := 5;

    2. Create an anonymous block to display the phrase Welcome to the world ofPL/SQL.

    3. Create a block that declares two variables, one of VARCHAR2 type and the

    other of NUMBER type. Assign the following values to the variables and display theoutput on the screen.

    4. Write a PL/SQL code to display the ID, Last Name, job ID and salary of anemployee?

  • 7/21/2019 PLSQL plsql Complete

    28/342

    SQL Star International Ltd.25

    5. Among the following which datatypes are enhancements of Oracle10g?

    a) BINARY_INTEGER

    b) BINARY_FLOAT

    c) BINARY_DOUBLE

    6. Display the following text using quote operator q:

    Im Oracle certified,youre not.

  • 7/21/2019 PLSQL plsql Complete

    29/342

    SQL Star International Ltd.26

    Chapter 2

    DMLs in PL/SQLSELECT Statements in PL/SQL

    DML Statements in PL/SQL

  • 7/21/2019 PLSQL plsql Complete

    30/342

    SQL Star International Ltd.27

    Objectives

    At the end of this chapter, you will be able to:

    Use SELECT statements within PL/SQL blocks Perform Data Manipulations within PL/SQL blocks

  • 7/21/2019 PLSQL plsql Complete

    31/342

    SQL Star International Ltd.28

    SELECT Statements in PL/SQL Blocks

    The use of a PL/SQL block would be incomplete without its ability to interact with thedatabase. To interact with the database you must use SQL. PL/SQL therefore,supports the use of Data Manipulation Language, Transaction Control commands,

    SQL functions for extracting data and also applying changes to the database.

    Embedding SELECT Statements

    SELECT statements within PL/SQL blocks help retrieve data from the database. The

    syntax is the same as when issued from iSQL*Plus, with a slight difference.

    SELECT

    INTO (variable_name[, variable_name] | record_name)

    FROM

    WHERE condition;

    In the syntax,

    select_listspecifies a list of columns whose values are to be retrieved. They could

    also include SQL expressions, row functions or group functions.

    variable_nameis the variable which has been declared to hold the values that are

    retrieved. They are also known output variables.

    record_nameis the PL/SQL record which has been declared to hold the retrieved

    values. [PL/SQL records will be dealt with in a later session]

    table_nameis the database table from which values are retrieved.

    conditioncomprises of various operators used to compare column names, constants,

    expressions and PL/SQL input variables.

    The main factor that differentiates this syntax from the usual SELECT syntax written

    in iSQL*Plus, is the INTO clause. This clause is mandatory and is placed between the

    SELECT clause and the FROM clause. It specifies the variables into which the values

    returned by the SELECT clause are to be stored. For each item selected in the

    SELECT clause there must be corresponding output variables in the INTO clause. The

    order of the output variables too must correspond to the columns selected.

    Embedded SELECT statements are within the scope of ANSI (American National

    Standard Institute) Classification of Embedded SQL, which states that Queries must

    return one and only one row. Therefore, if queries return more than one row the

    Oracle server generates an error.

  • 7/21/2019 PLSQL plsql Complete

    32/342

    SQL Star International Ltd.29

    The following example will illustrate the use of SELECT statements within PL/SQL

    blocks:

    SET SERVEROUT ON

    DECLARE

    vBkname Book.cBookName%TYPE;

    vAuthor Book.cAuthorName%TYPE;

    BEGIN

    SELECT cBookName, cAuthorName

    INTO vBkname, vAuthor

    FROM Book

    WHERE cBookID = HUM020000323;

    DBMS_OUTPUT.PUT_LINE(vBkname|| written by: ||vAuthor);

    END;

    /

    On executing the code, the following result is generated:

    Rapunzel meets Santa Claus in Hell

    written by: Tom Boyle

    PL/SQL procedure successfully completed.

    In the above program, the block retrieves the book name and author name for the

    specified book.

    In the following code SQL functions are used in the SELECT statement within the

    block:

    DECLARE

    vTotcopies NUMBER(4); Output variable

    vBrID Book.cBranchID%TYPE:=02CHSNJ;

    BEGIN

    SELECT SUM(nNoOfCopies)

    INTO vTotcopies

    FROM Book

    WHERE cBranchID = vBrID;

    Output

  • 7/21/2019 PLSQL plsql Complete

    33/342

    SQL Star International Ltd.30

    DBMS_OUTPUT.PUT_LINE(The total stock of books stored at

    the

    Chester NJ Library is: ||vTotcopies);

    END;

    /

    On executing the code, the following result is generated:

    The total stock of books stored at the Chester NJ Library is: 176

    PL/SQL procedure successfully completed.

    The block retrieves the total number of copies of books stored in the specified

    branch.

    There is a possibility of ambiguity in the WHERE clause if the variable name and thecolumn name are identical. This is because the Oracle server on encountering a

    variable on the right side of the comparison operator in the WHERE clause, checks

    whether it is a column in the database. If it is not, then it assumes it to be a PL/SQL

    variable. However, if the variable name is identical to the column name, the server

    considers it to be a column and therefore generates an erroneous result.

    Why dont you try this out to check what erroneous result the server gives?

    There is another interesting aspect of the WHERE clause. As you know, it is used in

    the SELECT statement to restrict the rows that are to be selected. However, if no

    rows are selected, the server generates an error. For example, if the branch ID

    specified does not exist in the Branch table and you use it in the WHERE clause, the

    query would be unable to retrieve any details pertaining to the branch ID specified.The following code would clarify this:

    DECLARE

    vBrName Branch.cBranchName%TYPE;

    BEGIN

    SELECT cBranchName

    INTO vBrName

    FROM Branch

    WHERE cBranchID = 00SCHNJ;

    END;

    /

  • 7/21/2019 PLSQL plsql Complete

    34/342

    SQL Star International Ltd.31

    On executing the code, the following error is generated:

    DECLARE

    *

    ERROR at line 1:

    ORA-01403: no data found

    While embedding SELECT statements within PL/SQL blocks you must remember the

    following points:

    Each SQL statement must be terminated with a semicolon (;).

    Do not forget the INTO clause in the SELECT statement.

    The WHERE clause specifies a condition using constants, literals or PL/SQL

    input variables (for example, vBrID). This clause is optional.

    The number of output variables in the INTO clause must be the same as the

    number of columns selected in the SELECT clause.

    Make sure that the output variables in the INTO clause are mapped correctly

    to the selected columns, and that their datatypes are also compatible.

    To ensure that the datatypes of variables match that of the columns selected

    in the SELECT clause, use the %TYPE attribute.

    You can use group functions in the SELECT statement but not in the PL/SQL

    statements.

    Do not give names to variables that are identical to column names. This could

    create ambiguity in the WHERE clause.

  • 7/21/2019 PLSQL plsql Complete

    35/342

    SQL Star International Ltd.32

    DML Statements in PL/SQL

    You can manipulate data stored in the database by embedding DML statementswithinPL/SQL blocks. This has its benefits. When you issue DML statements in iSQL*Plus,they are sent to the Oracle server for execution one at a time. This results in high

    performance overhead especially in a networked environment. Therefore, to improveperformance, club all the DML statements into one PL/SQL block, because the entireblock gets sent to the server for execution in one go.

    The transaction control options COMMIT, SAVEPOINT and ROLLBACK, that define the

    beginning, breakpoint and end of a logical unit of activity in SQL statement

    processing, are also available in PL/SQL. The option that provides locking

    mechanism, which enables only one user at a time to change a record in the

    database, is also supported in PL/SQL.

    However, an important difference between issuing SQL statements within PL/SQL

    blocks and in iSQL*Plus is that the beginning and end of a PL/SQL block does not

    necessarily imply the beginning or end of a transaction. The execution of the first

    DML statement in thePL/SQL block begins a transaction. To save or discard the changes made, the PL/SQL

    code should explicitly contain COMMIT or ROLLBACK statements.

    Embedding INSERT, UPDATE, DELETE and MERGE Statements

    The INSERT statement within a PL/SQL block is embedded just as you would enter

    them in iSQL*Plus.

    For example, insert details of a new member Ann Judd into the Membertable.

    BEGIN

    INSERT INTO Member

    VALUES(CAJ040501,ANN,JUDD,26A,Due Drops Apts,Blue

    Mountain Villa,Elizabeth,78099',

    NULL,22,SYSDATE,C,N,04RANNJ);

    END;

    /

    You can confirm the insertion of a new row by issuing a SELECT statement:

    SELECT *

    FROM Member

    WHERE cMemberID=CAJ040501';

  • 7/21/2019 PLSQL plsql Complete

    36/342

    SQL Star International Ltd.33

    This block does not have a declarative section, as you have not declared any

    variables that are to be used in the INSERT statement. The VALUES clause requires:

    The actual values Ann and Judd to be entered into the cFirstName and

    cLastNamecolumns respectively

    The SYSDATE function to enter the current date into the dMembershipDt

    column.

    You can also insert the above details in the following manner:

    DECLARE

    cFirstName Member.cFirstName%TYPE DEFAULT Ann;

    BEGIN

    INSERT INTO

    Member(cMemberID,cFirstName,cLastName,vAddress,

    cArea,cZipcode,cPhone,nAge,dMembershipDt,

    cGrade,cMaritalStatus,cBranchID)

    VALUES (CAJ040501,cFirstName,Judd,

    26A,Due Drops Apts,Blue Mountain Villa,

    Elizabeth,78099',NULL,22,SYSDATE,C,N,04RANNJ);

    END;

    /

    The block declares an input variable cFirstName of Member.cFirstName%TYPEand

    initializes it with a value Ann using the DEFAULT keyword instead of the assignment

    operator. This variable is then used in the VALUES clause to populate cFirstName

    column with the value Ann.

    Therefore, in the INSERT statement you can:

    Use functions such as SYSDATE

    Use database sequences to generate sequential numbers

    Add default column values

  • 7/21/2019 PLSQL plsql Complete

    37/342

    SQL Star International Ltd.34

    You can make changes to data or remove data stored in the database by embedding

    UPDATE and DELETE statements within PL/SQL blocks.

    For example, the librarian has to:

    Update the number of copies of the book On The Street Where You Live inthe Booktable

    Remove details pertaining to one Mr. Derian Bates from the Member table

    DECLARE

    vCopiesIncrease Book.nNoOfCopies%TYPE:=3;

    vMemID Member.cMemberID%TYPE: = CDB028504;

    BEGIN

    UPDATE Book

    SET nNoOfCopies = nNoOfCopies + vCopiesIncrease

    WHERE cBookName = On The Street Where You Live;

    DELETE FROM Member

    WHERE cMemberID = vMemID;

    COMMIT;

    END;

    /

    It is important to remember that there could be ambiguity in the SET clause if the

    declared variable has the same name as that of the column whose value is to be

    updated. This is because the name on the left of the assignment operator in the SET

    clause always corresponds to a database column, but on the right side, it could

    correspond either to a database column or a PL/SQL variable.

  • 7/21/2019 PLSQL plsql Complete

    38/342

    SQL Star International Ltd.35

    For instance, in the above code,

    SET nNoOfCopies = nNoOfCopies + vCopiesIncrease;

    nNoOfCopies to the left of the operator corresponds to the nNoOfCopies column of

    the Book table, where as nNoOfCopies to the right also corresponds to the same

    column. But vCopiesIncreaseto the right of the operator corresponds to the inputvariable. Therefore, there is no ambiguity in this SET clause.

    Why dont you see the outcome of naming a variable as that of a column name?

    Use the MERGE statement to update or insert rows in a table using data from

    another table. Rows are inserted or updated based on a specified equijoin condition.

    For example,

    DECLARE

    vMemID Member.cMemberID%TYPE:= CDB028504;

    BEGIN

    MERGE INTO MemberTabCopy mc

    USING Member m

    ON (m.cMemberID=vMemID)

    WHEN MATCHED THEN

    UPDATE SET

    mc.cFirstName = m.cFirstName,

    mc.cLastName = m.cLastName,

    mc.vAddress = m.vAddress,

    mc.cArea = m.cArea,

    mc.cZipcode = m.cZipcode,

    mc.cPhone = m.cPhone,

  • 7/21/2019 PLSQL plsql Complete

    39/342

    SQL Star International Ltd.36

    mc.nAge = m.nAge,

    mc.dMembershipDt = m.dMembershipDt,

    mc.cGrade = m.cGrade

    WHEN NOT MATCHED THEN

    INSERT VALUES (m.cMemberID, m.cFirstName, m.cLastName,

    m.vAddress, m.cArea, m.cZipcode, m.cPhone,

    m.nAge, m.dMembershipDt, m.cGrade);

    END;

    /[For the purpose of this code, we have created a table MemberTabCopy, which is

    similar in structure to the Membertable]

    The code matches the member ID in the MemberTabCopytable to the member ID

    in the Membertable. If they match, the row is updated to match the row in the

    Membertable or else the row is inserted into the MemberTabCopytable.

    But, where do the SQL statements issued within the blocks get parsed (that is,

    checked whether the tables the statements are accessing are the ones on which

    accessing privileges have been granted) and executed? This brings in the concept of

    cursors which will be discussed in a later chapter.

  • 7/21/2019 PLSQL plsql Complete

    40/342

    SQL Star International Ltd.37

    Summary

    In this chapter, you have learnt that:

    Embedded SELECT statement has a INTO clause in it. One or more variable

    follows after the INTO clause.

    Variable names should not collide with the names of the database columns.Data fetched from the database are stored in these variables.

    WHERE clause restricts the number of rows to be fetched into these scalar

    variables.

    DML Statements INSERT, UPDATE, DELETE and MERGE can be used tomanipulate the data in the database by embedding these statements inside thePL/SQL block.

    By doing so, one can reduce the network traffic, resulting in the performanceimprovement.

  • 7/21/2019 PLSQL plsql Complete

    41/342

    SQL Star International Ltd.38

    Lab Exercises

    1. Create a PL/SQL block that retrieves the sum of salary of department 80 and

    stores it in an iSQL*Plus variable. Print the value to the screen.

    2. Create a PL/SQL block that will:

    Retrieve the maximum employee ID in the Employeestable.

    Insert data regarding a new employee into the Employeestable. For the

    employee ID of the new employee, add 1 to the maximum employee ID

    retrieved. Leave the phone number, commission and manager ID as null.

    Display the new row created.

  • 7/21/2019 PLSQL plsql Complete

    42/342

    SQL Star International Ltd.39

    Chapter 3

    Control Structures

    Conditional ConstructsCase ConstructsLoop Constructs

    GOTO Statement

  • 7/21/2019 PLSQL plsql Complete

    43/342

    SQL Star International Ltd.40

    Objectives

    At the end of this chapter, you will be able to:

    Use Branching structures

    Use Case Control structures

    Use Loop Control Structures

  • 7/21/2019 PLSQL plsql Complete

    44/342

    SQL Star International Ltd.41

    Control Structures

    Oracle PL/SQL provides a range of constructs that allow you to control the flow ofprocess and there by produce well-structured programs. The selection structure testsa condition and then executes one sequence of statements instead of another, basedon whether the condition is true or false. A condition is any variable or expressionthat returns a Boolean value (TRUE or FALSE). The iteration structure executes asequence of statements repeatedly as long as a condition holds true. The sequencestructure simply executes a sequence of statements in the order in which they occur.Hence, Control structures are the backbones of a program, where logic of thetransactions are specified.

    Control Structures are broadly divided into

    Conditional constructs

    IF Statements CASE Statement and CASE Expression

    Loop constructs Basic Loop WHILE Loop FOR Loop

    CONDITIONAL CONSTRUCTS USING IF Statements

    When you need to perform actions based on conditions, you use the IF statements.

    The structure of the IF statement is the same as those used in other procedural

    programming languages. It has three variations. They are:

    IF - THEN - END IF IF - THEN - ELSE - END IF IF - THEN - ELSIF - END IF

    Their syntaxes are shown below.

    IF condition THEN IF condition THEN IF condition THEN

    statements; statements; statements;

    END IF; [ELSE [ELSIF condition THENstatements;] statements;]

    END IF; [ELSE

    statements;]

    END IF;

  • 7/21/2019 PLSQL plsql Complete

    45/342

    SQL Star International Ltd.42

    Where,

    condition is an expression, which when executed will return a value of TRUE or

    FALSE or NULL. If the result of the expression is TRUE then the set of statements,following the THEN clause is performed else the statements following the ELSE orELSIF clauses are performed.

    THEN is the clause that connects the value returned by the condition to thestatements that are to be executed

    statements include the SQL or PL/SQL statements that would perform the requiredactions. They may include further IF statements.

    The ELSIFkeyword is associated with another set of SQL or PL/SQL statements thatshould be executed in case the condition does not return the desired value.

    The ELSEkeyword is also associated with a set of SQL or PL/SQL statements that willbe executed, if none of the statements higher up in the construct has been executed,as the condition associated with each was not satisfied.

    Let us now look into a few examples of the various IF statements, to have a betterunderstanding of its functioning.

    Example of a simple IF statement:

    The library does not permit a person to be its member if his/her age happens to bebelow five. This could be checked using the following IF-THEN-END IF statement:

    DECLAREAge Member.nAge%TYPE:= &age;

    BEGIN

    IF Age

  • 7/21/2019 PLSQL plsql Complete

    46/342

    SQL Star International Ltd.43

    Example of an IF THEN ELSE END IF Statement

    The library charges a fine amount of Rs.1.5 per day after the elapse of the due date.To perform this calculation, the following IF-THEN-ELSE statement can be used:

    IF ActualRetDt > ReturnDt THEN

    Fine := (ActualRetDt ReturnDt)*1.5;

    ELSE

    Fine: = 0;

    END IF;

    In the statement, if the actual return date is later than the due date, fine amount iscalculated. If not, the fine amount is set to 0.

    Example of an IF-THEN-ELSE-ENDIF Statement using Logical Operators

    IF LibraryName=Jersey AND ActualRetDt>ReturnDt

    THEN

    Fine:=(ActualRetDt-ReturnDt)*1.5;

    ELSE

    Fine:=0;

    END IF;

    In the statement, the fine is calculated only if the Library Name is Jersey and actualreturn date is later than the due date. If not, the fine amount is set to 0.

    Nested IF Statements

    Either set of actions of the first IF statement can include further IF statements. Sucha condition is known as a nested IF statements. Its syntax is as follows:

    IF condition1 THEN

    Statement1;

    ELSE

    IF condition2 THEN

    Statement2;

    END IF;

    END IF;

    The nested IF statement is terminated with its corresponding END IF.

    Example of nested IF statements

    The library charges fee from members based on their ages. To illustrate this execute

    the following code:

    DECLARE

    Age NUMBER :=&age ;

  • 7/21/2019 PLSQL plsql Complete

    47/342

    SQL Star International Ltd.44

    Fee NUMBER;

    BEGIN

    IF Age < 5 THEN

    DBMS_OUTPUT.PUT_LINE (Membership

    denied: Age below permissiblemembership age);

    ELSE

    IF Age BETWEEN 5 AND 13 THEN

    Fee := 10;

    ELSE

    IF Age BETWEEN 14 AND 20 THEN

    Fee := 15;

    ELSE

    IF Age BETWEEN 21 AND 50 THEN

    Fee := 20;

    ELSE

    IF Age > 50 THEN

    Fee := 5;END IF;

    END IF;

    END IF;

    END IF;

    END IF;

    DBMS_OUTPUT.PUT_LINE (Fee for Member is:|| Fee);

    END;

    /

    If you execute the above code, you can see the following output:

    Enter value for age: 44

    old 2: Age NUMBER : = &age ;

    new 2: Age NUMBER : = 44 ;

    Fee for Member is:20

    PL/SQL procedure successfully completed.

    The above code does not permit a person to be the librarys member if his age is

    below five. However, if his age is above five, then membership fee is charged based

    on a set of nested IF conditions.

    If the age is between 5 and 13, then the fee amount is 10. If the age is between 14and 20, then the fee amount is 15. If the age is between 21 and 50, then the fee

    amount is 20. If the age is above 50, then the fee amount is 5.

    Example of IF THEN ELSIF END IF Statements

    The above code was quite cumbersome due to large number of nested IF

    statements. Instead you can use the ELSIF clause to make the code easier to read.

  • 7/21/2019 PLSQL plsql Complete

    48/342

    SQL Star International Ltd.45

    DECLARE

    Age NUMBER := &age ;

    Fee Number;

    BEGIN

    IF Age < 5 THEN

    DBMS_OUTPUT.PUT_LINE (Membership denied: Age below

    permissible membership age);

    ELSIF Age BETWEEN 5 AND 13 THEN

    Fee := 10;

    ELSIF Age BETWEEN 14 AND 20 THEN

    Fee := 15;

    ELSIF Age BETWEEN 21 AND 50 THEN

    Fee := 20;

    ELSIF Age > 50 THEN

    Fee := 5;

    END IF;

    DBMS_OUTPUT.PUT_LINE (Fee for Member is:|| Fee);

    END;/

    On executing the above code, you get the following result:

    Enter value for age: 55

    old 2: Age NUMBER : = &age ;

    new 2: Age NUMBER : = 55 ;

    Fee for Member is:5

    PL/SQL procedure successfully completed.

    After having used all the variations of the IF constructs, there are certain guidelines

    to remember while writing them in a program. They are:

    Every IF is followed by a THEN after stating the condition.

    Every IF must have a matching END IF.

    An IF construct may have any number of ELSIF statements.

    END IF is not one word but two words and ELSIF is one word.

    Every IF construct can have only one ELSE statement.

    The ELSIF keyword does not have a matching END IF.

    Do not use a semicolon (;) on the lines with the IFTHEN, ELSIF, and ELSEkeywords. END IF must be followed by a semicolon (;).

    You could indent the SQL and PL/SQL statements to be executed to increasereadability of the code.

    CASE Expressions and CASE Statements

    In PL/SQL, two types of CASE constructs are supported:

  • 7/21/2019 PLSQL plsql Complete

    49/342

    SQL Star International Ltd.46

    CASE expressions: The expressions select a result and return it. CASEexpressions can be assigned to variables, can be part of SQL statements andcan be used in logical expressions. The syntax begins with CASE and endswith END.

    CASE statements: They are independent statements just like other PL/SQLstatements such as IFTHENELSE statements. The syntax begins with CASE

    and ends with END CASE.

    CASE Expressions

    CASE expressions select a result and return it. To understand better, look at thefollowing syntax of a CASE expression:

    CASE selector

    WHEN expr1 THEN result1

    WHEN expr2 THEN result2

    WHEN exprn THEN resultn

    [ELSE resultn+1;]

    END;

    From the syntax, it becomes clear that, to select the result, the CASE expressionuses a selector, which is an expression whose value is used to select one of theseveral alternatives provided. The selectoris followed by one or more WHEN clauses.The value of the selector determines which clause is executed. This is the syntax of asimple CASE expression.

    Execute the following code of a simple CASE expression:

    DECLARE

    vGrade CHAR(1):= &grade;

    vGradeWiseAge VARCHAR2(25);

    BEGIN

    VGradeWiseAge :=

    CASE vGrade

    WHEN A THEN Age between 5 and 13

    WHEN B THEN Age between 14 and 20

    WHEN C THEN Age between 21 and 50

    WHEN D THEN Age above 50

    END;

    DBMS_OUTPUT.PUT_LINE (Grade : ||vGrade|| Stands for:

    ||vGradeWiseAge);

    END;

    /In the code, the CASE expression is being assigned to a variable vGradeWiseAge.The CASE expression uses the value in the vGradevariable (The value is accepted

  • 7/21/2019 PLSQL plsql Complete

    50/342

    SQL Star International Ltd.47

    using a substitution variable) as the expression. Depending on the value entered bythe user, the CASE expression assigns the value to variable vGradeWiseAge.

    PL/SQL also supports a searched CASE expression. It has the following syntax:

    CASE

    WHEN searchCondition1 THEN result1

    WHEN searchCondition2 THEN result2

    WHEN searchConditionn THEN resultn

    [ELSE resultn+1]

    END;

    Searched CASE expressions have no selectors. The WHEN clauses contain searchconditions, which return a Boolean value, and not expressions that can return valuesof any type. Each WHEN clause can have different conditional expressions, which cantake the form of searchCondition = operator . Theoperator can be any comparison operator.

    Execute following code of a searched CASE expression:

    DECLARE

    vAge NUMBER(3):= &age;

    vAgeWiseGrade CHAR(25);

    BEGIN

    vAgeWiseGrade :=

    CASE

    WHEN vAge

  • 7/21/2019 PLSQL plsql Complete

    51/342

    SQL Star International Ltd.48

    Evaluate multiple variables or expressions

    Each WHEN clause can:

    Evaluate different expressions

    Have comparison operators

    The following examples will help us in understanding the different ways in which thetwo different CASE statements function.

    A simple CASE statement example:

    DECLARE

    vGrade CHAR(1):= &grade;

    vGradeWiseAge VARCHAR2(25);

    BEGIN

    CASE vGradeWHEN A THEN vGradeWiseAge:=Age between 5 and 13;

    WHEN B THEN vGradeWiseAge:=Age between 14 and 20;

    WHEN C THEN vGradeWiseAge:=Age between 21 and 50;

    WHEN D THEN vGradeWiseAge:=Age above 50;

    END CASE;

    DBMS_OUTPUT.PUT_LINE (Grade : ||vGrade|| Stands for:

    ||vGradeWiseAge);

    END;

    /

    In the code, you will notice that the CASE statement ends with an END CASE

    statement, and that every executable statement following the WHEN clause isterminated with asemicolon. The above code accepts the value of vGrade and based on it assignsdifferent values to vGradeWiseAgevariable.

    A searched CASE statement example:

    DECLARE

    vAge NUMBER(3):= &age;

    vAgeWiseGrade CHAR(2);

    BEGIN

    CASE

    WHEN vAge

  • 7/21/2019 PLSQL plsql Complete

    52/342

    SQL Star International Ltd.49

    END;

    /

    In the code, you will observe that the searched CASE statement is similar to thesearched CASE expression except that the former cannot be assigned to variables.

    NULLIF and COALESCE Expressions

    NULLIF and COALESCE expressions are a form of shorthand for PL/SQL CASEexpressions.

    The NULLIF expression behaves like an inverse of the NVL function. The semanticscan be written as follows:

    CASE

    WHEN expr1=expr2 THEN NULL;

    ELSE expr1;

    END;

    Execute the following PL/SQL code:

    DECLARE

    vDate DATE:= &Date;

    vReturnDt DATE;

    BEGIN

    IF vDate >= SYSDATE THEN

    vReturnDt:= NULLIF (vDate, SYSDATE);

    DBMS_OUTPUT.PUT_LINE (The member has to pay a fine

    amount of: || (vReturnDt SYSDATE) * 1.5);

    ELSE

    DBMS_OUTPUT.PUT_LINE (No fines due);

    END IF;

    END;

    /

    In the code, if the date entered by the user is before the SYSDATE, the ELSEcondition is executed. However, in case the date entered is after the SYSDATE orequal to the SYSDATE, the NULLIF expression is evaluated. If the vDate variable

  • 7/21/2019 PLSQL plsql Complete

    53/342

    SQL Star International Ltd.50

    value is after SYSDATE, then the vDatevalue is returned to vReturnDtvariable. IfvDatevariable value is same as SYSDATE, then a NULL value is returned.

    The COALESCE expression behaves like the NVL function, but it can take a list ofvalues. Its semantics can be written as follows:

    Expression with two arguments:

    CASE

    WHEN expr1 IS NOT NULL THEN expr1

    ELSE expr2

    END;

    Expression with three or more arguments:

    CASE

    WHEN expr1 IS NOT NULL THEN expr1;

    ELSE COALESCE (expr2, expr3,,exprn);

    END CASE;

    LOOP Constructs

    You might require performing a set of steps continuously till the transaction or taskto be performed is complete.

    Using loops will enable you to perform such kind of execution. The execution of thestatements in a loop also depends on the result of a condition being true or false. Aslong as the condition is true, the statements in the loop will be executed. Thecondition to be satisfied is the starting point of the loop. Only when the condition issatisfied, the control of the block will enter the loop and will stay in the loop till thecondition is reversed. An important point to remember is that the loop shouldprovide for a condition to be checked for exiting from the loop.

    The different looping constructs include: BASIC loop FOR loop

    WHILE loop

    BASIC LOOP

    This is the simplest form of a loop. The statements are enclosed between thekeywords LOOP and END LOOP.

  • 7/21/2019 PLSQL plsql Complete

    54/342

    SQL Star International Ltd.51

    When the flow of execution encounters the END LOOP keyword, the control goesback to the corresponding LOOP statement above it. Hence, the statements in abasic loop will be executed at least once.

    The loop should contain an EXIT statement. This statement helps to end the loop. Ifit is not specified, then the loop will be endless. An EXIT statement can also be

    specified as an action within an IF statement. The EXIT statement must be placedwithin the loop.

    The syntax for a basic loop is:

    LOOP

    statements;

    . . .

    EXIT [WHEN condition];END LOOP;

    Where,

    LOOPis the beginning of the loop statements

    statementsare the SQL and/or PL/SQL statements to be executed.

    EXITis the keyword to end the loop.

    WHENcondition is the condition to be true to exit the loop.

    END LOOPmarks the end of the loop and takes the loop back to the beginning.

    An example of a basic loop to display the first ten even numbers is as follows:

    DECLARE

    Counter NUMBER(2):= 0;

    BEGIN

    LOOP

    DBMS_OUTPUT.PUT_LINE(Counter);

    Counter:= Counter+2;

    EXIT WHEN Counter= 20;

    END LOOP;

    END;

    /

    The output generated by the above code is:

    Result:

  • 7/21/2019 PLSQL plsql Complete

    55/342

    SQL Star International Ltd.52

    0

    2

    4

    6

    8

    10

    12

    14

    16

    18

    PL/SQL procedure successfully completed.

    The EXIT-WHEN statement replaces a simple IF statement. For example, comparethe following statements.

    IF counter=20 THEN | EXIT WHEN counter=20;EXIT;

    END IF;

    These statements are logically equivalent, but the EXIT-WHEN statement is easier toread and understand.

    If you are checking the condition first and then executing the statements, EXIT

    statement should be placed immediately after LOOP statement in the above

    program

    FOR LOOP

    In a FOR LOOP a condition is placed in the beginning of the loop as a counter. Thecondition controls the number of times the statements is to be executed. The syntax

    is:

    FOR counter in [REVERSE ]

    lower_bound .. upper_bound LOOP

    statement1;

    statement2;

    . . .

    END LOOP;

    Where,counter is an integer, which is implicitly declared and whose value increases (or

    decreases when used with the REVERSE keyword) by 1 until its value reaches eitherof the limits specified.

    REVERSE enables the counter to decrement in intervals of 1.

    lower_bound is the lower limit for the range of the counter value.

    upper_bound is the upper limit for the range of the counter value.

  • 7/21/2019 PLSQL plsql Complete

    56/342

    SQL Star International Ltd.53

    The values for these limits can be strings, variables or expressions or numericvalues. When the lower value is increased to a values higher or greater than thespecified upper limit then the loop will not be executed.Certain points to remember when using a FOR loop is that:

    The counter can be referenced only inside the loop.

    When referring to the counter use an expression to refer to its existing value. Cannot assign a value to the counter.

    Use a FOR loop instead of the basic loop to display the first ten even numbers.

    DECLARE

    EvenNo NUMBER(2): = 0;

    BEGIN

    FOR I IN 1..10

    LOOP

    DBMS_OUTPUT.PUT_LINE(EvenNo);

    EvenNo: = EvenNo +2;

    END LOOP;

    END;

    /

    This code generates the following output:

    Result:

    0

    2

    4

    6

    8

    ...

    ...

    PL/SQL procedure successfully completed.

    WHILE LOOPIn this loop construct, the statements are executed based on a condition at thebeginning of the loop. The condition controls the end of the loop as well. In otherwords, the statements in the loop will be executed as long as the condition specifiedis true.

    The condition is checked at the beginning of the LOOP and if the condition is FALSE,the loop is terminated and none of the statements are executed. The syntax is:

  • 7/21/2019 PLSQL plsql Complete

    57/342

    SQL Star International Ltd.54

    WHILE condition LOOP

    statement1;

    statement2;

    . . .

    END LOOP;Where,

    condition is the expression that returns a boolean value of TRUE, FALSE or NULL

    statement is the set of SQL and PL/SQL statements to be executed.

    In case the condition returns a NULL value then the loop is skipped and none of thestatements are executed.The same code regarding the display of even numbers can be done using the WHILEloop as follows:

    DECLARE

    EvenNo NUMBER(2):= 0;

    BEGINWHILE EvenNo < 20

    LOOP

    DBMS_OUTPUT.PUT_LINE(EvenNo);

    EvenNo := EvenNo+2;

    END LOOP;

    END;

    /

    The result when you execute the code is similar to that of the previous one.

    GOTO Statement

    The GOTO statement branches to a label unconditionally. The label must be uniquewithin its scope and must precede an executable statement or a PL/SQL block. Whenexecuted, the GOTO statement transfers control to the labeled statement or block. Inthe following example, you move to an executable statement further down in asequence of statements:

    BEGIN

    ...

    GOTO insert_row;

    ...

    INSERT INTO emp VALUES ...

    END;

    /In the next example, you move to a PL/SQL block further up in a sequence ofstatements:

    DECLARE

    x NUMBER := 0;

    BEGIN

    BEGIN

    x := x + 1;

  • 7/21/2019 PLSQL plsql Complete

    58/342

    SQL Star International Ltd.55

    END;

    IF x < 10 THEN

    GOTO increment_x;END IF;

    END;

    /

    The label in the following example is not allowed because it does notprecede an executable statement:

    DECLARE

    done BOOLEAN;

    BEGIN

    FOR i IN 1..50

    LOOP

    IF done THEN

    GOTO end_loop;

    END IF;

    --not allowed

    END LOOP; --not an executable statementEND;

    /

    To correct the previous example, add the NULL statement::

    FOR i IN 1..50

    LOOP

    IF done THEN

    ...

    GOTO end_loop;

    END IF;

    ...

    NULL; -- an executable statement

    END LOOP;

    Restrictions on the GOTO Statement

    A GOTO statement cannot branch from one IF statement clause to another, or fromone CASE statement WHEN clause to another.A GOTO statement cannot branch from an outer block into a sub-block (that is, aninner BEGIN-END block).A GOTO statement cannot branch out of a subprogram. To end a subprogram early,you can use the RETURN statement or use GOTO to branch to a place right before

    the end of the subprogram.A GOTO statement cannot branch from an exception handler back into the currentBEGIN-END block. However, a GOTO statement can branch from an exceptionhandler into an enclosing block.

  • 7/21/2019 PLSQL plsql Complete

    59/342

    SQL Star International Ltd.56

    Nested Loops

    You can write a loop within another loop. The inner loop is called a nested loop as itis nested within a loop, which is called the parent loop. You can nest FOR, WHILEand BASIC loops within one another. If the nested loop is terminated, the parentloop does not get terminated unless there is an exception raised in the nested loop.

    Loops can be labeled, to identify them. These are like identifiers and are placedbefore the word LOOP within label delimiter (). You could also place thelabel after the END LOOP keyword.

    Syntax for a nested loop is:

    . . .

    BEGIN

    LOOP

    counter_variable:= counter_variable + 1;

    EXIT WHEN nCount>20;

    LOOP

    . . .

    EXIT outerloop WHEN condition;

    . . .

    EXIT WHEN condition;

    . . .

    END LOOP innerloop;

    . . .

    END LOOP outerloop;

    END;

    Where,

    BEGINis the start of the block.

    LOOPon the second line marks the beginning of the first loop, labeled outerloop.

    counter_variableis the counter that is used to check the number of times the loop isbeing executed and is defined earlier.

    EXIT WHENis the condition to exit the outer loop.

    The nextLOOP marks the beginning of the nested or inner loop labeled innerloop.

    EXITouterloop will exit both the loops based on the condition.

    EXIT WHENon the next line will exit only the nested loop if the condition is satisfied.The control goes back to the outerloop.

    END LOOPinnerloop ends the inner loop and execution of the outer loop resumes ifthere are any statements to be executed.

  • 7/21/2019 PLSQL plsql Complete

    60/342

    SQL Star International Ltd.57

    END LOOP outerloop ends and exits the outer loop.

    ENDmarks the end of the block.

    Let us look at a complete example illustrating the Nested loop.

    DECLARE

    counter number:=0;

    counter1 number:=0;

    total_done varchar2(4);

    inner_done varchar2(4);

    BEGIN

    LOOP

    counter:=counter+1;

    EXIT WHEN counter>10;

    LOOP

    IF counter = 10 THEN

    total_done:=yes;

    END IF;

    EXIT outer_loop WHEN total_done= yes;

    counter1:=counter1+1;

    IF (MOD(counter1,2)!=0) THEN

    inner_done:=yes;

    END IF;

    EXIT WHEN inner_done =yes;

    DBMS_OUTPUT.PUT_LINE(Printing Even numbers

    from inner loop-||counter1);

    END LOOP inner_loop;

    inner_done:=No;

    DBMS_OUTPUT.PUT_LINE(*****Printing Whole Number from

    outer loop***** ||counter);

    END LOOP outer_loop;

    END;

    /

  • 7/21/2019 PLSQL plsql Complete

    61/342

    SQL Star International Ltd.58

    Summary

    In this chapter, you have learnt that:

    Statements such as IF and CASE give a directive approach to a program logic.

    Using IF statement, multiple nested conditions can be solved. Where as CASEstatement improves the readability of a program.

    The Iteration of a program can be controlled using1. Basic LOOP that runs at least once2. WHILE LOOP that runs till the specified condition is true3. FOR loop that executes till the specified Iteration in the loop.

    These loops can be nested within one another.

    Jumping from one point to other in a program can be done with GOTOStatement using the LABEL Option.

  • 7/21/2019 PLSQL plsql Complete

    62/342

    SQL Star International Ltd.59

    Lab Exercises

    1. Create a PL/SQL block that rewards an employee by appending an asterisk inthe STARS column for every $1000 of the employees salary. Save yourPL/SQL block in a file called c3q1.sql by clicking on the save script button.Remember to save script with .sql extension.

    a. Use the DEFINE command to provide the employee id. Pass the value to the

    PL/SQL block through a iSQL*Plus substitution variable.

    SET VERIFY OFF;

    DEFINE p_empno=104;

    b. Initailize a v_asterisk variable that contains a NULL.

    c. Append an asterisk to the string for every $1000 of the salary amount. For

    example, if the employee has a salary amount of $8000, the string of asterisks

    should contain eight asterisks. If the employee has a salary amount of

    $12500, the string of asterisks should contain 13 asterisks.

    d. Update the STARS column for the employee with the string of asterisks.

    e. Commit.

    f. Test the block for the following values.

    i. employee_id=174ii. employee_id=176

  • 7/21/2019 PLSQL plsql Complete

    63/342

    SQL Star International Ltd.60

    Chapter 4

    Composite Data-typesTypes of Composite Data-types

    INDEX BY TablesINDEX BY Table of Records

    Varying ArraysNested Tables

    Ref Cursor

  • 7/21/2019 PLSQL plsql Complete

    64/342

    SQL Star International Ltd.61

    Objectives

    At the end of this chapter, you will be able to:

    Identify the different composite datatypes

    Process data using different composite datatypes

  • 7/21/2019 PLSQL plsql Complete

    65/342

    SQL Star International Ltd.62

    What are Composite Datatypes?

    In the previous chapters, you have used variables of scalar datatypes such as

    VARCHAR2, NUMBER or DATE. These are primitive datatypes. In addition to scalar

    variables, there are also variables of compositedatatypes.

    Composite datatypes are created by users and hence are also known as userdefineddatatypes. They are made up of a collection of different predefined datatypes.

    Therefore, composite datatypes are also referred to as collections.

    In this chapter you will learn how to create PL/SQL records, INDEX BY tables, nested

    tables and varrays.

    But, what is the need of composite variables? The answer lies in the issue of

    reducing network traffic.

    A major concern for programmers is to keep the network traffic as minimal as

    possible. Every time you embed an SQL statement within the PL/SQL block, a

    request is sent to the server for execution. This increases the network traffic and

    also takes up a lot of servers memory. To overcome these problems, variables are

    declared within PL/SQL blocks. Scalar variables can store only one column value from

    the database. Therefore, the need was felt to declare variables, which could create

    temporary memory area within the client machine for storing collections of database

    items. Once declared, these collections of data could repeatedly be used for data

    manipulations in any application, without having to access the database. In otherwords, composite datatypes are reusable. This need led to the usage of composite

    datatypes.

    Types of Composite Datatypes

    Composite datatypes store collections of data items and the result set (of data) istreated as one logical unit of data. Once you have this result set stored within theclient memory, you can use the values returned by the result set instead of

    accessing the database. This reduces network traffic. Following are the differentcomposite datatypes that a user can define:

    PL/SQL record type

    User-defined records

    %ROWTYPE records

  • 7/21/2019 PLSQL plsql Complete

    66/342

    SQL Star International Ltd.63

    Collections

    INDEX BY Table

    Nested Table

    VARRAY

    Reference (REF)

    PL/SQL Records

    A record is defined as a collection of different but related data items stored in fields.

    For example, the New Jersey Central Library has different kinds of data regarding its

    members, such as their ID, name, address, membership date, age, etc. Each piece

    of information or data item is dissimilar in type, but it is all logically related to

    members. Therefore, a member record is made up of a collection of these logically

    related data items.

    In PL/SQL these data items are stored in fieldsand a PL/SQL recordis made up of acollection of such fields.

    A PL/SQL record is similar in concept and structure to a row in a database table. Just

    as a row in the Member table contains information about members stored in

    columns, similarly a record contains information of members stored in fields.

    Therefore, a record is a composite data structure, as it comprises more than one

    element or field. The record as a whole does not have any value of its own. Instead,

    each individual field in a record has a value. The record enables you to store and

    access these values as a group rather than accessing them from the database.

    To create a record, you should first create a record type and then declare records of

    that type. There are two types of PL/SQL records: User-defined records User-defined recordsare based on

    record types where you can specify the

    fields along with datatypes.

    %ROWTYPE records %ROWTYPE recordson the other hand

    are based on record types created using

    the %ROWTYPE attribute. Here, you

    cannot specify your own fields because

    using %ROWTYPE attribute creates a

    record that represents a row in adatabase table.

    User-defined Records

    A user-defined record is a composite datatype that allows you to declare fields of

    your own choice. You can define their datatypes based on database columns using

    %TYPE attribute. The syntax is:

  • 7/21/2019 PLSQL plsql Complete

    67/342

    SQL Star International Ltd.64

    TYPE IS RECORD

    (field_name1 {field_type | variable%TYPE | table.column%TYPE }[NOT

    NULL][:=| DEFAULT value],

    (field_name2 {field_type | variable%TYPE | table.column%TYPE } [NOT

    NULL] [:= |DEFAULT value],

    ...);

    Where,

    type_name is the name you give to the record type you are creating.

    field_name1| field_name2 is the unique name given to each field contained within

    the record.

    field_type is the datatype assigned to fields. You can use %TYPE or %ROWTYPE

    attribute. You can also define the NOT NULLconstraint to prevent null values from

    being entered into the fields or assign values to fields by using the assignment

    operator.

    (: =) or the DEFAULTkeyword.

    After creating record types, you can create identifiers of that type using the following

    syntax:

    ;

    Where,

    record_name is the name given to the record you are creating.

    type_name is the record type based on which you are creating the record.

    Before you start using PL/SQL records, you need to understand how to create them.

    Creating user-defined PL/SQL Records

    As there are no predefined datatypes based on which a PL/SQL record can be

    created, you should first create a datatype and then create a record based on that

    type. The following PL/SQL block illustrates how to create a user-defined record type

    and a record based on it:

    DECLARE

    TYPE rectpBk IS RECORD

    (BookID CHAR(13),BkName Book.cBookName%TYPE,

    Author Book.cAuthorName%TYPE);

    -- Create a record

  • 7/21/2019 PLSQL plsql Complete

    68/342

    SQL Star International Ltd.65

    recBook rectpBk;

    In the above code snippet:

    A record type rectpBk has been created which is made up of three fields

    namely BookID, BkName and Author. The %TYPE attribute is used tospecify the datatypes of BkNameand Author.

    A record recBookis then created using rectpBk.

    If you have not used the NOT NULL or the DEFAULT keyword to initialize fields, you

    must reference or initialize them explicitly.

    Initializing Records

    You can reference or initialize record fields using the following syntax:

    .

    In the syntax the dotnotation is used between the record name and the field name.

    In the above code snippet, you would reference the Bkname field as follows,

    recBook.Bkname

    and initialize it with a value,

    RecBook.Bkname:= Illusions;

    However, you cannot assign a list of values to a record by using an assignment

    operator. The following syntax is illegal:

    := (value1, value2, value3, ...);

    For instance, you cannot assign the values of book id, book name and author name

    all together to the record recBookin the following manner:

    recBook:=(0109918818122,The Jewel In The Crown, Paul

    Scott);

    Also, you cannot test records for equality or inequality. For instance, the following IF

    statement is illegal:

    IF record_name1 = record_name2 THEN

    END IF;

    You now know the syntax for assigning values to fields, but there are different ways

    to do it.

    Way of assigning values to Records

    There are two ways to assign values to fields:

    Instead of assigning values to fields individually, you can assign values to allthe fields at once. This can be done using a SELECT statement in the codewhere you had declared recBook record.

  • 7/21/2019 PLSQL plsql Complete

    69/342

    SQL Star International Ltd.66

    DECLARE

    BkID Book.cBookID%TYPE:=&Bkid;

    TYPE rectpBk IS RECORD

    (Bookid CHAR(13),

    Bkname Book.cBookName%TYPE,

    Author Book.cAuthorName%TYPE);

    recBook rectpBk;

    BEGIN

    SELECT cBookID,cBookName,cAuthorName

    INTO recBook

    FROM Book

    WHERE cBookID=BkID;

    DBMS_OUTPUT.PUT_LINE(recBook.Bookid|| || recBook.Bkname||

    ||recBook.Author);

    END;

    /

    In the code, the fields of recBook have been populated with values retrieved from

    the Book table by the SELECT statement. But you must ensure that the columnnames in the SELECT statement appear in the same order as the fields in the record.

    Also, their datatypes should be compatible with one another.

    On executing this code, the values are retrieved from the record rather than from

    the database table Book. This reduces your network traffic.

    Enter value for bkid: FIC011111111

    FIC011111111 Jewel In The Crown Paul Scott

    The second way is to assign one record to another. The following code

    illustrates this:

    DECLARE

    TYPE rectpBk IS RECORD

    (Bkid CHAR (15),

  • 7/21/2019 PLSQL plsql Complete

    70/342

    SQL Star International Ltd.67

    Bkname CHAR (100));

    TYPE rectpBkCtg IS RECORD

    (Bk rectpBk,

    Ctname CHAR (5),

    Pbyr DATE); --nested record

    TYPE rectpBrn IS RECORD

    (Brid CHAR (7),

    Brname CHAR (25),

    Bk rectpBk); --nested record

    recCtg rectpBkCtg;

    recBks rectpBkCtg;

    recBrn rectpBrn;

    BEGIN

    recCtg.Ctname:=Humor;

    recCtg.Bk.Bkid:=0117955574699';

    recCtg.Bk.Bkname:=Very Good Jeeves;

    recBks.Pbyr:=12-MAY-1965';

    recBks.Bk:=recCtg.Bk;

    recBrn.Bk:=recCtg.Bk;

    END;

    /

    This code snippet illustrates the declaration of nested records, i.e. a record defined

    as an element or a field of another record. For instance, Bkis a nested record as it is

    defined as a field (which is of rectpBk type) of recCtg,recBksand recBrn records.

    You can assign the values of a nested record to another if they are of the same

    datatype. For instance, recCtg and recBks are both of the same datatype,rectpBkCtg. The nested record Bk has been assigned values:

    recCtg.Bk.Bkid:=0117955574699;

    recCtg.Bk.Bkname:=Very Good Jeeves;

    You can now assign them to recBksrecord in the following manner:

  • 7/21/2019 PLSQL plsql Complete

    71/342

    SQL Star International Ltd.68

    recBks.Bk:=recCtg.Bk;

    Such assignments are also permissible if the records that contain the nested records

    are of different datatype. For instance, recBrn record of rectpBrn record type,

    which contains the nested record Bk. Yet, the values of Bk initialized by recCtg

    record can be assigned to recBrn.

    recBrn.Bk:=recCtg.Bk;

    The problem of not knowing the column details of a database table and unexpected

    changes taking place in the number and datatype of columns can be overcome using

    the %ROWTYPE attribute. The % ROWTYPE attribute enables you to create

    %ROWTYPE records, which are similar to rows in a database table.

    %ROWTYPE Records

    Sometimes you may want to create a record whose record type is same as a row of a

    database table (or a view). Such records are known as %ROWTYPE records. Just as

    you can create a record based on a user-defined record type, similarly, you can

    create a %ROWTYPE record using the %ROWTYPE attribute. The %ROWTYPE

    attribute enables you to declare a record type that represents a row in a table or a

    view.

    Creating %ROWTYPE Records

    Prefixing % ROWTYPE with the table name creates %ROWTYPE records. The syntax

    for declaring a %ROWTYPE record is:

    reference%ROWTYPE;

    Where,

    record_name is the name you assign for a record.