SlideShare ist ein Scribd-Unternehmen logo
1 von 44
Downloaden Sie, um offline zu lesen
PL/SQL - Overview

      Gagan Deep
       Assistant Professor
       University College,
Kurukshetra University, Kurukshetra
What is PL/SQL ?


• Procedural Language – SQL
• An extension to SQL with design features of
  programming languages (procedural and object
  oriented)
• PL/SQL and Java are both supported as internal
  host languages within Oracle products.
  Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
PL/SQL
                    The      PL/SQL      procedural
                    language was developed by
                    Oracle Corporation in the late
                    1980s as procedural extension
language for SQL and the Oracle relational database.
Following are notable facts about PL/SQL:
•PL/SQL is a completely portable, high-performance
transaction-processing language.
•PL/SQL provides a built-in interpreted and OS
independent programming environment.
  Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
• PL/SQL can also directly be
                                        called from the command-
                                        line SQL*Plus interface.
                                      • Direct call can also be made
                                        from external programming
                                        language calls to database.
• PL/SQL's general syntax is based on that of ADA
  and Pascal programming language.
• Apart from Oracle, PL/SQL is available in
  TimesTen in-memory database and IBM DB2.

  Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
Why PL/SQL
                  • Acts as host language for
                     stored procedures and triggers.
                  • Provides the ability to add
                     middle tier business logic to
                     client/server applications.
• Provides Portability of code from one
  environment to another
• Improves      performance       of    multi-query
  transactions.
• Provides error handling
  Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
Features of PL/SQL
                                          PL/SQL has the following features:
                                          • PL/SQL is tightly integrated with
                                            SQL.
                                          • It offers extensive error checking.
• It offers numerous data types.
• It offers a variety of programming structures.
• It supports structured programming through functions
  and procedures.
• It supports object oriented programming.
• It supports developing web applications and server
  pages.
   Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
Advantages of PL/SQL
                     PL/SQL has the following
                     advantages :
                     •SQL is the standard database
                     language and PL/SQL is
   strongly integrated with SQL. PL/SQL supports
  both static and dynamic SQL.
• Static SQL supports DML operations and
  transaction control from PL/SQL block.
• Dynamic SQL is SQL allows embedding DDL
  statements in PL/SQL blocks.
  Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
• PL/SQL allows sending an
                     entire block of statements to the
                     database at one time. This
                     reduces network traffic and
                     provides high performance for
                     the applications.
• PL/SQL give high productivity to programmers as
  it can query, transform, and update data in a
  database.
• PL/SQL saves time on design and debugging by
  strong features, such as exception handling,
  encapsulation, data hiding, and object-oriented.
   Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
• Applications     written   in
                                            PL/SQL are fully portable.
                                          • PL/SQL       provides    high
                                            security level.
                                          • PL/SQL provides access to
                                            predefined SQL packages.
• PL/SQL provides support for Object-Oriented
  Programming.
• PL/SQL provides support for Developing Web
  Applications and Server Pages.

   Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
Client-Server Architecture
• PL/SQL is a part of the Oracle RDBMS, and it can reside
  in two environments, the client and the server.
• Many Oracle applications are built using client-server
  architecture.
    The Oracle database resides on the server.
•   The program(` C, Java, or PL/SQL) that makes requests
    against this database resides on the client machine.
•   As a result, it is very easy to move PL/SQL modules
    between server-side and client-side applications.
•   When the PL/SQL engine is located on the server, the
    whole PL/SQL block is passed to the PL/SQL engine on
    the Oracle server. The PL/SQL engine processes the
    block according to the Figure 1.
Figure 1 : The PL/SQL Engine and
Oracle Server
• When the PL/SQL engine
                  is located on the client, as
                  it is in the Oracle
                  Developer Tools, the
                  PL/SQL processing is
                  done on the client side.

• All SQL statements that are embedded within
  the PL/SQL block are sent to the Oracle
  server for further processing. When PL/SQL
  block contains no SQL statement, the entire
  block is executed on the client side.
Comparison of PL/SQL
                                 and SQL
                            • When a SQL statement is issued on
                              the client computer, the request is
                              made to the database on the server,
                              and the result set is sent back to the
                              client.
• As a result, a single SQL statement causes two trips on the
  network. If multiple SELECT statements are issued, the
  network traffic increase significantly very fast. For example,
  four SELECT statements cause eight network trips.
• If these statements are part of the PL/SQL block, they are
  sent to the server as a single unit. The SQL statements in this
  PL/SQL program are executed at the server and the result set
  is sent back as a single unit. There is still only one network
  trip made as is in case of a single SELECT statement.
Comparison of PL/SQL
        and SQL
Figure 2 : The PL/SQL in client –
server architecture
PL/SQL Block-Structure
                  PL/SQL is a block-structured
                  language, meaning that PL/SQL
                  programs are divided and written
                  in logical blocks of code. Each
                  block consists of three sub-parts:
1 Declarations :
This section starts with the keyword DECLARE.
   It is an optional section and defines all
   variables, cursors, subprograms, and other
   elements to be used in the program.
 Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
2 Executable Commands
                                 This section is enclosed between
                                    the keywords BEGIN and
                                    END and it is a mandatory
                                    section. It consists of the
                                    executable PL/SQL statements
                                    of the program. It should have
                                    at least one executable line of
                                    code, which may be just a
                                    NULL command to indicate
                                    that    nothing   should     be
                                    executed.
Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
3 Exception Handling
                                       This section starts with the
                                          keyword       EXCEPTION.
                                          This section is again optional
                                          and contains exception(s)
                                          that handle errors in the
                                          program.

  Every PL/SQL statement end with a semicolon
  (;).
  PL/SQL blocks can be nested within other
  PL/SQL blocks using BEGIN and END.
Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
PL/SQL Block Structure
                                 DECLARE (optional)
                                  - variable declarations
                                 BEGIN (mandatory)
                                  - SQL statements
                                  - PL/SQL statements or sub-
                                  blocks
                                 EXCEPTION (optional)
                                  - actions to perform when errors
                                  occur
                                 END; (mandatory)
Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
PL/SQL Block Types

                                                                                     Function
 Anonymous                              Procedure                        FUNCTION <name>
                                                                         RETURN <datatype>
DECLARE                       PROCEDURE <name>
                                                                         IS
BEGIN                         IS
                                                                         BEGIN
  -statements                 BEGIN
                                                                            -statements
EXCEPTION                        -statements
                                                                         EXCEPTION
END;                          EXCEPTION
                                                                         END;
                              END;
   Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
Example
                                                                   The      end;     line
                                                                   signals the end of
                                                                   the PL/SQL block.
DECLARE
message varchar2(20):= 'Hello, World!';                            To run the code
BEGIN                                                              from SQL command
dbms_output.put_line(message);                                     line, you may need
END;                                                               to type / at the
 /                                                                 beginning of the
Hello, World !                                                     first blank line after
PL/SQL procedure successfully                                      the last line of the
  completed.
                                                                   code.
    Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
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 integer or INTEGER to represent a
numeric value. You cannot use a reserved keyword as
an identifier.
 Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
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

Similarly, there are                   :                      Host variable indicator

more delimiters.                       ,                      Item separator
   Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
The PL/SQL Comments
 • Program comments are explanatory statements that you
    can include in the PL/SQL code that you write and
    helps anyone reading it's source code. All
    programming languages allow for some form of
    comments.
• The PL/SQL supports single line and multi-line
   comments. All characters available inside any
   comment are ignored by PL/SQL compiler. The
   PL/SQL single-line comments start with the delimiter
   -- (double hyphen) and multi-line comments are
   enclosed by /* and */.
   Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
DECLARE
                                      -- variable declaration
                                      message varchar2(20):= 'Hello,
                                      World!’;
                                    BEGIN
                                          /*
                                           PL/SQL executable statement(s)
Example                                   */
                                      dbms_output.put_line(message);
                                    END;
                                    /
After Execution in SQL the result is : Hello, World!
PL/SQL procedure successfully completed.
   Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
Data Types
PL/SQL variables, constants and parameters must have
a valid data types which specifies a storage format,
constraints, and valid range of values. PL/SQL is
strongly typed. The following is the list of data types:
Category                                                Description
                                                        Single values with no internal components,
Scalar                                                  such as a NUMBER, DATE, or BOOLEAN.
                                                        Pointers to large objects that are stored
                                                        separately from other data items, such as
Large Object (LOB)                                      text, graphic images, video clips, and sound
                                                        waveforms.
                                                        Data items that have internal components
Composite                                               that can be accessed individually. For
                                                        example, collections and records.

Reference                                               Pointers to other data items.
  Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
Scalar Data Types
                                  • PL/SQL Scalar Data Types and
                                    Subtypes come under the following
                                    categories:
Date Type                 Description
                          Numeric values, on which arithmetic operations
Numeric                   are performed.
                          Alphanumeric values that represent single
Character                 characters or strings of characters.
                          Logical values, on which logical operations are
Boolean                   performed.

Datetime                  Dates and times.
  Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
PL/SQL Numeric Data
                                                 Types
Data Type                                                Description
                                                         ANSI and IBM specific floating-point type
FLOAT                                                    with maximum precision of 126 binary digits
                                                         (approximately 38 decimal digits)
                                                         ANSI and IBM specific integer type with
INTEGER OR INT
                                                         maximum precision of 38 decimal digits
                                                         Floating-point type with maximum precision
REAL                                                     of 63 binary digits (approximately 18 decimal
                                                         digits)
                                                         Signed integer in range -2,147,483,648
PLS_INTEGER OR BINARY_INTEGER
                                                         through 2,147,483,647, represented in 32 bits
                                                         ANSI specific fixed-point type with
DEC(prec, scale)
                                                         maximum precision of 38 decimal digits.


    Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
Following is a valid declaration:
                                     DECLARE
                                       num1 INTEGER;
                                       num2 REAL;
                                       num3 DOUBLE PRECISION;
                                     BEGIN
Example                                null;
                                     END;
                                     /
When the above code is compiled and executed, it
produces following result:
PL/SQL procedure successfully completed
  Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
Character Data Types


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

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

                       Variable-length binary or byte string with maximum size of
RAW                    32,767 bytes, not interpreted by PL/SQL
                       Variable-length character string with maximum size of 32,760
LONG                   bytes
ROWID                  Physical row identifier, the address of a row in an ordinary table

  Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
PL/SQL Operators
                                  An operator is a symbol that tells the
                                  compiler      to   perform    specific
                                  mathematical or logical manipulations.
                                  PL/SQL language is rich in built-in
                                  operators and provides following type
                                  of operators:
                                  •Arithmetic operators
                                  •Relational operators
                                  •Comparison operators
                                  •Logical operators
                                  •String operators
Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
Precedence of Operators

Operator                                                       Operation
**                                                             exponentiation
+, -                                                           identity, negation
*, /                                                           multiplication, division
+, -, ||                                                       addition, subtraction, concat
=, <, >, <=, >=, IS NULL, LIKE, IN Comparison
NOT                                                            logical negation
AND                                                            conjunction
OR                                                             inclusion

     Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
Control Structures
                                    • Programming languages provide
                                      various control structures that
                                      allow for more complicated
                                      execution paths.
• Decision making structures require that the
  programmer specify one or more conditions to be
  evaluated or tested by the program, along with a
  statement or statements to be executed if the
  condition is determined to be true, and optionally,
  other statements to be executed if the condition is
  determined to be false.
    Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
PL/SQL Decisions
                                             Making
                                 PL/SQL language provides
                                 following types of decision
                                 making statements.
                                 •IF-THEN Statement
                                 •IF-THEN-ELSE Statement
                                 •IF-THEN-ELSEIF Statement
                                 •Nested IF Statement
Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
Syntax for IF-THEN statement
  IF condition THEN
  S;
  END IF;
  Where condition is a Boolean or relational
  condition and S is a simple or compound
  statement.
Example of an IF-THEN statement is:
    IF (a <= 20) THEN
          c:= c+1;
          END IF;
  Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
DECLARE                                                          Example
           a number(2) := 10;
         BEGIN
           a:= 10;
         IF( a < 20 ) THEN
         dbms_output.put_line('a is less than 20' );
             END IF;
         dbms_output.put_line('value of a is:' || a);
         END;
         / After Execution :
            a is less than 20 value of a is : 10
            PL/SQL procedure successfully completed.
Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
PL/SQL - Loops
                                  PL/SQL provides the following
                                  types of loop to handle the looping
                                  requirements

                                  •BASIC LOOP
                                  •WHILE LOOP
                                  •FOR LOOP
                                  •NESTED LOOP
Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
Syntax of Basic Loop

The syntax of a basic loop in PL/SQL language
is:
     LOOP
          Sequence of statements;
     END LOOP



 Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
Example
                                   DECLARE
                                   x number := 10;
                                   BEGIN LOOP
                                   dbms_output.put_line(x);
                                   x := x + 10;
                                   IF x > 50 THEN exit; END IF;
                                   END LOOP;
                                   dbms_output.put_line('After Exit x is: ' || x);
                                   END;
                                   /
After Execution is : 10 20 30 40 50 After Exit x is: 60
   Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
Syntax of For Loop
  FOR counter IN i_value .. f_value LOOP
  sequence_of_statements;
  END LOOP;
Following are special characteristics of FOR loop:
The i_value and f_value of the loop variable or counter can
be literals, variables, or expressions but must evaluate to
numbers. Otherwise, PL/SQL raises the predefined
exception VALUE_ERROR.
The i_value need not to be 1; however, the loop counter
increment (or decrement) must be 1.
PL/SQL allows determine the loop range dynamically at
runGagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 Gate, K.U., Kurukshetra
    time.                                                                   rd
Example
                                   DECLARE
                                   a number(2);
                                   BEGIN
Result is
                                   FOR a in 10 .. 15 LOOP
value of a: 10
                                   dbms_output.put_line('value of a: ' ||
value of a: 11
                                     a);
value of a: 12
                                   END LOOP;
value of a: 13
                                   END;
value of a: 14
                                   /
value of a: 15
   Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
Common PL/SQL String
                                    Functions
                               •          CHR(ASCIIvalue)
                               •          ASCII(string)
                               •          LOWER(string)
                               •          SUBSTR(string,start,substrlength)
                               •          LTRIM(string)
                               •          RTRIM(string)
                               •          REPLACE(string, searchstring,
                                          replacestring)
                                        • UPPER(string)
Gagan Deep, Assistant Professor, UCK and
                                        • INITCAP(string)
 Director, Rozy Computech Services, 3
                                        • LENGTH(string)
                          rd

        Gate, K.U., Kurukshetra
Common PL/SQL Numeric
                                     Functions

                                 •    ABS(value)
                                 •    ROUND(value, precision)
                                 •    MOD(value,divisor)
                                 •    SQRT(value)
                                 •    TRUNC(value,|precision|)
                                 •    LEAST(exp1, exp2…)
                                 •    GREATEST(exp1, exp2…
Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
When is PL/SQL
                                                handy
                                 • When something is too
                                   complicated for SQL
                                 • When conditional branching
                                   and looping are needed.




Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
Thanks !
If you have any query please mail me at
         rozygag@yahoo.com

Weitere ähnliche Inhalte

Was ist angesagt?

Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sqlÑirmal Tatiwal
 
PL/SQL Code for Sample Projects
PL/SQL Code for Sample ProjectsPL/SQL Code for Sample Projects
PL/SQL Code for Sample Projectsjwjablonski
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS ConceptBoopathi K
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQLrehaniltifat
 
PL SQL Quiz | PL SQL Examples
PL SQL Quiz |  PL SQL ExamplesPL SQL Quiz |  PL SQL Examples
PL SQL Quiz | PL SQL ExamplesShubham Dwivedi
 
PL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics CoveredPL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics CoveredDanish Mehraj
 
SQLcl overview - A new Command Line Interface for Oracle Database
SQLcl overview - A new Command Line Interface for Oracle DatabaseSQLcl overview - A new Command Line Interface for Oracle Database
SQLcl overview - A new Command Line Interface for Oracle DatabaseJeff Smith
 
Packages in PL/SQL
Packages in PL/SQLPackages in PL/SQL
Packages in PL/SQLPooja Dixit
 
Oracle sql & plsql
Oracle sql & plsqlOracle sql & plsql
Oracle sql & plsqlSid Xing
 
Enhance ERD(Entity Relationship Diagram)
Enhance ERD(Entity Relationship Diagram)Enhance ERD(Entity Relationship Diagram)
Enhance ERD(Entity Relationship Diagram)Imdad Ul Haq
 
Exception handling in plsql
Exception handling in plsqlException handling in plsql
Exception handling in plsqlArun Sial
 
18 checklists, actions, and workforce predictions
18   checklists, actions, and workforce predictions18   checklists, actions, and workforce predictions
18 checklists, actions, and workforce predictionsmohamed refaei
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Salman Memon
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals INick Buytaert
 

Was ist angesagt? (20)

Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
 
Chapter8 pl sql
Chapter8 pl sqlChapter8 pl sql
Chapter8 pl sql
 
Oops
OopsOops
Oops
 
PL/SQL Code for Sample Projects
PL/SQL Code for Sample ProjectsPL/SQL Code for Sample Projects
PL/SQL Code for Sample Projects
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
 
PL SQL Quiz | PL SQL Examples
PL SQL Quiz |  PL SQL ExamplesPL SQL Quiz |  PL SQL Examples
PL SQL Quiz | PL SQL Examples
 
PL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics CoveredPL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics Covered
 
Sql loader good example
Sql loader good exampleSql loader good example
Sql loader good example
 
SQLcl overview - A new Command Line Interface for Oracle Database
SQLcl overview - A new Command Line Interface for Oracle DatabaseSQLcl overview - A new Command Line Interface for Oracle Database
SQLcl overview - A new Command Line Interface for Oracle Database
 
Cursores.ppt
Cursores.pptCursores.ppt
Cursores.ppt
 
Packages in PL/SQL
Packages in PL/SQLPackages in PL/SQL
Packages in PL/SQL
 
Intro to c++
Intro to c++Intro to c++
Intro to c++
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
 
Oracle sql & plsql
Oracle sql & plsqlOracle sql & plsql
Oracle sql & plsql
 
Enhance ERD(Entity Relationship Diagram)
Enhance ERD(Entity Relationship Diagram)Enhance ERD(Entity Relationship Diagram)
Enhance ERD(Entity Relationship Diagram)
 
Exception handling in plsql
Exception handling in plsqlException handling in plsql
Exception handling in plsql
 
18 checklists, actions, and workforce predictions
18   checklists, actions, and workforce predictions18   checklists, actions, and workforce predictions
18 checklists, actions, and workforce predictions
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
 

Ähnlich wie Plsql overview

PLSQL Notes.pptx
PLSQL Notes.pptxPLSQL Notes.pptx
PLSQL Notes.pptxSHRISANJAY4
 
Unit 4 rdbms study_material
Unit 4  rdbms study_materialUnit 4  rdbms study_material
Unit 4 rdbms study_materialgayaramesh
 
Introduction to PLSQL.PPT
Introduction to PLSQL.PPTIntroduction to PLSQL.PPT
Introduction to PLSQL.PPTSujayaBiju
 
Answer and provide 1 reference. Advanced SQL and PLSQLE.docx
Answer and provide 1 reference. Advanced SQL and PLSQLE.docxAnswer and provide 1 reference. Advanced SQL and PLSQLE.docx
Answer and provide 1 reference. Advanced SQL and PLSQLE.docxSHIVA101531
 
SQL EXCLUSIVE NOTES .pdf
SQL EXCLUSIVE NOTES .pdfSQL EXCLUSIVE NOTES .pdf
SQL EXCLUSIVE NOTES .pdfNiravPanchal50
 
1. In a 200 words or less explain the differences between SQL and .docx
1. In a 200 words or less explain the differences between SQL and .docx1. In a 200 words or less explain the differences between SQL and .docx
1. In a 200 words or less explain the differences between SQL and .docxSONU61709
 
Sql server difference faqs- 6
Sql server difference faqs- 6Sql server difference faqs- 6
Sql server difference faqs- 6Umar Ali
 
Presenter manual oracle D2K (specially for summer interns)
Presenter manual oracle D2K (specially for summer interns)Presenter manual oracle D2K (specially for summer interns)
Presenter manual oracle D2K (specially for summer interns)XPERT INFOTECH
 
U-SQL Intro (SQLBits 2016)
U-SQL Intro (SQLBits 2016)U-SQL Intro (SQLBits 2016)
U-SQL Intro (SQLBits 2016)Michael Rys
 

Ähnlich wie Plsql overview (20)

PL-SQL, Cursors & Triggers
PL-SQL, Cursors & TriggersPL-SQL, Cursors & Triggers
PL-SQL, Cursors & Triggers
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
PL/SQL
PL/SQLPL/SQL
PL/SQL
 
PLSQL Notes.pptx
PLSQL Notes.pptxPLSQL Notes.pptx
PLSQL Notes.pptx
 
Unit 4 rdbms study_material
Unit 4  rdbms study_materialUnit 4  rdbms study_material
Unit 4 rdbms study_material
 
Unit4_DBMS.pptx
Unit4_DBMS.pptxUnit4_DBMS.pptx
Unit4_DBMS.pptx
 
Bn 1018 demo pl sql
Bn 1018 demo  pl sqlBn 1018 demo  pl sql
Bn 1018 demo pl sql
 
Pl sql
Pl sqlPl sql
Pl sql
 
Introduction to PLSQL.PPT
Introduction to PLSQL.PPTIntroduction to PLSQL.PPT
Introduction to PLSQL.PPT
 
Answer and provide 1 reference. Advanced SQL and PLSQLE.docx
Answer and provide 1 reference. Advanced SQL and PLSQLE.docxAnswer and provide 1 reference. Advanced SQL and PLSQLE.docx
Answer and provide 1 reference. Advanced SQL and PLSQLE.docx
 
Jooq java object oriented querying
Jooq java object oriented queryingJooq java object oriented querying
Jooq java object oriented querying
 
SQL EXCLUSIVE NOTES .pdf
SQL EXCLUSIVE NOTES .pdfSQL EXCLUSIVE NOTES .pdf
SQL EXCLUSIVE NOTES .pdf
 
1. In a 200 words or less explain the differences between SQL and .docx
1. In a 200 words or less explain the differences between SQL and .docx1. In a 200 words or less explain the differences between SQL and .docx
1. In a 200 words or less explain the differences between SQL and .docx
 
Sql server difference faqs- 6
Sql server difference faqs- 6Sql server difference faqs- 6
Sql server difference faqs- 6
 
A42525
A42525A42525
A42525
 
Oracle PLSQL Training in Chennai, Tambaram
Oracle PLSQL Training in Chennai, TambaramOracle PLSQL Training in Chennai, Tambaram
Oracle PLSQL Training in Chennai, Tambaram
 
Presenter manual oracle D2K (specially for summer interns)
Presenter manual oracle D2K (specially for summer interns)Presenter manual oracle D2K (specially for summer interns)
Presenter manual oracle D2K (specially for summer interns)
 
Pl sql chapter 1
Pl sql chapter 1Pl sql chapter 1
Pl sql chapter 1
 
vikram ch resume
vikram ch resumevikram ch resume
vikram ch resume
 
U-SQL Intro (SQLBits 2016)
U-SQL Intro (SQLBits 2016)U-SQL Intro (SQLBits 2016)
U-SQL Intro (SQLBits 2016)
 

Mehr von Gagan Deep

Fundamentals of Neural Networks
Fundamentals of Neural NetworksFundamentals of Neural Networks
Fundamentals of Neural NetworksGagan Deep
 
Artificial Intelligence
Artificial IntelligenceArtificial Intelligence
Artificial IntelligenceGagan Deep
 
Software Project Planning V
Software Project Planning VSoftware Project Planning V
Software Project Planning VGagan Deep
 
Software Project Planning IV
Software Project Planning IVSoftware Project Planning IV
Software Project Planning IVGagan Deep
 
Software Project Planning III
Software Project Planning IIISoftware Project Planning III
Software Project Planning IIIGagan Deep
 
Software Project Planning II
Software Project Planning IISoftware Project Planning II
Software Project Planning IIGagan Deep
 
Software Project Planning 1
Software Project Planning 1Software Project Planning 1
Software Project Planning 1Gagan Deep
 
Software Engineering
Software Engineering Software Engineering
Software Engineering Gagan Deep
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : ArraysGagan Deep
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareGagan Deep
 
C lecture 3 control statements slideshare
C lecture 3 control statements slideshareC lecture 3 control statements slideshare
C lecture 3 control statements slideshareGagan Deep
 
C – A Programming Language- I
C – A Programming Language- IC – A Programming Language- I
C – A Programming Language- IGagan Deep
 
System Analysis & Design - 2
System Analysis & Design - 2System Analysis & Design - 2
System Analysis & Design - 2Gagan Deep
 
System Analysis & Design - I
System Analysis & Design - ISystem Analysis & Design - I
System Analysis & Design - IGagan Deep
 
Information System and MIS
Information System and MISInformation System and MIS
Information System and MISGagan Deep
 
SQL – A Tutorial I
SQL – A Tutorial  ISQL – A Tutorial  I
SQL – A Tutorial IGagan Deep
 
Boolean algebra
Boolean algebraBoolean algebra
Boolean algebraGagan Deep
 
Normalization 1
Normalization 1Normalization 1
Normalization 1Gagan Deep
 
Normalization i i
Normalization   i iNormalization   i i
Normalization i iGagan Deep
 

Mehr von Gagan Deep (20)

Number system
Number systemNumber system
Number system
 
Fundamentals of Neural Networks
Fundamentals of Neural NetworksFundamentals of Neural Networks
Fundamentals of Neural Networks
 
Artificial Intelligence
Artificial IntelligenceArtificial Intelligence
Artificial Intelligence
 
Software Project Planning V
Software Project Planning VSoftware Project Planning V
Software Project Planning V
 
Software Project Planning IV
Software Project Planning IVSoftware Project Planning IV
Software Project Planning IV
 
Software Project Planning III
Software Project Planning IIISoftware Project Planning III
Software Project Planning III
 
Software Project Planning II
Software Project Planning IISoftware Project Planning II
Software Project Planning II
 
Software Project Planning 1
Software Project Planning 1Software Project Planning 1
Software Project Planning 1
 
Software Engineering
Software Engineering Software Engineering
Software Engineering
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
 
C lecture 3 control statements slideshare
C lecture 3 control statements slideshareC lecture 3 control statements slideshare
C lecture 3 control statements slideshare
 
C – A Programming Language- I
C – A Programming Language- IC – A Programming Language- I
C – A Programming Language- I
 
System Analysis & Design - 2
System Analysis & Design - 2System Analysis & Design - 2
System Analysis & Design - 2
 
System Analysis & Design - I
System Analysis & Design - ISystem Analysis & Design - I
System Analysis & Design - I
 
Information System and MIS
Information System and MISInformation System and MIS
Information System and MIS
 
SQL – A Tutorial I
SQL – A Tutorial  ISQL – A Tutorial  I
SQL – A Tutorial I
 
Boolean algebra
Boolean algebraBoolean algebra
Boolean algebra
 
Normalization 1
Normalization 1Normalization 1
Normalization 1
 
Normalization i i
Normalization   i iNormalization   i i
Normalization i i
 

Plsql overview

  • 1. PL/SQL - Overview Gagan Deep Assistant Professor University College, Kurukshetra University, Kurukshetra
  • 2. What is PL/SQL ? • Procedural Language – SQL • An extension to SQL with design features of programming languages (procedural and object oriented) • PL/SQL and Java are both supported as internal host languages within Oracle products. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 3. PL/SQL The PL/SQL procedural language was developed by Oracle Corporation in the late 1980s as procedural extension language for SQL and the Oracle relational database. Following are notable facts about PL/SQL: •PL/SQL is a completely portable, high-performance transaction-processing language. •PL/SQL provides a built-in interpreted and OS independent programming environment. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 4. • PL/SQL can also directly be called from the command- line SQL*Plus interface. • Direct call can also be made from external programming language calls to database. • PL/SQL's general syntax is based on that of ADA and Pascal programming language. • Apart from Oracle, PL/SQL is available in TimesTen in-memory database and IBM DB2. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 5. Why PL/SQL • Acts as host language for stored procedures and triggers. • Provides the ability to add middle tier business logic to client/server applications. • Provides Portability of code from one environment to another • Improves performance of multi-query transactions. • Provides error handling Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 6. Features of PL/SQL PL/SQL has the following features: • PL/SQL is tightly integrated with SQL. • It offers extensive error checking. • It offers numerous data types. • It offers a variety of programming structures. • It supports structured programming through functions and procedures. • It supports object oriented programming. • It supports developing web applications and server pages. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 7. Advantages of PL/SQL PL/SQL has the following advantages : •SQL is the standard database language and PL/SQL is strongly integrated with SQL. PL/SQL supports both static and dynamic SQL. • Static SQL supports DML operations and transaction control from PL/SQL block. • Dynamic SQL is SQL allows embedding DDL statements in PL/SQL blocks. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 8. • PL/SQL allows sending an entire block of statements to the database at one time. This reduces network traffic and provides high performance for the applications. • PL/SQL give high productivity to programmers as it can query, transform, and update data in a database. • PL/SQL saves time on design and debugging by strong features, such as exception handling, encapsulation, data hiding, and object-oriented. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 9. • Applications written in PL/SQL are fully portable. • PL/SQL provides high security level. • PL/SQL provides access to predefined SQL packages. • PL/SQL provides support for Object-Oriented Programming. • PL/SQL provides support for Developing Web Applications and Server Pages. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 10. Client-Server Architecture • PL/SQL is a part of the Oracle RDBMS, and it can reside in two environments, the client and the server. • Many Oracle applications are built using client-server architecture. The Oracle database resides on the server. • The program(` C, Java, or PL/SQL) that makes requests against this database resides on the client machine. • As a result, it is very easy to move PL/SQL modules between server-side and client-side applications. • When the PL/SQL engine is located on the server, the whole PL/SQL block is passed to the PL/SQL engine on the Oracle server. The PL/SQL engine processes the block according to the Figure 1.
  • 11. Figure 1 : The PL/SQL Engine and Oracle Server
  • 12. • When the PL/SQL engine is located on the client, as it is in the Oracle Developer Tools, the PL/SQL processing is done on the client side. • All SQL statements that are embedded within the PL/SQL block are sent to the Oracle server for further processing. When PL/SQL block contains no SQL statement, the entire block is executed on the client side.
  • 13. Comparison of PL/SQL and SQL • When a SQL statement is issued on the client computer, the request is made to the database on the server, and the result set is sent back to the client. • As a result, a single SQL statement causes two trips on the network. If multiple SELECT statements are issued, the network traffic increase significantly very fast. For example, four SELECT statements cause eight network trips. • If these statements are part of the PL/SQL block, they are sent to the server as a single unit. The SQL statements in this PL/SQL program are executed at the server and the result set is sent back as a single unit. There is still only one network trip made as is in case of a single SELECT statement.
  • 14. Comparison of PL/SQL and SQL Figure 2 : The PL/SQL in client – server architecture
  • 15. PL/SQL Block-Structure PL/SQL is a block-structured language, meaning that PL/SQL programs are divided and written in logical blocks of code. Each block consists of three sub-parts: 1 Declarations : This section starts with the keyword DECLARE. It is an optional section and defines all variables, cursors, subprograms, and other elements to be used in the program. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 16. 2 Executable Commands This section is enclosed between the keywords BEGIN and END and it is a mandatory section. It consists of the executable PL/SQL statements of the program. It should have at least one executable line of code, which may be just a NULL command to indicate that nothing should be executed. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 17. 3 Exception Handling This section starts with the keyword EXCEPTION. This section is again optional and contains exception(s) that handle errors in the program. Every PL/SQL statement end with a semicolon (;). PL/SQL blocks can be nested within other PL/SQL blocks using BEGIN and END. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 18. PL/SQL Block Structure DECLARE (optional) - variable declarations BEGIN (mandatory) - SQL statements - PL/SQL statements or sub- blocks EXCEPTION (optional) - actions to perform when errors occur END; (mandatory) Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 19. PL/SQL Block Types Function Anonymous Procedure FUNCTION <name> RETURN <datatype> DECLARE PROCEDURE <name> IS BEGIN IS BEGIN -statements BEGIN -statements EXCEPTION -statements EXCEPTION END; EXCEPTION END; END; Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 20. Example The end; line signals the end of the PL/SQL block. DECLARE message varchar2(20):= 'Hello, World!'; To run the code BEGIN from SQL command dbms_output.put_line(message); line, you may need END; to type / at the / beginning of the Hello, World ! first blank line after PL/SQL procedure successfully the last line of the completed. code. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 21. 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 integer or INTEGER to represent a numeric value. You cannot use a reserved keyword as an identifier. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 22. 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 Similarly, there are : Host variable indicator more delimiters. , Item separator Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 23. The PL/SQL Comments • Program comments are explanatory statements that you can include in the PL/SQL code that you write and helps anyone reading it's source code. All programming languages allow for some form of comments. • The PL/SQL supports single line and multi-line comments. All characters available inside any comment are ignored by PL/SQL compiler. The PL/SQL single-line comments start with the delimiter -- (double hyphen) and multi-line comments are enclosed by /* and */. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 24. DECLARE -- variable declaration message varchar2(20):= 'Hello, World!’; BEGIN /* PL/SQL executable statement(s) Example */ dbms_output.put_line(message); END; / After Execution in SQL the result is : Hello, World! PL/SQL procedure successfully completed. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 25. Data Types PL/SQL variables, constants and parameters must have a valid data types which specifies a storage format, constraints, and valid range of values. PL/SQL is strongly typed. The following is the list of data types: Category Description Single values with no internal components, Scalar such as a NUMBER, DATE, or BOOLEAN. Pointers to large objects that are stored separately from other data items, such as Large Object (LOB) text, graphic images, video clips, and sound waveforms. Data items that have internal components Composite that can be accessed individually. For example, collections and records. Reference Pointers to other data items. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 26. Scalar Data Types • PL/SQL Scalar Data Types and Subtypes come under the following categories: Date Type Description Numeric values, on which arithmetic operations Numeric are performed. Alphanumeric values that represent single Character characters or strings of characters. Logical values, on which logical operations are Boolean performed. Datetime Dates and times. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 27. PL/SQL Numeric Data Types Data Type Description ANSI and IBM specific floating-point type FLOAT with maximum precision of 126 binary digits (approximately 38 decimal digits) ANSI and IBM specific integer type with INTEGER OR INT maximum precision of 38 decimal digits Floating-point type with maximum precision REAL of 63 binary digits (approximately 18 decimal digits) Signed integer in range -2,147,483,648 PLS_INTEGER OR BINARY_INTEGER through 2,147,483,647, represented in 32 bits ANSI specific fixed-point type with DEC(prec, scale) maximum precision of 38 decimal digits. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 28. Following is a valid declaration: DECLARE num1 INTEGER; num2 REAL; num3 DOUBLE PRECISION; BEGIN Example null; END; / When the above code is compiled and executed, it produces following result: PL/SQL procedure successfully completed Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 29. Character Data Types Data Type Description CHAR Fixed-length character string with maximum size of 32,767 bytes VARCHAR Variable-length character string with maximum size of 32,767 2 bytes Variable-length binary or byte string with maximum size of RAW 32,767 bytes, not interpreted by PL/SQL Variable-length character string with maximum size of 32,760 LONG bytes ROWID Physical row identifier, the address of a row in an ordinary table Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 30. PL/SQL Operators An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. PL/SQL language is rich in built-in operators and provides following type of operators: •Arithmetic operators •Relational operators •Comparison operators •Logical operators •String operators Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 31. Precedence of Operators Operator Operation ** exponentiation +, - identity, negation *, / multiplication, division +, -, || addition, subtraction, concat =, <, >, <=, >=, IS NULL, LIKE, IN Comparison NOT logical negation AND conjunction OR inclusion Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 32. Control Structures • Programming languages provide various control structures that allow for more complicated execution paths. • Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 33. PL/SQL Decisions Making PL/SQL language provides following types of decision making statements. •IF-THEN Statement •IF-THEN-ELSE Statement •IF-THEN-ELSEIF Statement •Nested IF Statement Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 34. Syntax for IF-THEN statement IF condition THEN S; END IF; Where condition is a Boolean or relational condition and S is a simple or compound statement. Example of an IF-THEN statement is: IF (a <= 20) THEN c:= c+1; END IF; Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 35. DECLARE Example a number(2) := 10; BEGIN a:= 10; IF( a < 20 ) THEN dbms_output.put_line('a is less than 20' ); END IF; dbms_output.put_line('value of a is:' || a); END; / After Execution : a is less than 20 value of a is : 10 PL/SQL procedure successfully completed. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 36. PL/SQL - Loops PL/SQL provides the following types of loop to handle the looping requirements •BASIC LOOP •WHILE LOOP •FOR LOOP •NESTED LOOP Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 37. Syntax of Basic Loop The syntax of a basic loop in PL/SQL language is: LOOP Sequence of statements; END LOOP Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 38. Example DECLARE x number := 10; BEGIN LOOP dbms_output.put_line(x); x := x + 10; IF x > 50 THEN exit; END IF; END LOOP; dbms_output.put_line('After Exit x is: ' || x); END; / After Execution is : 10 20 30 40 50 After Exit x is: 60 Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 39. Syntax of For Loop FOR counter IN i_value .. f_value LOOP sequence_of_statements; END LOOP; Following are special characteristics of FOR loop: The i_value and f_value of the loop variable or counter can be literals, variables, or expressions but must evaluate to numbers. Otherwise, PL/SQL raises the predefined exception VALUE_ERROR. The i_value need not to be 1; however, the loop counter increment (or decrement) must be 1. PL/SQL allows determine the loop range dynamically at runGagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 Gate, K.U., Kurukshetra time. rd
  • 40. Example DECLARE a number(2); BEGIN Result is FOR a in 10 .. 15 LOOP value of a: 10 dbms_output.put_line('value of a: ' || value of a: 11 a); value of a: 12 END LOOP; value of a: 13 END; value of a: 14 / value of a: 15 Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 41. Common PL/SQL String Functions • CHR(ASCIIvalue) • ASCII(string) • LOWER(string) • SUBSTR(string,start,substrlength) • LTRIM(string) • RTRIM(string) • REPLACE(string, searchstring, replacestring) • UPPER(string) Gagan Deep, Assistant Professor, UCK and • INITCAP(string) Director, Rozy Computech Services, 3 • LENGTH(string) rd Gate, K.U., Kurukshetra
  • 42. Common PL/SQL Numeric Functions • ABS(value) • ROUND(value, precision) • MOD(value,divisor) • SQRT(value) • TRUNC(value,|precision|) • LEAST(exp1, exp2…) • GREATEST(exp1, exp2… Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 43. When is PL/SQL handy • When something is too complicated for SQL • When conditional branching and looping are needed. Gagan Deep, Assistant Professor, UCK and Director, Rozy Computech Services, 3 rd Gate, K.U., Kurukshetra
  • 44. Thanks ! If you have any query please mail me at rozygag@yahoo.com