This document discusses PL/SQL and some of its key features. PL/SQL is a programming language designed for SQL and for working with Oracle databases. It allows embedding SQL statements in code and runs within the Oracle database process. Packages are used to organize related PL/SQL objects and provide public and private interfaces. Collections allow storing multiple values in a PL/SQL block and include index-by tables, nested tables, and varrays. The document provides examples of using packages and index-by tables.
1. Plsql
• PL/SQL is a procedural language designed specifically to embrace
SQL statements within its syntax. PL/SQL program units are compiled
by the Oracle Database server and stored inside the database. And at
run-time, both PL/SQL and SQL run within the same server process,
bringing optimal efficiency.
What is the use of Plsql?
• PL/SQL lets you use all SQL data manipulation, cursor control, and
transaction control statements, and all SQL functions, operators, and
pseudo columns. PL/SQL fully supports SQL data types. You need not
convert between PL/SQL and SQL data types.
2. PACKAGES
• This chapter, we will discuss the Packages in PL/SQL. Packages are schema
objects that groups logically related PL/SQL types, variables, and subprograms.
• A package will have two mandatory parts −
• Package specification
• Package body or definition
• Package Specification
• The specification is the interface to the package. It just DECLARES the types,
variables, constants, exceptions, cursors, and subprograms that can be referenced
from outside the package. In other words, it contains all information about the
content of the package, but excludes the code for the subprograms.
• All objects placed in the specification are called public objects. Any subprogram
not in the package specification but coded in the package body is called
a private object.
• The following code snippet shows a package specification having a single
procedure. You can have many global variables defined and multiple procedures
or functions inside a package.
3. Syntax of Packages
CREATE PACKAGE cust_sal AS PROCEDURE
find_sal(c_id customers.id%type);
END cut_sal;
/
When the above code is executed at the SQL prompt, it produces the
following result −
Package created.
4. Packages Created
CREATE OR REPLACE PACKAGE BODY cust_sal AS
PROCEDURE find_sal(c_id customers.id%TYPE) IS
c_sal customers.salary%TYPE;
BEGIN
SELECT salary INTO c_sal FROM customers WHERE id
= c_id;
dbms_output.put_line('Salary: '|| c_sal);
END
find_sal; END cust_sal; /
Package body created.
5. COLLECTION
• A collection is an ordered group of elements having the same data
type. Each element is identified by a unique subscript that represents
its position in the collection.
• PL/SQL provides three collection types −
• Index-by tables or Associative array
• Nested table
• Variable-size array or Varray
6. Collection Type Number of Elements Subscript Type Dense or Sparse Where Created Can Be Object Type Attribute
Associative array (or index-
by table)
Unbounded String or integer Either Only in PL/SQL block No
Nested table Unbounded Integer
Starts dense, can become
sparse
Either in PL/SQL block or at
schema level
Yes
Variablesize array (Varray) Bounded Integer Always dense
Either in PL/SQL block or at
schema level
Yes
7. Index-By Table
• An index-by table (also called an associative array) is a
set of key-value pairs. Each key is unique and is used to
locate the corresponding value. The key can be either an
integer or a string.
• An index-by table is created using the following syntax.
Here, we are creating an index-by table
named table_name, the keys of which will be of the
subscript_type and associated values will be of
the element_type
8. Example
Following example shows how to create a table to store integer values along with names
and later it prints the same list of names
.
DECLARE
TYPE salary IS TABLE OF NUMBER INDEX BY VARCHAR2(20);
salary_list salary;
name VARCHAR2(20);
BEGIN
-- adding elements to the table salary_list('Rajnish') := 62000;
salary_list('Minakshi') := 75000; salary_list('Martin') := 100000;
salary_list('James') := 78000; -- printing the table name := salary_list.FIRST;
WHILE name IS NOT null LOOP dbms_output.put_line ('Salary of ' || name || ' is ' ||
TO_CHAR(salary_list(name)));
name := salary_list.NEXT(name);
END LOOP;
END;
/