+ All Categories
Home > Documents > Oracle PL Notes

Oracle PL Notes

Date post: 03-Jun-2018
Category:
Upload: sabari-nathan
View: 219 times
Download: 0 times
Share this document with a friend

of 49

Transcript
  • 8/12/2019 Oracle PL Notes

    1/49

    http://www.tutorialspoint.com/plsql/plsql_conditional_control.htm

    http://plsql-tutorial.com/plsql-constants.htm

    http://www.freejavaguide.com/plsql2.htm

    PL/SQL is a block-structured language, meaning that PL/SQL programs are divided and writtenin logical blocks of code. Each block consists of three sub-parts:

    S.N. Sections & Description

    1DeclarationsThis section starts with the keyword DECLARE. It is an optional section and defines allvariables, cursors, subprograms, and other elements to be used in the program.

    2

    Executable CommandsThis section is enclosed between the keywords BEGINand ENDand it is a mandatorysection. It consists of the executable PL/SQL statements of the program. It should have atleast one executable line of code, which may be just a NULL command to indicate thatnothing should be executed.

    3Exception HandlingThis section section starts with the keyword EXCEPTION. This section is again optionaland contains exception(s) that handle errors in the program.

    Every PL/SQL statement end with a semicolon (;). PL/SQL blocks can be nested within otherPL/SQL blocks using BEGINand END. Here is the basic structure of a PL/SQL block:

    DECLARE

    BEGIN

    EXCEPTION

    END;

    The 'Hello World' Example:

    DECLAREmessage varchar2(20):= 'Hello, World!';

    BEGINdbms_output.put_line(message);

    END;/

    http://www.tutorialspoint.com/plsql/plsql_conditional_control.htmhttp://www.tutorialspoint.com/plsql/plsql_conditional_control.htmhttp://plsql-tutorial.com/plsql-constants.htmhttp://plsql-tutorial.com/plsql-constants.htmhttp://www.freejavaguide.com/plsql2.htmhttp://www.freejavaguide.com/plsql2.htmhttp://www.freejavaguide.com/plsql2.htmhttp://plsql-tutorial.com/plsql-constants.htmhttp://www.tutorialspoint.com/plsql/plsql_conditional_control.htm
  • 8/12/2019 Oracle PL Notes

    2/49

    The end;line signals the end of the PL/SQL block. To run the code from SQL command line,you may need to type /at the beginning of the first blank line after the last line of the code. Whenthe above code is executed at SQL prompt, it produces following result:

    Hello World

    PL/SQL procedure successfully completed.

    The PL/SQL Identifiers

    PL/SQL identifiers are constants, variables, exceptions, procedures, cursors, and reserved words.The identifiers consist of a letter optionally followed by more letters, numerals, dollar signs,underscores, and number signs and should not exceed 30 characters.

    By default, identifiers are not case-sensitive. So you can use integeror INTEGERto representa numeric value. You cannot use a reserved keyword as an identifier.

    The PL/SQL Delimiters

    A delimiter is a symbol with a special meaning. Following is the list of delimiters in PL/SQL:

    Delimiter Description

    +, -, *, / Addition, subtraction/negation, multiplication, division

    % Attribute indicator

    ' Character string delimiter

    . Component selector

    (,) Expression or list delimiter

    : Host variable indicator

    , Item separator

    " Quoted identifier delimiter

    = Relational operator

    @ Remote access indicator

    ; Statement terminator

    := Assignment operator

    => Association operator

    || Concatenation operator

    ** Exponentiation operator

    Label delimiter (begin and end)

    /*, */ Multi-line comment delimiter (begin and end)

    -- Single-line comment indicator

    .. Range operator

  • 8/12/2019 Oracle PL Notes

    3/49

    , = Relational operators

    , '=, ~=, ^= Different versions of NOT EQUAL

    The PL/SQL Comments

    Program comments are explanatory statements that you can include in the PL/SQL code that youwrite and helps anyone reading it's source code. All programming languages allow for someform of comments.

    The PL/SQL supports single line and multi-line comments. All characters available inside anycomment are ignored by PL/SQL compiler. The PL/SQL single-line comments start with thedelimiter --(double hyphen) and multi-line comments are enclosed by /* and */.

    DECLARE-- variable declarationmessage varchar2(20):= 'Hello, World!';

    BEGIN/** PL/SQL executable statement(s)*/dbms_output.put_line(message);

    END;/

    When the above code is executed at SQL prompt, it produces following result:

    Hello World

    PL/SQL procedure successfully completed.

    PL/SQL Program Units

    A PL/SQL unit is any one of the following:

    PL/SQL block Function Package Package body Procedure Trigger Type Type body

    Each of these units will be discussed in the forthcoming chapters.

    PL/SQL variables, constants and parameters must have a valid data types which specifies astorage format, constraints, and valid range of values. This tutorial will take you through

  • 8/12/2019 Oracle PL Notes

    4/49

    SCALARand LOBdata types available in PL/SQL and other two data types will be covered inother chapters.

    Category Description

    Scalar

    Single values with no internal components, such as a NUMBER, DATE,

    or BOOLEAN.

    Large Object (LOB)Pointers to large objects that are stored separately from other data items,such as text, graphic images, video clips, and sound waveforms.

    CompositeData items that have internal components that can be accessedindividually. For example, collections and records.

    Reference Pointers to other data items.

    PL/SQL Scalar Data Types and Subtypes

    PL/SQL Scalar Data Types and Subtypes come under the following categories:

    Date Type Description

    Numeric Numeric values, on which arithmetic operations are performed.

    CharacterAlphanumeric values that represent single characters or strings ofcharacters.

    Boolean Logical values, on which logical operations are performed.

    Datetime Dates and times.

    PL/SQL provides subtypes of data types. For example, the data type NUMBER has a subtypecalled INTEGER. You can use subtypes in your PL/SQL program to make the data typescompatible with data types in other programs while embedding PL/SQL code in anotherprogram, such as a Java program.

    PL/SQL Numeric Data Types and Subtypes

    Following is the detail of PL/SQL pre-defined numeric data types and their sub-types:

    Data Type Description

    PLS_INTEGERSigned integer in range -2,147,483,648 through 2,147,483,647,represented in 32 bits

    BINARY_INTEGERSigned integer in range -2,147,483,648 through 2,147,483,647,represented in 32 bits

    BINARY_FLOAT Single-precision IEEE 754-format floating-point number

    BINARY_DOUBLE Double-precision IEEE 754-format floating-point number

    NUMBER(prec, scale)Fixed-point or floating-point number with absolute value in range 1E-130 to (but not including) 1.0E126. A NUMBER variable can alsorepresent 0.

  • 8/12/2019 Oracle PL Notes

    5/49

    DEC(prec, scale)ANSI specific fixed-point type with maximum precision of 38 decimaldigits.

    DECIMAL(prec, scale)IBM specific fixed-point type with maximum precision of 38 decimaldigits.

    NUMERIC(pre, secale) Floating type with maximum precision of 38 decimal digits.

    DOUBLE PRECISIONANSI specific floating-point type with maximum precision of 126binary digits (approximately 38 decimal digits)

    FLOATANSI and IBM specific floating-point type with maximum precision of126 binary digits (approximately 38 decimal digits)

    INTANSI specific integer type with maximum precision of 38 decimaldigits

    INTEGERANSI and IBM specific integer type with maximum precision of 38decimal digits

    SMALLINTANSI and IBM specific integer type with maximum precision of 38decimal digits

    REALFloating-point type with maximum precision of 63 binary digits(approximately 18 decimal digits)

    Following is a valid declaration:

    DECLAREnum1 INTEGER;num2 REAL;num3 DOUBLE PRECISION;

    BEGINnull;

    END;

    /

    When the above code is compiled and executed, it produces following result:

    PL/SQL procedure successfully completed

    PL/SQL Character Data Types and Subtypes

    Following is the detail of PL/SQL pre-defined character data types and their sub-types:

    Data Type DescriptionCHAR Fixed-length character string with maximum size of 32,767 bytes

    VARCHAR2 Variable-length character string with maximum size of 32,767 bytes

    RAWVariable-length binary or byte string with maximum size of 32,767bytes, not interpreted by PL/SQL

    NCHARFixed-length national character string with maximum size of 32,767bytes

  • 8/12/2019 Oracle PL Notes

    6/49

    NVARCHAR2Variable-length national character string with maximum size of 32,767bytes

    LONG Variable-length character string with maximum size of 32,760 bytes

    LONG RAWVariable-length binary or byte string with maximum size of 32,760bytes, not interpreted by PL/SQL

    ROWID Physical row identifier, the address of a row in an ordinary table

    UROWID Universal row identifier (physical, logical, or foreign row identifier)

    PL/SQL Boolean Data Types

    The BOOLEANdata type stores logical values that are used in logical operations. The logicalvalues are the Boolean values TRUE and FALSE and the value NULL.

    However, SQL has no data type equivalent to BOOLEAN. Therefore Boolean values cannot beused in:

    SQL statements Built-in SQL functions (such as TO_CHAR) PL/SQL functions invoked from SQL statements

    PL/SQL Datetime and Interval Types

    The DATEdatatype to store fixed-length datetimes, which include the time of day in secondssince midnight. Valid dates range from January 1, 4712 BC to December 31, 9999 AD.

    The default date format is set by the Oracle initialization parameter NLS_DATE_FORMAT. Forexample, the default might be 'DD-MON-YY', which includes a two-digit number for the day ofthe month, an abbreviation of the month name, and the last two digits of the year, for example,01-OCT-12.

    Each DATE includes the century, year, month, day, hour, minute, and second. The followingtable shows the valid values for each field:

    Field Name Valid Datetime Values Valid Interval Values

    YEAR -4712 to 9999 (excluding year 0) Any nonzero integer

    MONTH 01 to 12 0 to 11

    DAY01 to 31 (limited by the values of MONTHand YEAR, according to the rules of thecalendar for the locale)

    Any nonzero integer

    HOUR 00 to 23 0 to 23

    MINUTE 00 to 59 0 to 59

    SECOND00 to 59.9(n), where 9(n) is the precision oftime fractional seconds

    0 to 59.9(n), where 9(n) isthe precision of interval

  • 8/12/2019 Oracle PL Notes

    7/49

    fractional seconds

    TIMEZONE_HOUR-12 to 14 (range accommodates daylightsavings time changes)

    Not applicable

    TIMEZONE_MINUTE 00 to 59 Not applicable

    TIMEZONE_REGIONFound in the dynamic performance viewV$TIMEZONE_NAMES Not applicable

    TIMEZONE_ABBRFound in the dynamic performance viewV$TIMEZONE_NAMES

    Not applicable

    PL/SQL Large Object (LOB) Data Types

    Large object (LOB) data types refer large to data items such as text, graphic images, video clips,and sound waveforms. LOB data types allow efficient, random, piecewise access to this data.Following are the predefined PL/SQL LOB data types:

    Data Type Description Size

    BFILEUsed to store large binary objects in operatingsystem files outside the database.

    System-dependent. Cannotexceed 4 gigabytes (GB).

    BLOBUsed to store large binary objects in thedatabase.

    8 to 128 terabytes (TB)

    CLOBUsed to store large blocks of character data inthe database.

    8 to 128 TB

    NCLOBUsed to store large blocks of NCHAR data inthe database.

    8 to 128 TB

    PL/SQL User-Defined Subtypes

    A subtype is a subset of another data type, which is called its base type. A subtype has the samevalid operations as its base type, but only a subset of its valid values.

    PL/SQL predefines several subtypes in package STANDARD. For example, PL/SQL predefinesthe subtypes CHARACTER and INTEGER as follows:

    SUBTYPE CHARACTER IS CHAR;SUBTYPE INTEGER IS NUMBER(38,0);

    You can define and use your own subtypes. The following program illustrates defining and usinga user-defined subtype:

    DECLARESUBTYPE name IS char(20);SUBTYPE message IS varchar2(100);salutation name;greetings message;

  • 8/12/2019 Oracle PL Notes

    8/49

    BEGINsalutation := 'Reader ';greetings := 'Welcome to the World of PL/SQL';dbms_output.put_line('Hello ' || salutation || greetings);

    END;/

    When the above code is executed at SQL prompt, it produces following result:

    Hello Reader Welcome to the World of PL/SQL

    PL/SQL procedure successfully completed.

    NULLs in PL/SQL

    PL/SQL NULL values represent missing or unknown data and they are not an integer, acharacter, or any other specific data type. Note that NULL is not the same as an empty datastring or the null character value '\0'. A null can be assigned but it cannot be equated withanything, including itself.

    DECLARE-- variable declarationmessage varchar2(20):= 'Hello, World!';

    BEGIN/** PL/SQL executable statement(s)*/dbms_output.put_line(message);

    END;/

    Example program

    DECLARE

    message varchar2(20):= 'Hello, xzxczzxccxd!';

    a int :=5;

    b float default 2.2;

  • 8/12/2019 Oracle PL Notes

    9/49

    -- c constant double(5,4) 3.1415;

    c constant double precision := 3.1415;

    BEGIN

    dbms_output.put_line(b);

    b:=a*c;

    declare

    d int :=10;

    BEGIN

    b:=a+c;

    dbms_output.put_line('INSIDE THE BLOCK');

    dbms_output.put_line(b);

    dbms_output.put_line(d);

    END;

    dbms_output.put_line('welcome pl sql');

    dbms_output.put_line(a);

    dbms_output.put_line(b);

    dbms_output.put_line(c);

    END;

    Output

    2.2INSIDE THE BLOCK8.141510welcome pl sql

  • 8/12/2019 Oracle PL Notes

    10/49

    58.14153.1415

    Statement processed.

    A variable is nothing but a name given to a storage area that our programs can manipulate. Eachvariable in PL/SQL has a specific data type, which determines the size and layout of thevariable's memory; the range of values that can be stored within that memory and the set ofoperations that can be applied to the variable.

    The name of a PL/SQL variable consist of a letter optionally followed by more letters, numerals,dollar signs, underscores, and number signs and should not exceed 30 characters. By default,variable names are not case-sensitive. You cannot use a reserved PL/SQL keywords as a variable

    name.

    PL/SQL programming language allows to define various type of variables which we will coverin subsequent chapters like date time data types, records, collections etc. For this chapter, let usstudy only basic variable types.

    Variable Declaration in PL/SQL

    PL/SQL variables must be declared in the declaration section or in a package as a globalvariable. When you declare a variable, PL/SQL allocates memory for the variable's value and thestorage location is identified by the variable name.

    The syntax for declaring a variable is:

    variable_name [CONSTANT] datatype [NOT NULL] [:= | DEFAULT initial_value]

    Where, variable_nameis a valid identifier in PL/SQL, datatypemust be a valid PL/SQL datatype or any user defined data type which we already have discussed in last chapter. Some validvariable declarations along with their definition are shown below:

    sales number(10, 2);pi CONSTANT double precision := 3.1415;name varchar2(25);address varchar2(100);

    When you provide a size, scale or precision limit with the data type, it is called a constraineddeclaration. Constrained declarations require less memory than unconstrained declarations, Forexample:

    sales number(10, 2);name varchar2(25);

  • 8/12/2019 Oracle PL Notes

    11/49

    address varchar2(100);

    Initializing Variables in PL/SQL

    Whenever you declare a variable, PL/SQL assigns it a default value of NULL. If you want to

    initialize a variable with a value other than the NULL value, you can do so during thedeclaration, using either of the following:

    The DEFAULTkeyword The assignmentoperator

    For example:

    counter binary_integer := 0;greetings varchar2(20) DEFAULT 'Have a Good Day';

    You can also specify that a variable should not have a NULLvalue using the NOT NULLconstraint. If you use the NOT NULL constraint, you must explicitly assign an initial value forthat variable.

    It is a good programming practice to initialize variables properly otherwise, sometime programwould produce unexpected result. Try following example which makes use of various types ofvariables:

    DECLAREa integer := 10;b integer := 20;c integer;

    f real;BEGINc := a + b;dbms_output.put_line('Value of c: ' || c);f := 70.0/3.0;dbms_output.put_line('Value of f: ' || f);

    END;/

    When the above code is executed, it produces following result:

    Value of c: 30Value of f: 23.333333333333333333

    PL/SQL procedure successfully completed.

    Variable Scope in PL/SQL

    PL/SQL allows the nesting of Blocks i.e., each program block may contain another inner block.If a variable is declared within an inner block, it is not accessible to the outer block. However, if

  • 8/12/2019 Oracle PL Notes

    12/49

    a variable is declared and accessible to an outer Block, it is also accessible to all nested innerBlocks. There are two types of variable scope:

    Local variables- variables declared in an inner block and not accessible to outer blocks. Global variables- variables declared in the outermost block or a package.

    Following example shows the usage of Localand Globalvariables in its simple form:

    DECLARE-- Global variablesnum1 number := 95;num2 number := 85;

    BEGINdbms_output.put_line('Outer Variable num1: ' || num1);dbms_output.put_line('Outer Variable num2: ' || num2);DECLARE

    -- Local variablesnum1 number := 195;

    num2 number := 185;BEGINdbms_output.put_line('Inner Variable num1: ' || num1);dbms_output.put_line('Inner Variable num2: ' || num2);

    END;END;/

    When the above code is executed, it produces following result:

    Outer Variable num1: 95Outer Variable num2: 85Inner Variable num1: 195

    Inner Variable num2: 185

    PL/SQL procedure successfully completed.

    Assigning SQL Query Results to PL/SQL Variables

    You can use the SELECT INTO statement of SQL to assign values to PL/SQL variables. Foreach item in the SELECT list, there must be a corresponding, type-compatible variable in theINTO list. The following example illustrates the concept: Let us create a table namedCUSTOMERS:

    (For SQL statements please look at theSQL tutorial)

    CREATE TABLE CUSTOMERS(ID INT NOT NULL,NAME VARCHAR (20) NOT NULL,AGE INT NOT NULL,ADDRESS CHAR (25),SALARY DECIMAL (18, 2),PRIMARY KEY (ID)

    http://www.tutorialspoint.com/sql/index.htmhttp://www.tutorialspoint.com/sql/index.htmhttp://www.tutorialspoint.com/sql/index.htmhttp://www.tutorialspoint.com/sql/index.htm
  • 8/12/2019 Oracle PL Notes

    13/49

    );

    Table Created

    Next, let us insert some values in the table:

    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );

    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)VALUES (2, 'Khilan', 25, 'Delhi', 1500.00 );

    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)VALUES (3, 'kaushik', 23, 'Kota', 2000.00 );

    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)VALUES (4, 'Chaitali', 25, 'Mumbai', 6500.00 );

    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)

    VALUES (5, 'Hardik', 27, 'Bhopal', 8500.00 );

    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)VALUES (6, 'Komal', 22, 'MP', 4500.00 );

    The following program assigns values from the above table to PL/SQL variables using theSELECT INTO clause of SQL:

    DECLAREc_id customers.id%type := 1;c_name customers.name%type;c_addr customers.address%type;c_sal customers.salary%type;

    BEGINSELECT name, address, salary INTO c_name, c_addr, c_salFROM customersWHERE id = c_id;

    dbms_output.put_line('Customer ' ||c_name || ' from ' || c_addr || ' earns ' || c_sal);

    END;/

    When the above code is executed, it produces following result:

    Customer Ramesh from Ahmedabad earns 2000

    PL/SQL procedure completed successfully

    Decision making structures require that the programmer specify one or more conditions to beevaluated or tested by the program, along with a statement or statements to be executed if the

  • 8/12/2019 Oracle PL Notes

    14/49

    condition is determined to be true, and optionally, other statements to be executed if thecondition is determined to be false.

    Following is the general from of a typical conditional (i.e. decision making) structure found inmost of the programming languages:

    PL/SQL programming language provides following types of decision making statements. Clickthe following links to check their detail.

    Statement Description

    IF - THEN statement

    The IF statementassociates a condition with a sequence ofstatements enclosed by the keywords THENand END IF. Ifthe condition is true, the statements get executed and if thecondition is false or NULL then the IF statement does nothing.

    IF-THEN-ELSE statement

    IF statementadds the keyword ELSEfollowed by analternative sequence of statement. If the condition is false orNULL , then only the alternative sequence of statements getexecuted. It ensures that either of the sequence of statements isexecuted.

    IF-THEN-ELSIF statement It allows you to choose between several alternatives.

    Case statement

    Like the IF statement, the CASE statementselects onesequence of statements to execute. However, to select thesequence, the CASE statement uses a selector rather thanmultiple Boolean expressions. A selector is an expressionwhose value is used to select one of several alternatives.

    Searched CASE statement The searched CASE statement has no selector, and it's

    http://www.tutorialspoint.com/plsql/plsql_if_then.htmhttp://www.tutorialspoint.com/plsql/plsql_if_then.htmhttp://www.tutorialspoint.com/plsql/plsql_if_then_else.htmhttp://www.tutorialspoint.com/plsql/plsql_if_then_else.htmhttp://www.tutorialspoint.com/plsql/plsql_if_then_elsif.htmhttp://www.tutorialspoint.com/plsql/plsql_if_then_elsif.htmhttp://www.tutorialspoint.com/plsql/plsql_case_statement.htmhttp://www.tutorialspoint.com/plsql/plsql_case_statement.htmhttp://www.tutorialspoint.com/plsql/plsql_searched_case.htmhttp://www.tutorialspoint.com/plsql/plsql_searched_case.htmhttp://www.tutorialspoint.com/plsql/plsql_searched_case.htmhttp://www.tutorialspoint.com/plsql/plsql_case_statement.htmhttp://www.tutorialspoint.com/plsql/plsql_if_then_elsif.htmhttp://www.tutorialspoint.com/plsql/plsql_if_then_else.htmhttp://www.tutorialspoint.com/plsql/plsql_if_then.htm
  • 8/12/2019 Oracle PL Notes

    15/49

    WHEN clauses contain search conditions that yield Booleanvalues.

    nested IF-THEN-ELSEYou can use one IF-THEN or IF-THEN-ELSIFstatementinside another IF-THENor IF-THEN-ELSIFstatement(s).

    What is PL/SQL?

    PLSQL stands for "Procedural Language extensions to SQL", and can be used in Oracledatabases. PL SQL is closely integrated into theSQLlanguage, yet it adds programmingconstructs that are not native to SQL. PL/SQL also implements basic exception handling. Thistutorial contains an introduction to beginning pl sql. This Oracle pl/sql tutorial also provides a

    hands on experience for beginning plsql. It contains many free plsql examples which can bereused in your code.

    Pl/Sql is the procedural implementation of sql i.e. you can pass sql statements in proceduralformat using pl/sql. Normal sql does not have any procedural capabilities moreover you can onlypass one statement at a time toOracle Engine. Hence, pl/sql have come up to avoid thislimitation. Hence, pl/sql is the structured programming language for oracle. It's structure issimilar to any other procedural language such as C or C++

    The following document was written by Yu-May Chang and Jeff Ullman of Stanford University for CS145, Autumn 1997; revised by Jun Yang for Prof.Jennifer Widom's CS145 class in Spring, 1998; additional material by Jeff Ullman, Autumn 1998; further revisions by Jun Yang, Spring 1999; minorrevisions by Jennifer Widom, Spring 2000. Linked here

    PL/SQL Tutorial

    Variables and Types Simple PL/SQL Programs Control Flow in PL/SQL Cursors Procedures Discovering Errors Printing Variables

    Basic Structure of PL/SQL

    PL/SQL stands for Procedural Language/SQL. PL/SQL extends SQL by adding constructs foundin procedural languages, resulting in a structural language that is more powerful than SQL. Thebasic unit in PL/SQL is a block. All PL/SQL programs are made up of blocks, which can benested within each other. Typically, each block performs a logical action in he program. A blockhas the following structure:

    http://www.tutorialspoint.com/plsql/plsql_nested_if.htmhttp://www.freejavaguide.com/sql.htmhttp://www.freejavaguide.com/sql.htmhttp://www.freejavaguide.com/sql.htmhttp://www.freejavaguide.com/oracleint.htmhttp://infolab.stanford.edu/~ullman/fcdb/oracle/or-plsql.htmlhttp://infolab.stanford.edu/~ullman/fcdb/oracle/or-plsql.htmlhttp://www.freejavaguide.com/plsql.htm#variableshttp://www.freejavaguide.com/plsql.htm#programshttp://www.freejavaguide.com/plsql.htm#controlhttp://www.freejavaguide.com/plsql2.htm#cursorshttp://www.freejavaguide.com/plsql2.htm#procedureshttp://www.freejavaguide.com/plsql2.htm#errorshttp://www.freejavaguide.com/plsql2.htm#printinghttp://www.freejavaguide.com/plsql2.htm#printinghttp://www.freejavaguide.com/plsql2.htm#errorshttp://www.freejavaguide.com/plsql2.htm#procedureshttp://www.freejavaguide.com/plsql2.htm#cursorshttp://www.freejavaguide.com/plsql.htm#controlhttp://www.freejavaguide.com/plsql.htm#programshttp://www.freejavaguide.com/plsql.htm#variableshttp://infolab.stanford.edu/~ullman/fcdb/oracle/or-plsql.htmlhttp://www.freejavaguide.com/oracleint.htmhttp://www.freejavaguide.com/sql.htmhttp://www.tutorialspoint.com/plsql/plsql_nested_if.htm
  • 8/12/2019 Oracle PL Notes

    16/49

  • 8/12/2019 Oracle PL Notes

    17/49

    DECLARE

    price NUMBER;

    myBeer VARCHAR(20);

    Note that PL/SQL allows BOOLEAN variables, even though Oracle does not support BOOLEAN as a type

    for database columns.

    Types in PL/SQL can be tricky. In many cases, a PL/SQL variable will be used to manipulate data stored in

    a existing relation. In this case, it is essential that the variable have the same type as the relation

    column. If there is any type mismatch, variable assignments and comparisons may not work the way you

    expect. To be safe, instead of hard coding the type of a variable, you should use the %TYPE operator. For

    example:

    DECLARE

    myBeer Beers.name%TYPE;

    gives PL/SQL variable myBeer whatever type was declared for the name column in relation Beers.

    A variable may also have a type that is a record with several fields. The simplest way to declare such a

    variable is to use %ROWTYPE on a relation name. The result is a record type in which the fields have the

    same names and types as the attributes of the relation. For instance:

    DECLARE

    beerTuple Beers%ROWTYPE;

    makes variable beerTuple be a record with fields name and manufacture, assuming that the relation has

    the schema Beers(name, manufacture).

    The initial value of any variable, regardless of its type, is NULL. We can assign values to variables, using

    the ":=" operator. The assignment can occur either immediately after the type of the variable is

    declared, or anywhere in the executable portion of the program. An example:

    DECLARE

    a NUMBER := 3;

    BEGIN

  • 8/12/2019 Oracle PL Notes

    18/49

    a := a + 1;

    END;

    run;

    This program has no effect when run, because there are no changes to the database.

    Simple Programs in PL/SQL

    The simplest form of program has some declarations followed by an executable sectionconsisting of one or more of the SQL statements with which we are familiar. The major nuanceis that the form of the SELECT statement is different from its SQL form. After the SELECT

    clause, we must have an INTO clause listing variables, one for each attribute in the SELECTclause, into which the components of the retrieved tuple must be placed.

    Notice we said "tuple" rather than "tuples", since the SELECT statement in PL/SQL only worksif the result of the query contains a single tuple. The situation is essentially the same as that ofthe "single-row select" discussed in Section 7.1.5 of the text, in connection with embedded SQL.If the query returns more than one tuple, you need to use a cursor. Here is an example:

    CREATE TABLE T1(

    e INTEGER,

    f INTEGER

    );

    DELETE FROM T1;

    INSERT INTO T1 VALUES(1, 3);

    INSERT INTO T1 VALUES(2, 4);

    /* Above is plain SQL; below is the PL/SQL program. */

    DECLARE

    a NUMBER;

  • 8/12/2019 Oracle PL Notes

    19/49

    b NUMBER;

    BEGIN

    SELECT e,f INTO a,b FROM T1 WHERE e>1;

    INSERT INTO T1 VALUES(b,a);

    END;

    run;

    Fortuitously, there is only one tuple of T1 that has first component greater than 1, namely (2,4).The INSERT statement thus inserts (4,2) into T1.

    Control Flow in PL/SQL

    PL/SQL allows you to branch and create loops in a fairly familiar way.

    An IF statement looks like:

    IF THEN ELSE END IF;

    The ELSE part is optional. If you want a multiway branch, use:

    IF THEN ...

    ELSIF THEN ...

    ... ...

    ELSIF THEN ...

    ELSE ...

    END IF;

    The following is an example, slightly modified from the previous one, where now we only do theinsertion if the second component is 1. If not, we first add 10 to each component and then insert:

    DECLARE

    a NUMBER;

    b NUMBER;

  • 8/12/2019 Oracle PL Notes

    20/49

    BEGIN

    SELECT e,f INTO a,b FROM T1 WHERE e>1;

    IF b=1 THEN

    INSERT INTO T1 VALUES(b,a);

    ELSE

    INSERT INTO T1 VALUES(b+10,a+10);

    END IF;

    END;

    run;

    Loops are created with the following:

    LOOP

    /* A list of statements. */

    END LOOP;

    At least one of the statements in should be an EXIT statement of the form

    EXIT WHEN ;

    The loop breaks if is true. For example, here is a way to insert each of the pairs (1,1) through (100, 100) into T1 of the above two examples:

    DECLARE

    i NUMBER := 1;

    BEGIN

    LOOP

    INSERT INTO T1 VALUES(i,i);

    i := i+1;

  • 8/12/2019 Oracle PL Notes

    21/49

    EXIT WHEN i>100;

    END LOOP;

    END;

    .run;

    Some other useful loop-forming statements are:

    * EXIT by itself is an unconditional loop break. Use it inside a conditional if you like.* A WHILE loop can be formed with

    WHILE LOOP

    END LOOP;

    * A simple FOR loop can be formed with:

    FOR IN .. LOOP

    END LOOP;

    Here, can be any variable; it is local to the for-loop and need not be declared. Also, and are constants.

    PL/SQL Tutorial- pages ...1 2

    Cursors

    A cursor is a variable that runs through the tuples of some relation. This relation can be a storedtable, or it can be the answer to some query. By fetching into the cursor each tuple of therelation, we can write a program to read and process the value of each such tuple. If the relationis stored, we can also update or delete the tuple at the current cursor position.

    http://www.freejavaguide.com/plsql.htmhttp://www.freejavaguide.com/plsql.htmhttp://www.freejavaguide.com/plsql.htmhttp://www.freejavaguide.com/plsql.htm
  • 8/12/2019 Oracle PL Notes

    22/49

    The example below illustrates a cursor loop. It uses our example relation T1(e,f) whose tuplesare pairs of integers. The program will delete every tuple whose first component is less than thesecond, and insert the reverse tuple into T1.

    1) DECLARE

    /* Output variables to hold the result of the query: */

    2) a T1.e%TYPE;

    3) b T1.f%TYPE;

    /* Cursor declaration: */

    4) CURSOR T1Cursor IS

    5) SELECT e, f

    6) FROM T1

    7) WHERE e < f

    8) FOR UPDATE;

    9) BEGIN

    10) OPEN T1Cursor;

    11) LOOP

    /* Retrieve each row of the result of the above query

    into PL/SQL variables: */

    12) FETCH T1Cursor INTO a, b;

    /* If there are no more rows to fetch, exit the loop: */

    13) EXIT WHEN T1Cursor%NOTFOUND;

    /* Delete the current tuple: */

    14) DELETE FROM T1 WHERE CURRENT OF T1Cursor;

    /* Insert the reverse tuple: */

  • 8/12/2019 Oracle PL Notes

    23/49

    15) INSERT INTO T1 VALUES(b, a);

    16) END LOOP;

    /* Free cursor used by the query. */

    17) CLOSE T1Cursor;

    18) END;

    19) .

    20) run;

    Here are explanations for the various lines of this program:

    * Line (1) introduces the declaration section.* Lines (2) and (3) declare variables a and b to have types equal to the types of attributes e and fof the relation T1. Although we know these types are INTEGER, we wisely make sure thatwhatever types they may have are copied to the PL/SQL variables (compare with the previousexample, where we were less careful and declared the corresponding variables to be of typeNUMBER).* Lines (4) through (8) define the cursor T1Cursor. It ranges over a relation defined by theSELECT-FROM-WHERE query. That query selects those tuples of T1 whose first component isless than the second component. Line (8) declares the cursor FOR UPDATE since we willmodify T1 using this cursor later on Line (14). In general, FOR UPDATE is unnecessary if thecursor will not be used for modification.* Line (9) begins the executable section of the program.* Line (10) opens the cursor, an essential step.* Lines (11) through (16) are a PL/SQL loop. Notice that such a loop is bracketed by LOOP andEND LOOP. Within the loop we find:o On Line (12), a fetch through the cursor into the local variables. In general, the FETCHstatement must provide variables for each component of the tuple retrieved. Since the query ofLines (5) through (7) produces pairs, we have correctly provided two variables, and we knowthey are of the correct type.o On Line (13), a test for the loop-breaking condition. Its meaning should be clear:%NOTFOUND after the name of a cursor is true exactly when a fetch through that cursor hasfailed to find any more tuples.o On Line (14), a SQL DELETE statement that deletes the current tuple using the specialWHERE condition CURRENT OF T1Cursor.o On Line (15), a SQL INSERT statement that inserts the reverse tuple into T1.* Line (17) closes the cursor.* Line (18) ends the PL/SQL program.* Lines (19) and (20) cause the program to execute.

  • 8/12/2019 Oracle PL Notes

    24/49

    Procedures

    PL/SQL procedures behave very much like procedures in other programming language. Here isan example of a PL/SQL procedure addtuple1 that, given an integer i, inserts the tuple (i, 'xxx')into the following example relation:

    CREATE TABLE T2 (

    a INTEGER,

    b CHAR(10)

    );

    CREATE PROCEDURE addtuple1(i IN NUMBER) AS

    BEGIN

    INSERT INTO T2 VALUES(i, 'xxx');

    END addtuple1;

    run;

    A procedure is introduced by the keywords CREATE PROCEDURE followed by the procedurename and its parameters. An option is to follow CREATE by OR REPLACE. The advantage ofdoing so is that should you have already made the definition, you will not get an error. On theother hand, should the previous definition be a different procedure of the same name, you willnot be warned, and the old procedure will be lost.

    There can be any number of parameters, each followed by a mode and a type. The possiblemodes are IN (read-only), OUT (write-only), and INOUT (read and write). Note: Unlike the typespecifier in a PL/SQL variable declaration, the type specifier in a parameter declaration must beunconstrained. For example, CHAR(10) and VARCHAR(20) are illegal; CHAR or VARCHARshould be used instead. The actual length of a parameter depends on the corresponding argumentthat is passed in when the procedure is invoked.

    Following the arguments is the keyword AS (IS is a synonym). Then comes the body, which isessentially a PL/SQL block. We have repeated the name of the procedure after the END, but thisis optional. However, the DECLARE section should not start with the keyword DECLARE.Rather, following AS we have:

    ... AS

  • 8/12/2019 Oracle PL Notes

    25/49

    BEGIN

    END;

    .

    run;

    The run at the end runs the statement that creates the procedure; it does not execute theprocedure. To execute the procedure, use another PL/SQL statement, in which the procedure isinvoked as an executable statement. For example:

    BEGIN addtuple1(99); END;

    .

    run;

    The following procedure also inserts a tuple into T2, but it takes both components as arguments:

    CREATE PROCEDURE addtuple2(

    x T2.a%TYPE,

    y T2.b%TYPE)

    AS

    BEGIN

    INSERT INTO T2(a, b)

    VALUES(x, y);

    END addtuple2;

    .

    run;

    Now, to add a tuple (10, 'abc') to T2:

  • 8/12/2019 Oracle PL Notes

    26/49

    BEGIN

    addtuple2(10, 'abc');

    END;

    .

    run;

    The following illustrates the use of an OUT parameter:

    CREATE TABLE T3 (

    a INTEGER,

    b INTEGER

    );

    CREATE PROCEDURE addtuple3(a NUMBER, b OUT NUMBER)

    AS

    BEGIN

    b := 4;

    INSERT INTO T3 VALUES(a, b);

    END;

    .

    run;

    DECLARE

    v NUMBER;

    BEGIN

    addtuple3(10, v);

  • 8/12/2019 Oracle PL Notes

    27/49

    END;

    run;

    Note that assigning values to parameters declared as OUT or INOUT causes the corresponding

    input arguments to be written. Because of this, the input argument for an OUT or INOUTparameter should be something with an "lvalue", such as a variable like v in the example above.A constant or a literal argument should not be passed in for an OUT/INOUT parameter.

    We can also write functions instead of procedures. In a function declaration, we follow theparameter list by RETURN and the type of the return value:

    CREATE FUNCTION () RETURN AS ...

    In the body of the function definition, "RETURN ;" exits from the function andreturns the value of .

    To find out what procedures and functions you have created, use the following SQL query:

    select object_type, object_name

    from user_objects

    where object_type = 'PROCEDURE'

    or object_type = 'FUNCTION';

    To drop a stored procedure/function:

    drop procedure ;

    drop function ;

    Discovering Errors

    PL/SQL does not always tell you about compilation errors. Instead, it gives you a crypticmessage such as "procedure created with compilation errors". If you don't see what is wrongimmediately, try issuing the command

    show errors procedure ;

    Alternatively, you can type, SHO ERR (short for SHOW ERRORS) to see the most recentcompilation error.

    Printing Variables

  • 8/12/2019 Oracle PL Notes

    28/49

    Sometimes we might want to print the value of a PL/SQL local variable. A ``quick-and-dirty''way is to store it as the sole tuple of some relation and after the PL/SQL statement print therelation with a SELECT statement. A more couth way is to define a bind variable, which is theonly kind that may be printed with a print command. Bind variables are the kind that must beprefixed with a colon in PL/SQL statements.

    The steps are as follows:

    1. We declare a bind variable as follows:

    VARIABLE

    where the type can be only one of three things: NUMBER, CHAR, or CHAR(n).

    2. We may then assign to the variable in a following PL/SQL statement, but we must prefix itwith a colon.

    3. Finally, we can execute a statement

    PRINT :;

    outside the PL/SQL statement

    Here is a trivial example, which prints the value 1.

    VARIABLE x NUMBER

    BEGIN

    :x := 1;

    END;

    .

    run;

    PRINT :x;

    PL/SQL Tutorial (Examples)- page 3

    PL/SQL BLOCK

  • 8/12/2019 Oracle PL Notes

    29/49

    The pl/sql block contains the following section:--

    -----The DECLARE section.

    -----The Master BEGIN and END section that contains the EXCEPTIONsection.

    The declare section contains declaration of memory variables, constants,

    cursors etc. The begin section contains sql executable statements and pl/sqlexecutable statements. The exception section contains code to handle errorsthat may arise during the execution of the code block. The end declares theend of pl/sql block.

    A bit about it's working. When you typed out the pl/sql block for execution.It is sent to the pl/sql engine, where procedural statements are executed;and sql statements are sent to the sql executor in the oracle engine. Since

    pl/sql engine resides in the oracle engine, the codes executes smoothly and

    efficiently.

    PL/SQL DATA-TYPE

    This is easy since it includes almost all the data types which u have used in sql such as date,varchar, number, char etc etc... Some of the attributes such as %TYPE is also used. This attributeautomatically takes in the default data type of the sql table from which u have passed the query.We will discuss this later.

    Remember in pl/sql a variable name must begin with a character and can be followed bymaximum of 29 other characters. Reserved words can't be used unless enclosed within doublequotes. Variables must be separated from each other by at least one space or by a punctuationmark. You can assign values of operator using := operator. I won't discuss about logicalcomparisons operators such as , >=, NOT, TRUE, AND, OR, NULL etc since they r quiteeasy to understand.

    HOW TO DISPLAY MESSAGES ON SCREEN ---

    DBMS_OUTPUT : is a package that includes a number of procedure and functions thataccumulate information in a buffer so that it can be retrieved later. These functions can also beused to display messages to the user.PUT_LINE : Put a piece of information in the package buffer followed by an end-of-line marker.It can also be used to display message to the user. Put_line expects a single parameter ofcharacter data type. If used to display a message, it is the message 'string'.EG: dbms_output.put_line(x);

    REMEMBER: To display messages to the user the SERVEROUTPUT should be set to ON.SERVEROUTPUT is a sql*plus environment parameter that displays the information pased as aparameter to the PUT_LINE function.

  • 8/12/2019 Oracle PL Notes

    30/49

  • 8/12/2019 Oracle PL Notes

    31/49

    a number;b number;c number;begina:=&a;

    b:=&b;c:=a+b;dbms_output.put_line('Sum of ' || a || ' and ' || b || ' is ' || c);

    Here & is used to take user input at runtime.....

    --SUM OF 100 NUMBERS

    Declarea number;s1 number default 0;

    Begina:=1;loops1:=s1+a;exit when (a=100);a:=a+1;end loop;dbms_output.put_line('Sum between 1 to 100 is '||s1);End;

    --SUM OF odd NUMBERS USING USER INPUT...for loop

    declaren number;sum1 number default 0;endvalue number;beginendvalue:=&endvalue;n:=1;for n in 1.. endvalueloopif mod(n,2)=1thensum1:=sum1+n;end ifend loop;dbms_output.put_line('sum = ' || sum1);end;

    --SUM OF 100 ODD NUMBER .. WHILE LOOP

  • 8/12/2019 Oracle PL Notes

    32/49

    declaren number;endvalue number;sum1 number default 0;

    beginendvalue:=&endvalue;n:=1;while (n < endvalue)loopsum1:=sum1+n;n:=n+2;end loop;dbms_output.put_line('Sum of odd numbers between 1 and ' || endvalue || ' is ' || sum1);end;

    --CALCULATION OF NET SALARY

    declareename varchar2(15);basic number;da number;hra number;pf number;netsalary number;beginename:=&ename;basic:=&basic;

    da:=basic * (41/100);hra:=basic * (15/100);

    if (basic < 3000)thenpf:=basic * (5/100);elsif (basic >= 3000 and basic = 5000 and basic

  • 8/12/2019 Oracle PL Notes

    33/49

    dbms_output.put_line('Providend Fund : ' || pf);dbms_output.put_line('Net salary : ' || netsalary);end;

    --MAXIMUM OF 3 NUMBERS

    Declarea number;b number;c number;d number;Begindbms_output.put_line('Enter a:');a:=&a;dbms_output.put_line('Enter b:');b:=&b;

    dbms_output.put_line('Enter c:');c:=&b;if (a>b) and (a>c) thendbms_output.putline('A is Maximum');elsif (b>a) and (b>c) thendbms_output.putline('B is Maximum');elsedbms_output.putline('C is Maximum');end if;End;

    --QUERY EXAMPLE--IS SMITH EARNING ENOUGH

    declares1 emp.sal %type;beginselect sal into s1 from empwhere ename = 'SMITH';if(no_data_found)thenraise_application_error(20001,'smith is not present');end if;

    if(s1 > 10000)thenraise_application_error(20002,'smith is earning enough');end if;

  • 8/12/2019 Oracle PL Notes

    34/49

    update emp set sal=sal + 500where ename='SMITH';end;

    --PRIME NO OR NOT

    DECLAREno NUMBER (3) := &no;a NUMBER (4);b NUMBER (2);BEGINFOR i IN 2..no - 1LOOPa := no MOD i;IF a = 0THEN

    GOTO out;END IF;END LOOP;IF a = 1THENDBMS_OUTPUT.PUT_LINE (no || ' is a prime number');ELSEDBMS_OUTPUT.PUT_LINE (no || ' is not a prime number');END IF;END;

    --SIMPLE EXAMPLE OF LOOP STATEMENT I.E. EXIT WHEN

    Declarea number:= 100;beginloopa := a+25;exit when a=250;end loop;dbms_output.put_line (to_Char(a));end;

    --EXAMPLE OF WHILE LOOP

    Declarei number:=0;j number:= 0;begin

  • 8/12/2019 Oracle PL Notes

    35/49

    while i 7000 thengoto Upd;end if;>Update price set minprice = 6999 where prodid=111;end;

    --CALCULATE THE AREA OF A CIRCLE FOR A VALUE OF RADIUS VARYING FROM3 TO 7. STORE THE RADIUS AND THE CORRESPONDING VALUES OF CALCULATEDAREA IN A TABLE AREAS.

    Declarepi constant number(4,2) := 3.14;radius number(5);area number(14,2);

    Beginradius := 3;While radius

  • 8/12/2019 Oracle PL Notes

    36/49

    Insert into areas values (radius, area);radius:= radius+1;end loop;end;

    --REVERSING A NUMBER 5639 TO 9365

    Declaregiven_number varchar(5) := '5639';str_length number(2);inverted_number varchar(5);

    Beginstr_length := length(given_number);For cntr in reverse 1..str_lengthloop

    inverted_number := inverted_number || substr(given_number, cntr, 1);end loop;dbms_output.put_line('The Given no is ' || given_number);dbms_output.put_line('The inverted number is ' || inverted_number);end;

    EXCEPTION HANDLING IN PLSQL

    Errors in pl/sql block can be handled...error handling refers to the way we handle the errors inpl/sql block so that no crashing stuff of code takes place...This is exactly the same as we do inC++ or java..right!!There are two type:===> predefined exceptions===> user defined exceptionsThe above 2 terms are self explanatory

    predefined exceptions:

    No-data-found == when no rows are returnedCursor-already-open == when a cursor is opened in advanceDup-val-On-index == for duplicate entry of index..Storage-error == if memory is damagedProgram-error == internal problem in pl/sqlZero-divide == divide by zeroinvalid-cursor == if a cursor is not open and u r trying to close itLogin-denied == invalid user name or passwordInvalid-number == if u r inserting a string datatype for a number datatype which is alreadydeclaredToo-many-rows == if more rows r returned by select statement

  • 8/12/2019 Oracle PL Notes

    37/49

    SYNTAX

    beginsequence of statements;exception

    when --exception name thensequence of statements;end;

    EXAMPLES

    --When there is no data returned by rowdeclareprice item.actualprice%type;beginSelect actual price into price from item where qty=888;

    when no-data-found thendbms_output.put_line('item missing');end;

    --EXAMPLE OF USER DEFINED EXCEPTION

    DECLAREe_rec emp%ROWTYPE;e1 EXCEPTION;sal1 emp.sal%TYPE;BEGINSELECT sal INTO sal1 FROM emp WHERE deptno = 30 AND ename = 'John';IF sal1 < 5000 THENRAISE e1;sal1 := 8500;UPDATE emp SET sal = sal1 WHERE deptno = 30 AND ename = 'John';END IF;EXCEPTIONWHEN no_data_found THENRAISE_APPLICATION_ERROR (-20001, 'John is not there.');WHEN e1 THENRAISE_APPLICATION_ERROR (-20002, 'Less Salary.');END;

    --EXAMPLE OF RAISE-APPLICATION-ERROR... THIS IS YOUR OWN ERROR

    STATEMENT...YOU RAISE YOUR OWN ERROR

    Declares1 emp.sal %type;begin

  • 8/12/2019 Oracle PL Notes

    38/49

    select sal into s1 from emp where ename='SOMDUTT';if(no-data-found) thenraise_application_error(20001, 'somdutt is not there');end if;if(s1 > 10000) then

    raise_application_error(20002, 'somdutt is earing a lot');end if;update emp set sal=sal+500 where ename='SOMDUTT';end;

    --INTERESTING EG OF USER DEFINED EXCEPTIONS

    Declarezero-price exception;price number(8);begin

    select actualprice into price from item where ordid =400;if price=0 or price is null thenraise zero-price;end if;exceptionwhen zero-price thendbms_output.put_line('raised xero-price exception');end;

    PL/SQL Tutorial (Examples)- page 4 (go to page 1)

    CURSORS

    Cursor is a work area in pl/sql which is used by sql server used to store theresult of a query. Each column value is pointed using pointer. You can

    independently manipulate cursor values. A bit about it's working..... supposeyou ask for a query stored in the server ... at first a cursor consisting of

    query result is created in server...now the cursor is transferred to the clientwhere again cursor is created and hence the result is displayed......

    Cursors are of 2 types: implicit and explicit.......implicit cursors are createdby oracle engine itself while explicit cursors are created by theusers......cursors are generally used in such a case when a query returns

    http://www.freejavaguide.com/plsql.htmhttp://www.freejavaguide.com/plsql.htmhttp://www.freejavaguide.com/plsql.htm
  • 8/12/2019 Oracle PL Notes

    39/49

    more than one rows....normal pl/sql returning more than one rows givens

    error but using cursor this limitation can be avoided....so cursors are used....

    Cursor attributes

    %ISOPEN == returns true if ursor is open, false otherwise%FOUND == returns true if recod was fetched successfully, false otherwise%NOTFOUND == returns true if record was not fetched successfully, false otherwise%ROWCOUNT == returns number of records processed from the cursor.

    Very important: Cursor can be controlled using following 3 control statements. They are Open,Fetch, Close.....open statement identifies the active set...i.e. query returned by selectstatement...close statement closes the cursor...and fetch statement fetches rows into thevariables...Cursors can be made into use using cursor for loop and fetch statement...we will seethe corresponding examples...

    EXAMPLES

    --EXAMPLE OF SQL%FOUND (IMPLICIT CURSORS)

    beginupdate employee set salary=salary *0.15where emp_code = &emp_code;if sql%found thendbms_output.put_line('employee record modified successfully');elsedbms_output.put_line('employee no does not exist');

    end if;end;

    --EXAMPLE FOR SQL%NOTFOUND (IMPLICIT CURSORS)

    beginupdate employee set salary = salary*0.15 where emp_code = &emp_code;if sql%notfound thendbms_output.put_line('employee no . does not exist');elsedbms_output.put_line('employee record modified successfully');

    end if;end;

    --EXAMPLE FOR SQL%ROWCOUNT (IMPLICIT CURSORS)

    declarerows_affected char(4);begin

  • 8/12/2019 Oracle PL Notes

    40/49

    update employee set salary = salary*0.15 where job='programmers';rows_affected := to_char(sql%rowcount);if sql%rowcount > 0 thendbms_output.put_line(rows_affected || 'employee records modified successfully');else

    dbms_output.put_line('There are no employees working as programmers');end if;end;

    Syntax of explicit cursor: Cursor cursorname is sql select statement;Syntax of fetch : fetch cursorname into variable1, variable2...;Syntax of close; close cursorname;Syntax of open cursor; open cursorname;

    --EXPLICIT CURSOR EG

    DECLARECURSOR c1 is SELECT * FROM emp;str_empno emp.empno%type;str_ename emp.ename%type;str_job emp.job%type;str_mgr emp.mgr%type;str_hiredate emp.hiredate%type;str_sal emp.sal%type;str_comm emp.comm%type;str_deptno emp.deptno%type;rno number;BEGINrno := &rno;FOR e_rec IN c1LOOPIF c1%rowcount = rno THENDBMS_OUTPUT.PUT_LINE (str_empno || ' ' || str_ename || ' ' || str_job || ' ' || str_mgr || ' ' ||str_hiredate || ' ' || str_sal || ' ' || str_comm || ' ' || str_deptno);END IF;END LOOP;END;

    --ANOTHER EG DISPLAYING VALUE OF A TABLE

    DECLARECURSOR c1 IS SELECT * FROM emp;e_rec emp%rowtype;BEGINOPEN c1;

  • 8/12/2019 Oracle PL Notes

    41/49

    LOOPFETCH c1 INTO e_rec;DBMS_OUTPUT.PUT_LINE('Number: ' || ' ' || e_rec.empno);DBMS_OUTPUT.PUT_LINE('Name : ' || ' ' || e_rec.ename);DBMS_OUTPUT.PUT_LINE('Salary: ' || ' ' || e_rec.sal);

    EXIT WHEN c1%NOTFOUND;END LOOP;CLOSE c1;END;

    -- Display details of Highest 10 salary paid employee

    DECLARECURSOR c1 IS SELECT * FROM emp ORDER BY sal DESC;e_rec emp%rowtype;BEGIN

    FOR e_rec IN c1LOOPDBMS_OUTPUT.PUT_LINE('Number: ' || ' ' || e_rec.empno);DBMS_OUTPUT.PUT_LINE('Name : ' || ' ' || e_rec.ename);DBMS_OUTPUT.PUT_LINE('Salary: ' || ' ' || e_rec.sal);EXIT WHEN c1%ROWCOUNT >= 10;END LOOP;END;

    -- EXAMPLE OF CURSOR FOR LOOP

    declare cursor c1 is select * from somdutt;beginfor outvariable in c1loopexit when c1%notfound;if outvariable.age < 21 thendbms_output.put_line(outvariable.age || ' ' || outvariable.name);end if;end loop;end;

    --ref STRONG CURSORS

    DECLARETYPE ecursor IS REF CURSOR RETURN emp%ROWTYPE;ecur ecursor;e_rec emp%ROWTYPE;dn NUMBER;BEGIN

  • 8/12/2019 Oracle PL Notes

    42/49

    dn := &deptno;OPEN ecur FOR SELECT * FROM emp WHERE deptno = dn;FOR e_rec IN ecurLOOPDBMS_OUTPUT.PUT_LINE ('Employee No : ' || e_rec.empno);

    DBMS_OUTPUT.PUT_LINE ('Employee Salary: ' || e_rec.salary);END LOOP;END;

    --REF WEAK CURSORS

    DECLARETYPE tcursor IS REF CURSOR;tcur tcursor;e1 emp%ROWTYPE;d1 dept%ROWTYPE;

    tname VARCHAR2(20);BEGINtname := &tablename;IF tname = 'emp' THENOPEN tcur FOR SELECT * FORM emp;DBMS_OUTPUT.PUT_LINE ('Emp table opened.');close tcur;DBMS_OUTPUT.PUT_LINE ('Emp table closed.');ELSE IF tname = 'dept' THENOPEN tcur FOR SELECT * FROM dept;DBMS_OUTPUT.PUT_LINE ('Dept table opened.');close tcur;DBMS_OUTPUT.PUT_LINE ('Emp table closed.');ELSERAISE_APPLICATION_ERROR (-20004, 'Table name is wrong');END IF;END;

    --CURSOR FOR LOOP WITH PARAMETERS

    DeclareCursor c1(Dno number) is select * from emp where deptno = dno;beginfor empree in c1(10) loop;dbms_output.put_line(empree.ename);end loop;end;

    TRIGGERS

  • 8/12/2019 Oracle PL Notes

    43/49

    Trigger is a stored procedure which is called implicitly by oracle engine whenever a insert,update or delete statement is fired.

    Advantages of database triggers:---> Data is generated on it's own

    ---> Replicate table can be maintained---> To enforce complex integrity contraints---> To edit data modifications---> To autoincrement a fieldetc..

    Syntax: Create or replace trigger --triggername-- [before/after] [insert/pdate/delete] on --tablename-- [for each satement/ for each row] [when --condition--] plus..begin.and exception

    Triggers are of following type: before or after trigger ....and for each row and for each statementtrigger... before trigger is fired before insert/update/delete statement while after trigger is fired

    after insert/update/delete statement...for each row and for each statements triggers are selfexplainatory..

    EXAMPLE

    -- A database trigger that allows changes to employee table only during the business hours(i.e.from 8 a.m to 5.00 p.m.) from monday to saturday. There is no restriction on viewing data fromthe table -CREATE OR REPLACE TRIGGER Time_Check BEFORE INSERT OR UPDATEOR DELETE ON EMP BEGIN IF TO_NUMBER(TO_CHAR(SYSDATE,'hh24')) < 10 ORTO_NUMBER(TO_CHAR(SYSDATE,'hh24')) >= 17 OR TO_CHAR(SYSDATE,'DAY') ='SAT' OR TO_CHAR(SYSDATE,'DAY') = 'SAT' THEN RAISE_APPLICATION_ERROR (-20004,'YOU CAN ACCESS ONLY BETWEEN 10 AM TO 5 PM ON MONDAY TO FRIDAYONLY.'); END IF; END; --YOU HAVE 2 TABLES WITH THE SAME STRUCTURE. IF UDELETE A RECORD FROM ONE TABLE , IT WILL BE INSERTED IN 2ND TABLE EDTRIGGERNAME Create or replace trigger backup after delete on emp fro each row begin insertinto emp/values (:old.ename,:old.job,:old.sal); end; save the file.. and then sql> @ triggername --To STICK IN SAL FIELD BY TRIGGER MEANS WHEN U ENTER GREATER THAN 5000,THEN THIS TRIGGER IS EXECUTED Create or replace trigger check before insert on emp foreach row when (New.sal > 5000); begin raise_application_error(-20000, 'your no is greater than5000'); end; --NO CHANGES CAN BE DONE ON A PARTICULAR TABLE ON SUNDAYAND SATURDAY Create or replace trigger change before on emp for each row when(to_char(sysdate,'dy') in ('SAT','SUN')) begin raise_application_error(-200001, 'u cannot enterdata in saturnday and sunday'); end; --IF U ENTER IN EMP TABLE ENAME FIELD'S DATAIN ANY CASE IT WILL BE INSERTED IN CAPITAL LETTERS'S ONLY Create or replacetrigger cap before insert on emp for each row begin :New.ename = upper(:New.ename); end; --ATRIGGER WHICH WILL NOT ALLOW U TO ENTER DUPLICATE VALUES IN FIELDEMPNO IN EMP TABLE Create or replace trigger dubb before insert on emp for each rowDeclare cursor c1 is select * from emp; x emp%rowtype; begin open c1; loop fetch c1 into x; if:New.empno = x.empno then dbms_output.put_line('you entered duplicated no'); elseif

  • 8/12/2019 Oracle PL Notes

    44/49

    :New.empno is null then dbms_output.put_line('you empno is null'); end if; exit whenc1%notfound; end loop; close c1; end;

    Remember trigger can be dropped using Drop Trigger triggername ; statement...

    PROCEDURES AND FUNCTIONS

    procedure is a subprogram...which consists of a set of sql statement. Procedures are not verydifferent from functions. A procedure or function is a logically grouped set of SQL and PL/SQLstatements that perform a specific task. A stored procedure or function is a named pl/sql codeblock that have been compiled and stored in one of the oracle engines's system tables.

    To make a procedure or function dynamic either of them can be passed parameters beforeexecution. A procedure or function can then change the way it works depending upon theparameters passed prior to its execution.

    Procedures and function are made up of a declarative part, an executable part and an optionalexception-handling part

    A declaration part consists of declarations of variables. A executable part consists of the logic i.e.sql statements....and exception handling part handles any error during run-time

    The oracle engine performs the following steps to execute a procedure or function....Verifies useraccess, Verifies procedure or function validity and executes the procedure or function. Some ofthe advantages of using procedures and functions are: security, performance, memory allocation,productivity, integrity.

    Most important the difference between procedures and functions: A function must return a valueback to the caller. A function can return only one value to the calling pl/sql block. By definingmultiple out parameters in a procedure, multiple values can be passed to the caller. The outvariable being global by nature, its value is accessible by any pl/sql code block including thecalling pl/sql block.

    Syntax for stored procedure:CREATE OR REPLACE PROCEDURE [schema] procedure name (argument { IN, OUT, INOUT} data type, ..) {IS, AS}variable declarations; constant declarations; BEGINpl/sql subprogram body;EXCEPTIONexception pl/sql block;END;

    Syntax for stored function:CREATE OR REPLACE FUNCTION [schema] functionname(argument IN data type, ..)RETURN data type {IS, AS}variable declarations; constant declarations; BEGIN

  • 8/12/2019 Oracle PL Notes

    45/49

  • 8/12/2019 Oracle PL Notes

    46/49

    --CREATE A PROCEDURE THAT DELETE ROWS FROM ENQUIRY--WHICH ARE 1 YRS BEFORE

    Create or replace procedure myprocedure is begindelete from enquiry where enquirydate

  • 8/12/2019 Oracle PL Notes

    47/49

    --IN and OUT procedure example

    Create or replace procedure lest ( a number, b out number) isidentify number;begin

    select ordid into identity from item whereitemid = a;if identity < 1000 thenb := 100;end if;end l

    --in out parameter

    Create or replace procedure sample ( a in number, b in out number) isidentity number;

    beginselect ordid, prodid into identity, b from item where itemid=a;if b

  • 8/12/2019 Oracle PL Notes

    48/49

    getsal(sal1);dbms_output.put_line('The employee salary is' || sal1);end ;

    You can make a procedure and functions similarly.....also if u wanna drop a function then usedrop function functionname and for procedure use drop procedure procedurename

    PACKAGES

    A package is an oracle object, which holds other objects within it. Objects commonly held withina package are procedures, functions, variables, constants, cursors and exceptions. Packages inplsql is very much similar to those packages which we use in JAVA......yeah!! java packagesholds numerous classes..right!!!...

    A package has 2 parts..... package specification and package body

    A package specification part consists of all sort of declaration of functions and procedures whilepackage body consists of codings and logic of declared functions and procedures...

    EXAMPLE

    --SIMPLEST EG

    --specificationcreate or replace package pack2 isfunction rmt(x in number) return number;procedure rmt1(x in number);end;

    --bodycreate or replace package body pack2 isfunction rmt(x in number) return number isbeginreturn (x*x);end;

    procedure rmt1(x in number) isbegindbms_output.put_line(x*x);end;end;

    (how to run.....)

  • 8/12/2019 Oracle PL Notes

    49/49

    exec packagename.procedurenamei.e.exec pack2.rmt1(3);

    As shown above u can put in complicated procedures and functions inside the package...I have

    just shown a simple example...you can easily modify the above code to fit yourrequirement......Just try out packages which includes cursors, procedures andfunctions..etc..Remeber pl/sql supports overloading...i.e. you can use the same function orprocedure name in your application but with different no or type of arguments.


Recommended