Вы находитесь на странице: 1из 38

21.

 1. 1. Block Structure
You typically use PL/SQL to add business logic to the database.
PL/SQL programs are divided up into structures known as blocks.
Each block containing PL/SQL and SQL statements.
A typical PL/SQL block has the following structure:
[DECLARE
  declaration_statements
]
BEGIN
  executable_statements
[EXCEPTION
  exception_handling_statements
]
END;

1. The declaration and exception blocks are optional.


2. declaration_statements declares the variables subsequently used in the rest of the block.
3. These variables are local to that block.
4. Declarations are always placed at the start of the block.
5. executable_statements are the actual executable statements for the block.
6. executable_statements may include statements for performing tasks such as loops, conditional
so on.
7. exception_handling_statements are statements that handle any errors.
8. Every statement is terminated by a semicolon (;).
9. A block is terminated using the END keyword.

DECLARE        
  2    width INTEGER;
  3    height INTEGER := 2;
  4    area INTEGER;
  5  BEGIN
  6    area := 6;
  7    width := area / height;
  8    DBMS_OUTPUT.PUT_LINE('width = ' || width);
  9  EXCEPTION
 10    WHEN ZERO_DIVIDE THEN
 11      DBMS_OUTPUT.PUT_LINE('Division by zero');
 12  END;
 13  /
width = 3

PL/SQL procedure successfully completed.

SQL>

Your First PL/SQL Block


SQL>
SQL> DECLARE
  2    x     NUMBER;
  3  BEGIN
  4    x := 72600;
  5  END;
  6  /

PL/SQL procedure successfully completed.

The slash character (/) at the end of the example executes the PL/SQL.

SQL>
SQL> SET ECHO ON
SQL> SET SERVEROUTPUT ON
SQL> DECLARE
  2    age   NATURAL;
  3
  4  BEGIN
  5    age := 10;
  6    DBMS_OUTPUT.PUT_LINE('age:');
  7    DBMS_OUTPUT.PUT_LINE(age);
  8  END;
  9  /
age:
10

PL/SQL procedure successfully completed.

SQL>

You can declare the whole string to be enclosed in quotes by using t


construct q'!text!'

SQL>
SQL> declare
  2      
  3      
  4  begin
  5      NULL;
  6  End;
  7  /

PL/SQL procedure successfully completed.

Text literals in Oracle are case sensitive.
All identifiers within the same scope must be unique.

SQL>
SQL> declare
  2      v_amount NUMBER;
  3      v_amount BINARY_INTEGER; -- duplicate!!!
  4  Begin
  5     NULL;
  6  end;
  7  /

PL/SQL procedure successfully completed.

SQL>
SQL>

Building Expressions with Operators

Expressions are constructed by using operands and operators.


An Example of a Simple PL/SQL Expression
SQL>
SQL> declare
  2      v_i1 NUMBER;
  3      v_i2 NUMBER;
  4  begin
  5      v_i1:=10/3;
  6      v_i2:=-v_i1;
  7  end;
  8  /

PL/SQL procedure successfully completed.

SQL>

PL/SQL datatypes
Oracle PL/SQL supports all SQL types plus the following additional Oracle PL/SQL specific types
Type Description
Fixed-length character data of length bytes or characters
CHAR[(length [BYTE | CHAR])] and padded with trailing spaces. Maximum length is 2,000
bytes.
VARCHAR2(length [BYTE | Variable-length character data of up to length bytes or
CHAR]) characters. Maximum length is 4,000 bytes.
NCHAR[(length)] Fixed-length Unicode character data of length characters.
Number of bytes stored is 2 * length for AL16UTF16
encoding and 3 * length for UTF8. Maximum length is 2,000
bytes.
Variable-length Unicode character data of length characters.
Number of bytes stored is 2 * length for AL16UTF16
NVARCHAR2(length)
encoding and 3 * length for UTF8 encoding. Maximum
length is 4,000 bytes.
Stores a single precision 32-bit floating-point number.
Operations involving BINARY_FLOAT are typically
BINARY_FLOAT
performed faster than on NUMBERs. BINARY_FLOAT
requires 5 bytes of storage space.
Stores a double precision 64-bit floating-point number.
Operations involving BINARY_DOUBLE are typically
BINARY_DOUBLE
performed faster than on NUMBERs. BINARY_DOUBLE
requires 9 bytes of storage space.
Variable-length number; precision is the maximum number
of digits (in front of and behind a decimal point, if used) that
may be used for the number. The maximum precision
supported is 38; scale is the maximum number of digits to
NUMBER(precision, scale) and
the right of a decimal point (if used). If neither precision nor
NUMERIC(precision, scale)
scale is specified, then a number with up to a precision and
scale of 38 digits may be supplied (meaning you can supply
a number with up to 38 digits, and any of those 38 digits
may be in front of or behind the decimal point).
Subtype of NUMBER. A fixed-point decimal number with up
DEC and DECIMAL
to 38 digits of decimal precision.
Subtype of NUMBER. A floating-point number with up to 38
DOUBLE PRECISION and FLOAT
digits of precision.
Subtype of NUMBER. A floating-point number with up to 18
REAL
digits of precision.
Subtype of NUMBER. An integer with up to 38 digits of
INT, INTEGER, and SMALLINT
decimal precision.
Date and time with the century, all four digits of year, month,
day, hour (in 24-hour format), minute, and second. May be
used to store a date and time between January 1, 4712 B.C.
DATE
and December 31, 4712 A.D. Default format is specified by
the NLS_DATE_FORMAT parameter (for example: DD-
MON-RR).
Time interval measured in years and months;
INTERVAL
years_precision specifies the precision for the years, which
YEAR[(years_precision)] TO
may be an integer from 0 to 9 (default is 2). Can be used to
MONTH
represent a positive or negative time interval.
INTERVAL DAY[(days_precision)] Time interval measured in days and seconds;
TO days_precision specifies the precision for the days, which is
SECOND[(seconds_precision)] an integer from 0 to 9 (default is 2); seconds_precision
specifies the precision for the fractional part of the seconds,
which is an integer from 0 to 9 (default is 6). Can be used to
represent a positive or negative time interval.
Date and time with the century, all four digits of year, month,
day, hour (in 24-hour format), minute, and second;
TIMESTAMP[(seconds_precision) seconds_precision specifies the number of digits for the
] fractional part of the seconds, which can be an integer from
0 to 9 (default is 6). Default format is specified by the
NLS_TIMESTAMP_FORMAT parameter.
Extends TIMESTAMP to store a time zone. The time zone
TIMESTAMP[(seconds_precision) can be an offset from UTC, such as ??-5:0', or a region
] WITH TIME ZONE name, such as ??US/Pacific'. Default format is specified by
the NLS_TIMESTAMP_TZ_FORMAT parameter.
Extends TIMESTAMP to convert a supplied datetime to the
local time zone set for the database. The process of
TIMESTAMP[(seconds_precision)
conversion is known as normalizing the datetime. Default
] WITH LOCAL TIME ZONE
format is specified by the NLS_TIMESTAMP_FORMAT
parameter.
Variable length single-byte character data of up to 128
CLOB
terabytes.
Variable length Unicode national character set data of up to
NCLOB
128 terabytes.
BLOB Variable length binary data of up to 128 terabytes.
BFILE Pointer to an external file.
Variable length character data of up to 2 gigabytes.
LONG Superceded by CLOB and NCLOB types, but supported for
backwards compatibility.
Variable length binary data of up to length bytes. Maximum
RAW(length) length is 2,000 bytes. Superceded by BLOB type, but
supported for backwards compatibility.
Variable length binary data of up to 2 gigabytes. Superceded
LONG RAW
by BLOB type but supported for backwards compatibility.
ROWID Hexadecimal string used to represent a row address.
Hexadecimal string representing the logical address of a row
UROWID[(length)] of an index-organized table; length specifies the number of
bytes. Maximum length is 4,000 bytes (also default).
REF object_type Reference to an object type.
Variable length array. This is a composite type and stores an
VARRAY
ordered set of elements.
Nested table. This is a composite type and stores an
NESTED TABLE
unordered set of elements.
XMLType Stores XML data.
You can define your own object type and create objects of
User defined object type
that type.
PL/SQL only data type PL/SQL only data type
BOOLEAN Boolean value (TRUE, FALSE, or NULL).
Integer between C231 (C2,147,483,648) and 231
BINARY_INTEGER
(2,147,483,648).
NATURAL Subtype of BINARY_INTEGER. A non-negative integer.
Subtype of BINARY_INTEGER. A non-negative integer (and
NATURALN
cannot be NULL).
POSITIVE Subtype of BINARY_INTEGER. A positive integer.
Subtype of BINARY_INTEGER. A positive integer (and
POSITIVEN
cannot be NULL).
SIGNTYPE Subtype of BINARY_INTEGER. An integer of C1, 0, or 1.
Integer between C231 (C2,147,483,648) and 231
PLS_INTEGER (2,147,483,648). Similar to BINARY_INTEGER, but
computations involving PLS_INTEGER values are faster.
STRING Same as VARCHAR2.
Composite of a group of other types. Similar to a structure in
RECORD
C.
REF CURSOR Pointer to a set of rows.
These datatypes can be used for creating simple scalar variables.
They can be combined into structures such as records or PL/SQL tables.
A scalar variable is a variable that is not made up of some combination of other variables.
Scalar variables don't have internal components that you can manipulate individually.
They are often used to build up more complex datatypes such as records and arrays.
Some of the datatype names match those used by Oracle for defining database columns.
In most cases the definitions are the same for both the database and PL/SQL, but there are a few diffe
PL/SQL also provides subtypes of some datatypes.
A subtype represents a special case of a datatype.
A subtype represents a narrower range of values than the parent type.
For example, POSITIVE is a subtype of BINARY_INTEGER that holds only positive values.

Variable Naming Rules


Variable names can be composed of letters, dollar signs, underscores, and number signs.
No other characters can be used.
A variable name must start with a letter, after which any combination of the allowed characters can be
The maximum length for a variable name is 30 characters.
Variable names, like those of keywords and other identifiers, are not case sensitive.

Introducing the Main Data type Groups


There are four main groups:
1. Scalar datatypes represent single values that can't be divided into parts.
2. Composite datatypes include internal components that can be manipulated independently.
3. %ROWTYPE is an example of the PL/SQL RECORD datatype.
4. References contain pointers to other program items.
5. Large objects store or point to large amounts of textual or binary information, such as images, m
books.

Scalar datatypes are divided into families:


1. Numeric datatypes.
2. Character datatypes.
3. Date/time datatypes.
4. Boolean datatypes.

Conditional Logic

You may use the IF, THEN, ELSE, ELSIF, and END IF keywords in PL/SQL for performing conditio
The following syntax illustrates the use of conditional logic:
IF condition1 THEN
  statements1
ELSIF condition2 THEN
  statements2
ELSE
  statements3
END IF;

where
1. condition1 and condition2 are Boolean expressions that evaluate to true or false.
2. statements1, statements2, and statements3 are PL/SQL statements.

This conditional logic flows as follows:


1. If condition1 is true, then statements1 is executed.
2. If condition1 is false but condition2 is true, then statements2 is executed.
3. If neither condition1 nor condition2 are true, then statements3 is executed.

SQL> CREATE OR REPLACE PROCEDURE cant_go_there
  2  AS
  3     l_salary NUMBER := 10000;
  4  BEGIN
  5     IF l_salary > 20000
  6     THEN
  7        dbms_output.put_line ('Executive');
  8     ELSE
  9        dbms_output.put_line ('Drone');
 10     END IF;
 11  END cant_go_there;
 12  /

Procedure created.

SQL>
SQL> SHOW ERRORS
No errors.
SQL>

SQL> CREATE OR REPLACE PROCEDURE cant_go_there
  2  AS
  3     l_name varchar2(100) := 'steven';
  4  BEGIN
  5     IF l_name = 'STEVEN'
  6     THEN
  7        dbms_output.put_line ('Impossible');
  8     ELSE
  9        dbms_output.put_line ('Guaranteed');
 10     END IF;
 11  END cant_go_there;
 12  /

Procedure created.

SQL>
SQL> SHOW ERRORS
No errors.
SQL>

Handling conditions

Conditions can be connected by using logical operations: AND, OR, and NOT.
The default order of evaluation is standard.
First any parentheses are resolved.
Then operators are executed on the same level in order of precedence: NOT (highest precedence), AN
OR (lowest precedence).
SQL> declare
  2      v_day NUMBER := TO_CHAR(TO_DATE('20060101','YYYYMMDD'),'D');
  3  begin
  4      if v_day in (1,7) or (v_day not in (1,7) and (v_day between 0 and 6 or v_day betw
  5      then
  6          DBMS_OUTPUT.put_line(v_day||': Off-peak');
  7      else
  8          DBMS_OUTPUT.put_line(v_day||': Peak');
  9      end if;
 10  end;
 11  /
1: Off-peak

PL/SQL procedure successfully completed.
SQL>

A Simple Condition Statement

SQL>
SQL> create or replace function f_isSunday (in_dt DATE)
  2  return VARCHAR2
  3  is
  4      v_out VARCHAR2(10);
  5  begin
  6      if to_char(in_dt,'d')=1 then
  7          v_out:='Y';
  8           DBMS_OUTPUT.put_line('IsSunday=Y');
  9      end if;
 10      return v_out;
 11  end;
 12  /

Function created.

SQL>
SQL>

A Simple Condition Statement with BOOLEAN variable

SQL>
SQL> create or replace function f_isSunday (in_dt DATE)
  2  return VARCHAR2
  3  is
  4      v_out VARCHAR2(10);
  5      v_flag_b BOOLEAN;
  6  begin
  7      v_flag_b := to_char(in_dt,'d')=1;
  8      if v_flag_b then
  9          v_out:='Y';
 10          DBMS_OUTPUT.put_line('IsSunday=Y');
 11      end if;
 12      return v_out;
 13  end;
 14  /

Function created.

The IF...THEN...ELSE Statement

The IF...THEN...ELSE statement allows you to process a series of statements under ELSE if the cond
false.
The Syntax for the IF...THEN...ELSE Statement
IF  <some_condition_evaluates_to_true>
THEN
    <perform_statements_condition_true>
ELSE
    <perform_statements_condition_false>
END IF;

some_condition_evaluates_to_true, tests a BOOLEAN condition that you provide.


If true, the second parameter, perform_statements_condition_true, executes.
If the condition is false, the parameter perform_statements_condition_false executes.
SQL>
SQL> set echo on
SQL>
SQL> DECLARE
  2    v_a Number := 50 ;
  3    v_b Number;
  4  BEGIN
  5    IF v_a > 40 THEN
  6      v_b := v_a - 40;
  7         DBMS_OUTPUT.PUT_LINE('Hours b worked = ' || v_b);
  8       ELSE
  9            v_b := 0;
 10    END IF;
 11  END;
 12  /
Hours b worked = 10

PL/SQL procedure successfully completed.

SQL>

Use IF THEN ELSE IF

SQL>
SQL> SET SERVEROUTPUT ON
SQL> SET ECHO ON
SQL> DECLARE
  2    employee_name_c CHAR(32);
  3    employee_name_v VARCHAR2(32);
  4  BEGIN
  5    --Assign the same value to each string.
  6    employee_name_c := 'CHAR';
  7    employee_name_v := 'VARCHAR';
  8
  9    --Test the strings for equality.
 10    IF employee_name_c = employee_name_v THEN
 11      DBMS_OUTPUT.PUT_LINE('The names are the same');
 12    ELSE
 13      DBMS_OUTPUT.PUT_LINE('The names are NOT the same');
 14    END IF;
 15  END;
 16  /
The names are NOT the same

PL/SQL procedure successfully completed.

SQL>
SQL>

IF...ELSE statements

Using ELSE in a Condition Statement


IF <condition> then
   ...<<set of statements>>...
else
   ...<<set of statements>>...
end if;

SQL>
SQL> create or replace function f_isSunday (in_dt DATE)
  2  return VARCHAR2
  3  is
  4      v_out VARCHAR2(10);
  5      v_flag_b BOOLEAN;
  6  begin
  7      if to_char(in_dt,'d')=1 then
  8          v_out:='Y';
  9      else
 10          v_out:='N';
 11      end if;
 12      return v_out;
 13  end;
 14  /

Function created.

Using an ELSIF Statement

SQL>
SQL>
SQL> create or replace function f_getDateType (in_dt DATE)
  2  return VARCHAR2
  3  is
  4      v_out VARCHAR2(10);
  5  begin
  6      if to_char(in_dt,'MMDD') in ('0101','0704') then
  7          v_out:='HOLIDAY';
  8      elsif to_char(in_dt,'d') = 1 then
  9          v_out:='SUNDAY';
 10      elsif to_char(in_dt,'d') = 7 then
 11          v_out:='SATURDAY';
 12      else
 13          v_out:='WEEKDAY';
 14      end if;
 15      return v_out;
 16  end;
 17  /

Function created.

IF..ELSIF ladder

if <condition> then
   ...<<set of statements>>...
elsif <condition> then
   ...<<set of statements>>...
elsif <condition> then
   ...<<set of statements>>...
else
   ...<<set of statements>>...
end if;

SQL>
SQL> create or replace function f_isSunday (in_dt DATE)
  2  return VARCHAR2
  3  is
  4      v_out VARCHAR2(10);
  5  begin
  6      if to_char(in_dt,'d')=1 then
  7          v_out:='Y';
  8      else
  9          null;
 10      end if;
 11      return v_out;
 12  end;
 13  /

SP2-0806: Function created with compilation warnings

Block IF statement

SQL>
SQL> set serveroutput on
SQL> set echo on
SQL>
SQL> DECLARE
  2    v_A Number := 50 ;
  3    v_B Number := 0 ;
  4  BEGIN
  5    IF v_A > 40 THEN
  6      v_B := v_A - 40;
  7      DBMS_OUTPUT.PUT_LINE('V_B = ' || v_B);
  8    END IF;
  9  END;
 10  /
V_B = 10

PL/SQL procedure successfully completed.

SQL>

IF with ELSE

SQL>
SQL> set serveroutput on
SQL> set echo on
SQL> DECLARE
  2    v_A Number := 50;
  3    v_B Number;
  4  BEGIN
  5    IF v_A > 40 THEN
  6         v_B := v_A - 40;
  7         DBMS_OUTPUT.PUT_LINE('V_B = ' || v_B);
  8    ELSE
  9         v_B := 0;
 10    END IF;
 11  END;
 12  /
V_B = 10

PL/SQL procedure successfully completed.

The Syntax for IF...ELSIF

IF <condition1_evaluates_to_true>
  THEN
      <perform_statements>
  ELSIF <condition2_evaluates_to_true>
  THEN
       <perform_statements>
  ELSIF <condition3_evaluates_to_true>
  THEN
       <perform_statements>
  ELSE <this is always optional as the default value>
       <perform_statements>
  END IF;

ELSIF Ladder

SQL>
SQL> set serveroutput on
SQL> set echo on
SQL>
SQL> DECLARE
  2      v_Score Number := 85; --Percentage
  3      v_LetterGrade Char(1);
  4  BEGIN
  5      IF v_Score >= 90 THEN
  6           v_LetterGrade := 'A';
  7      ELSIF v_Score >= 80 THEN
  8           v_LetterGrade := 'B';
  9      ELSIF v_Score >= 70 THEN
 10           v_LetterGrade := 'C';
 11      ELSIF v_Score >= 60 THEN
 12           v_LetterGrade := 'D';
 13      ELSE
 14           v_LetterGrade := 'E';
 15      END IF;
 16      DBMS_OUTPUT.PUT_LINE('Your Letter Grade is: ' || v_LetterGrade);
 17  END;
 18  /
Your Letter Grade is: B

PL/SQL procedure successfully completed.

SQL>
SQL>

The Syntax for Nested IF Statements


IF <condition1 evaluates to true>
  THEN
       IF <condition2 evaluates to true>
       THEN
            <perform statements>
       ELSE <both conditions have been evaluated to false>
            IF <condition3 evaluates to true>
            THEN
                 <perform statements>
            ELSE
                 <perform statements>
            END IF;
       END IF;
  END IF;

Use if with 'IN'

SQL>
SQL> SET SERVEROUTPUT ON
SQL> DECLARE
  2    test_date     DATE;
  3    day_of_week   VARCHAR2(3);
  4    years_ahead   INTEGER;
  5  BEGIN
  6    test_date := TO_DATE('1-Jan-1997','dd-mon-yyyy');
  7
  8    FOR years_ahead IN 1..10 LOOP
  9      day_of_week := TO_CHAR(test_date,'Dy');
 10
 11      IF day_of_week IN ('Sat','Sun') THEN
 12        DBMS_OUTPUT.PUT_LINE(TO_CHAR(test_date,'dd-Mon-yyyy')|| '     A long weekend!')
 13      ELSE
 14        DBMS_OUTPUT.PUT_LINE(TO_CHAR(test_date,'dd-Mon-yyyy')|| ' Not a long weekend.')
 15      END IF;
 16      test_date := ADD_MONTHS(test_date,12);
 17    END LOOP;
 18  END;
 19  /
01-Jan-1997 Not a long weekend.
01-Jan-1998 Not a long weekend.
01-Jan-1999 Not a long weekend.
01-Jan-2000     A long weekend!
01-Jan-2001 Not a long weekend.
01-Jan-2002 Not a long weekend.
01-Jan-2003 Not a long weekend.
01-Jan-2004 Not a long weekend.
01-Jan-2005     A long weekend!
01-Jan-2006     A long weekend!

PL/SQL procedure successfully completed.

SQL>
SQL>

Three valued comparison


SQL>
SQL> SET ECHO ON
SQL> SET SERVEROUTPUT ON
SQL> DECLARE
  2    a     INTEGER;
  3    b     BOOLEAN;
  4    n     INTEGER;     --this will be our null value.
  5  BEGIN
  6    a := 2;
  7
  8    b := (a <> n);
  9    IF b THEN
 10      DBMS_OUTPUT.PUT_LINE('a <> n is TRUE');
 11    ELSIF NOT b THEN
 12      DBMS_OUTPUT.PUT_LINE('a <> n is FALSE');
 13    ELSE
 14      DBMS_OUTPUT.PUT_LINE('a <> n is NULL');
 15    END IF;
 16  END;
 17  /
a <> n is NULL

PL/SQL procedure successfully completed.

SQL>

JUMP out of a IF statement with goto


SQL>
SQL> set serveroutput on
SQL> set echo on
SQL>
SQL>    DECLARE
  2          v_Status NUMBER := 1;
  3     BEGIN
  4          IF v_Status = 1 THEN
  5               GOTO mybranch;
  6          ELSE
  7               v_Status := 1;
  8          END IF;
  9          <<mybranch>>
 10          NULL;
 11    END;
 12  /

PL/SQL procedure successfully completed.

SQL>

Comparing with NULL


SQL>
SQL>
SQL> declare
  2      v NUMBER;
  3  begin
  4      if v = 1 then
  5          DBMS_OUTPUT.put_line('Equal to 1');
  6      elsif v!= 1 then
  7          DBMS_OUTPUT.put_line('Not equal to 1');
  8      elsif v = v then
  9          DBMS_OUTPUT.put_line('Equal to itself');
 10      else
 11          DBMS_OUTPUT.put_line('Undefined result');
 12      end if;
 13      v:=v+1;
 14      DBMS_OUTPUT.put_line('New value: <'||v||'>');
 15  end;
 16  /
Undefined result
New value: <>

PL/SQL procedure successfully completed.
SQL>
SQL>

If block statement
SQL>
SQL>
SQL> create table employee (
  2  id                  number,
  3  employee_type_id    number,
  4  external_id         varchar2(30),
  5  first_name          varchar2(30),
  6  middle_name         varchar2(30),
  7  last_name           varchar2(30),
  8  name                varchar2(100),
  9  birth_date          date,
 10  gender_id           number);

Table created.

SQL>
SQL>
SQL>
SQL> create table gender (
  2  id                  number,
  3  code                varchar2(30),
  4  description         varchar2(80),
  5  active_date         date        default SYSDATE  not null,
  6  inactive_date       date );

Table created.

SQL>
SQL>
SQL>
SQL> insert into gender ( id, code, description ) values ( 1, 'F', 'Female' );

1 row created.

SQL> insert into gender ( id, code, description ) values ( 2, 'M', 'Male' );

1 row created.

SQL> insert into gender ( id, code, description ) values ( 3, 'U', 'Unknown' );

1 row created.

SQL>
SQL> create table employee_type (
  2  id                             number                         not null,
  3  code                           varchar2(30)                   not null,
  4  description                    varchar2(80)                   not null,
  5  active_date                    date          default SYSDATE  not null,
  6  inactive_date                  date );

Table created.

SQL>
SQL> insert into employee_type(id,code,description)values(1,'C','Contractor' );

1 row created.

SQL> insert into employee_type(id,code,description)values(2,'E','Employee' );

1 row created.

SQL> insert into employee_type(id,code,description)values(3,'U','Unknown' );

1 row created.

SQL>
SQL>
SQL>
SQL>
SQL> set serveroutput on size 1000000;
SQL>
SQL> declare
  2      n_id                employee.id%TYPE;
  3      n_employee_type_id  employee.employee_type_id%TYPE;
  4      v_external_id       employee.external_id%TYPE;
  5      v_first_name        employee.first_name%TYPE;
  6      v_middle_name       employee.middle_name%TYPE;
  7      v_last_name         employee.last_name%TYPE;
  8      v_name              employee.name%TYPE;
  9      d_birth_date        employee.birth_date%TYPE;
 10      n_gender_id         employee.gender_id%TYPE;
 11
 12      n_inserted          number := 0;
 13      n_updated           number := 0;
 14
 15  begin
 16    v_first_name  := 'JOHN';
 17    v_middle_name := 'J.';
 18    v_last_name   := 'DOE';
 19    v_name        := rtrim(v_last_name||', '||v_first_name||' '||v_middle_name);
 20    d_birth_date  := to_date('19800101', 'YYYYMMDD');
 21
 22    begin
 23      select id into n_employee_type_id from employee_type where  code = 'C';
 24    exception
 25      when OTHERS then
 26        raise_application_error(-20002, SQLERRM||' on select employee_type');
 27    end;
 28
 29    begin
 30      select id into n_gender_id from gender where code = 'M';
 31    exception
 32      when OTHERS then
 33        raise_application_error(-20004, SQLERRM||' on select gender');
 34    end;
 35
 36    begin
 37      select id into n_id from employee
 38      where  name       = v_name
 39      and    birth_date = d_birth_date
 40      and    gender_id  = n_gender_id;
 41    exception
 42      when NO_DATA_FOUND then
 43        n_id := NULL;
 44      when OTHERS then
 45        raise_application_error(-20003, SQLERRM||' on select employee_T');
 46    end;
 47
 48    if n_id is NULL then
 49      begin
 50        select 12 into n_id from SYS.DUAL;
 51      exception
 52        when OTHERS then
 53          raise_application_error(-20004, SQLERRM||' on select 12');
 54      end;
 55
 56      begin
 57        select lpad(to_char(12), 9, '0') into v_external_id from SYS.DUAL;
 58      exception
 59        when OTHERS then
 60          raise_application_error(-20005, SQLERRM||' on select 12');
 61      end;
 62
 63      begin
 64        insert into employee (
 65               id,
 66               employee_type_id,
 67               external_id,
 68               first_name,
 69               middle_name,
 70               last_name,
 71               name,
 72               birth_date,
 73               gender_id )
 74        values (
 75               n_id,
 76               n_employee_type_id,
 77               v_external_id,
 78               v_first_name,
 79               v_middle_name,
 80               v_last_name,
 81               v_name,
 82               d_birth_date,
 83               n_gender_id );
 84
 85        n_inserted := sql%rowcount;
 86      exception
 87        when OTHERS then
 88          raise_application_error(-20006, SQLERRM||' on insert employee');
 89      end;
 90    else
 91      begin
 92        update employee
 93        set    employee_type_id  = n_employee_type_id
 94        where  id              = n_id;
 95
 96        n_updated := sql%rowcount;
 97      exception
 98        when OTHERS then
 99          raise_application_error(-20007, SQLERRM||' on update employee');
100      end;
101    end if;
102
103    DBMS_OUTPUT.PUT_LINE(to_char(n_inserted)||' row(s) inserted.');
104    DBMS_OUTPUT.PUT_LINE(to_char(n_updated)||' row(s) updated.');
105  end;
106  /
1 row(s) inserted.
0 row(s) updated.

PL/SQL procedure successfully completed.

SQL>
SQL> drop table gender;

Table dropped.

SQL>
SQL> drop table employee;

Table dropped.

SQL>
SQL> drop table employee_type;

Table dropped.

SQL>

Create a function and call it in an if statement


SQL> CREATE TABLE emp (
  2    id         NUMBER PRIMARY KEY,
  3    fname VARCHAR2(50),
  4    lname  VARCHAR2(50)
  5  );

Table created.

SQL>
SQL> CREATE TABLE books (
  2    isbn      CHAR(10) PRIMARY KEY,
  3    category  VARCHAR2(20),
  4    title     VARCHAR2(100),
  5    num_pages NUMBER,
  6    price     NUMBER,
  7    copyright NUMBER(4),
  8    emp1   NUMBER,
  9    emp2   NUMBER,
 10    emp3   NUMBER
 11  );

Table created.

SQL>
SQL>
SQL> CREATE OR REPLACE FUNCTION Threeemp(p_ISBN IN books.isbn%TYPE)
  2    RETURN BOOLEAN AS
  3
  4    v_emp3 books.emp3%TYPE;
  5  BEGIN
  6    SELECT emp3 INTO v_emp3 FROM books WHERE isbn = p_ISBN;
  7
  8    IF v_emp3 IS NULL THEN
  9      RETURN FALSE;
 10    ELSE
 11      RETURN TRUE;
 12    END IF;
 13  END Threeemp;
 14  /

Function created.

SQL>
SQL> set serveroutput on
SQL>
SQL> BEGIN
  2    FOR v_Rec IN (SELECT ISBN, title FROM books) LOOP
  3      IF Threeemp(v_Rec.ISBN) THEN
  4        DBMS_OUTPUT.PUT_LINE('"' || v_Rec.title || '" has 3 emp');
  5      END IF;
  6    END LOOP;
  7  END;
  8  /

PL/SQL procedure successfully completed.

SQL> drop table books;

Table dropped.

SQL> drop table emp;

Table dropped.

PLW-06002: Unreachable code


SQL>
SQL> ALTER SESSION SET plsql_warnings = 'enable:all'
  2  /

Session altered.

SQL>
SQL> DROP FUNCTION plw6002;

Function dropped.

SQL>
SQL> CREATE OR REPLACE PROCEDURE plw6002
  2  AS
  3     l_checking BOOLEAN := FALSE;
  4  BEGIN
  5     NULL;
  6     IF l_checking
  7     THEN
  8        DBMS_OUTPUT.put_line ('Never here...');
  9     ELSE
 10        DBMS_OUTPUT.put_line ('Always here...');
 11        GOTO end_of_function;
 12     END IF;
 13     <<end_of_function>>
 14     NULL;
 15  END plw6002;
 16  /

SP2-0804: Procedure created with compilation warnings

SQL>
SQL> SHOW ERRORS PROCEDURE plw6002
Errors for PROCEDURE PLW6002:

LINE/COL ERROR
-------- -----------------------------------------------------------------
5/4      PLW-06002: Unreachable code
6/7      PLW-06002: Unreachable code
8/7      PLW-06002: Unreachable code
14/4     PLW-06002: Unreachable code
SQL>
SQL>

Operators
PL/SQL operators are either unary or binary.
Binary operators act on two values.
An example of binary operators is the addition operator, which adds two numbers together.
Unary operators only operate on one value.
The negation operator is unary.
PL/SQL operators can be divided into the following categories:
1. Arithmetic operators
2. Comparison operators
3. Logical operators
4. String operators

The basic arithmetic operators in action

SQL>
SQL> SET ECHO ON
SQL> SET SERVEROUTPUT ON
SQL>
SQL> BEGIN
  2    DBMS_OUTPUT.PUT_LINE(4 * 2);  --multiplication
  3    DBMS_OUTPUT.PUT_LINE(24 / 3); --division
  4    DBMS_OUTPUT.PUT_LINE(4 + 4);  --addition
  5    DBMS_OUTPUT.PUT_LINE(16 - 8); --subtraction
  6  END;
  7  /
8
8
8
8

PL/SQL procedure successfully completed.

SQL>
SQL>

Logical Operators in PL/SQL


x AND
x y x OR y NOT x
y
True True True True False
True False False True False
Fals
True False True True
e
Fals
False False False True
e
Running Anonymous Blocks of Code
SQL>
SQL>  set serveroutput on
SQL>  declare
  2       v_length NUMBER :=5.5;
  3       v_width  NUMBER :=3.5;
  4       v_area   NUMBER;
  5   begin
  6       v_area:=v_length*v_width;
  7       DBMS_OUTPUT.put_line('Area:'||v_area);
  8   end;
  9  /
Area:19.25

PL/SQL procedure successfully completed.

SQL>

Arithmetic Operators
Arithmetic operators are used for mathematical computations.
Operato
Example Usage
r
** 10**5 The exponentiation operator. 10**5 = 100,000.
* 2*3 The multiplication operator. 2 * 3 = 6.
/ 6/2 The division operator. 6/2 = 3.
+ 2+2 The addition operator. 2+2 = 4.
- 4-2 The subtraction operator. 4 -2 = 2.
- -5 The negation operator.
+ +5 It complements the negation operator.
The basic arithmetic operators in action.
SQL>
SQL> SET SERVEROUTPUT ON
SQL> BEGIN
  2    DBMS_OUTPUT.PUT_LINE(4 * 2);  --multiplication
  3    DBMS_OUTPUT.PUT_LINE(24 / 3); --division
  4    DBMS_OUTPUT.PUT_LINE(4 + 4);  --addition
  5    DBMS_OUTPUT.PUT_LINE(16 - 8); --subtraction
  6  END;
  7  /
8
8
8
8

PL/SQL procedure successfully completed.

SQL>

Exponentiation
SQL>
SQL> SET SERVEROUTPUT ON
SQL> BEGIN
  2    DBMS_OUTPUT.PUT_LINE(4 ** 2);
  3  END;
  4  /
16
PL/SQL procedure successfully completed.

SQL>
SQL>

The negation and identity operators in action.


SQL>
SQL> SET SERVEROUTPUT ON
SQL> DECLARE
  2    x   NUMBER;
  3  BEGIN
  4    DBMS_OUTPUT.PUT_LINE(-242.24);
  5    x := 5;
  6    DBMS_OUTPUT.PUT_LINE(-x);
  7     x := -5;
  8     DBMS_OUTPUT.PUT_LINE(-x);
  9     DBMS_OUTPUT.PUT_LINE(+10);
 10     DBMS_OUTPUT.PUT_LINE(+x);
 11  END;
 12  /
-242.24
-5
5
10
-5

PL/SQL procedure successfully completed.

SQL>

Comparison Operators
Comparison operators are used to compare one value or expression to another.
All comparison operators return a boolean result.
Operator Example Usage
= IF A = B THEN The equality operator.
<> IF A <> B THEN The inequality operator.
!= IF A != B THEN Another inequality operator, synonymous with <>.
~= IF A ~= B THEN Another inequality operator, synonymous with <>.
< IF A < B THEN The less than operator.
> IF A > B THEN The greater than operator.
<= IF A <= B THEN The less than or equal to operator.
>= IF A >= B THEN The greater than or equal to operator.
LIKE IF A LIKE B THEN The pattern-matching operator.
BETWEEN IF A BETWEEN B AND C THEN Checks to see if a value lies within a specified range of va
IN IF A IN (B,C,D) THEN Checks to see if a value lies within a specified list of value
IS NULL IF A IS NULL THEN Checks to see if a value is null.
The Relational Operators: =, <>, !=, ~=, <, >, <=, >=
String comparisons are case-sensitive.
String comparisons are dependent on the character set being used.
String comparisons are affected by the underlying datatype.
Comparing two values as CHAR strings might yield different results than the same values compared a
VARCHAR2 strings.
It's important to remember that Oracle dates contain a time component
True Expressions False Expressions
5=5 5=3
'AAAA' = 'AAAA' 'AAAA ' = 'AAAA'
5 != 3 5 <> 5
'AAAA ' ~= 'AAAA' 'AAAA' ~= 'AAAA'
10 < 200 10.1 < 10.05
'Jeff' < 'Jenny' 'jeff' < 'Jeff'
TO_DATE('15-Nov-61' < '15-Nov-97') TO_DATE('1-Jan-97' < '1-Jan-96')
10.1 <= 10.1 10 <= 20
'A' <= 'B' 'B' <= 'A'
TO_DATE('1-Jan-97') <= TO_DATE('1-Jan-
TO_DATE('15-Nov-61') <= TO_DATE('15-Nov-60)
97)
Logical Operators
PL/SQL has three logical operators: AND, OR, and NOT.
The NOT operator is typically used to negate the result of a comparison expression.
The AND and OR operators are typically used to link together multiple comparisons.
The Syntax for the NOT Operator:
NOT boolean_expression

boolean_expression can be any expression resulting in a boolean, or true/false value.


The Syntax for the AND Operator:
boolean_expression AND boolean_expression

boolean_expression can be any expression resulting in a boolean, or true/false value.


The AND operator returns a value of true if both expressions each evaluate to true;
otherwise, a value of false is returned.
Expression Result
(5 = 5) AND (4 < 10) AND (2 >=
true
2)
(5 = 7) AND (5 = 5) false
'Mon' IN ('Sun','Sat') AND (2 = 2) false
The Syntax for the OR Operator:
boolean_expression OR boolean_expression

boolean_expression can be any expression resulting in a boolean, or true/false, value.


The OR operator returns a value of true if any one of the expressions evaluates to true.
A value of false is returned only if both the expressions evaluate to false.
Expression Result
(5 <> 5) OR (4 >= 100) OR (2 <
false
2)
(7 = 4) OR (5 = 5) true
'Mon' IN ('Sun','Sat') OR (2 = 2) true
String Operators
PL/SQL has two operators specifically designed to operate only on character string data.
These are the LIKE operator and the concatenation (||) operator.
The Syntax for the Concatenation Operator:
string_1 || string_2

string_1 and string_2 are both character strings and can be string constants, string variables, or string
expressions.
SQL>
SQL> SET SERVEROUTPUT ON
SQL>
SQL> DECLARE
  2    a     VARCHAR2(30);
  3    b     VARCHAR2(30);
  4    c     VARCHAR2(30);
  5  BEGIN
  6    c := 'A' || ' AND ' || 'B';
  7    DBMS_OUTPUT.PUT_LINE(c);
  8     a := ' C ';
  9     b := ' D ';
 10     DBMS_OUTPUT.PUT_LINE(a || ' ' || b || ',');
 11     a := ' E ';
 12     b := ' F';
 13     c := a || b;
 14     DBMS_OUTPUT.PUT_LINE(c);
 15  END;
 16  /
A AND B
C   D ,
E  F

PL/SQL procedure successfully completed.

SQL>

Use of Comparison Operators with Strings


When using character strings in comparison expressions, the result depends on several things:
1. Character set
2. Datatype
3. Case (upper versus lower)

In the typical ASCII environment,


1. all lowercase letters are actually greater than all uppercase letters,
2. digits are less than all letters, and
3. the other characters fall in various places depending on their corresponding ASCII codes.

Writing a simple program

The simplest kind of PL/SQL code is called an anonymous block.


An anonymous block is a block of code that has its own DECLARE/BEGIN/END structure.
Anonymous blocks can either stand on their own (as shown here) or they can sit within any other P
program.
The general syntax for an anonymous block:
declare
    ...
     <Declaration part>
    ...
begin
    ...
    <Procedural part>
    ...
exception
    ...
    <Exception handler>
    ...
end;

1. The declaration section defines all variables, cursors, subprograms, and other elements to be u
code.
2. The declaration section is optional.
3. The procedural section contains the main body of the routine.
4. The procedural section starts with the begin keyword and ends with the exception keyword or th
keyword if you have no exception section.
5. The procedural section is the only mandatory part of the code.
6. You must have at least one line of executable code in the procedural section.
7. You can use the NULL command to indicate that nothing should be executed.
8. The exception section is also optional.
9. The exception section allows the program to intercept and process exceptions.

Each complete line of the PL/SQL code must end with a semicolon (;).

To run the code, type / at the beginning of the first blank line after the last line of the code.
SQL> set SERVEROUTPUT ON
SQL> declare
  2      v_string varchar2(256):='Hello, World!';
  3  begin
  4      dbms_output.put_line(v_string);
  5  end;
  6  /
Hello, World!

PL/SQL procedure successfully completed.

SQL>

Anonymous Block Structure


An anonymous block does not form the body of a procedure, function, or trigger.
Anonymous blocks can be used inline as part of a SQL*Plus script.
Anonymous blocks can also be nested inside procedure and function blocks for error handling.
The Syntax for PL/SQL Anonymous Blocks
[DECLARE variable_declarations]
BEGIN
  program_code
  [EXCEPTION error_handling_code]
END;

error_handling_code controls branches in the event of an error.


The keyword EXCEPTION begins the portion of the block that contains exception-handling code.
The exception-handling portion of a block is optional.
If the exception-handling portion is present, any runtime error or exception will cause program control t
to this part of the block.
An example of an anonymous block.

DECLARE
  hundreds_counter  NUMBER(1,-2);
BEGIN
  hundreds_counter := 100;
  LOOP
    DBMS_OUTPUT.PUT_LINE(hundreds_counter);
     hundreds_counter := hundreds_counter + 100;
   END LOOP;
 EXCEPTION
 WHEN OTHERS THEN
   DBMS_OUTPUT.PUT_LINE('That is as high as you can go.');
END;
/

Anonymous blocks can be nested in the procedure and exception bl


as many levels as you want
<<MAIN>>
declare
    ...
     Declaration section
    ...
begin
    ...
    Procedural section
    ...
        <<SUB1>>
        declare
        ...
        begin
        ...
        end;
    ...
exception
    ...
end;

You can label all blocks including nested ones by using identifiers enclosed in << >>.
This notation allows programmers to reference elements of different blocks.
The Lexical Set of Elements

1. The PL/SQL lexical set of elements consists of identifiers, delimiters, literals, and comments.
2. Identifiers are names of PL/SQL program items and units.
3. These items and units could be of different kinds - constants, variables, exceptions, cursors, cu
variables, subprograms, and packages.
4. An identifier cannot exceed 30 characters.
5. An identifier consists of a letter optionally followed by more letters, numerals, dollar signs, unde
and number signs.
6. By default, identifiers are not case sensitive.
7. Identifiers may not be the same as reserved words, so you cannot use the word end as a variab

Delimiters

A delimiter is a simple or compound symbol that has a special meaning 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
<, >, <=, >= Relational operators
<>, '=, ~=,
Different version of NOT EQUAL
^=
Comments
PL/SQL allows two types of comments: single and multi-line.
Single-line comments start with a delimiter -- and go to the end of the line, as shown here:
declare
  here you should declare variables,
  constants, etc.
   ...
begin
-- here you place your code
    ...
end;
/

Multi-line comments start with /* and end with */.


These delimiters may span as many lines as needed. An example is shown here:
declare
/* This code is written by java2s.com. Dec 20 2007 */
    ...
begin
    ...
end;

Declaring variables
In PL/SQL, variables must be included in the declaration block before they can be used.
There are a number of ways to declare a variable.
The most common way is by using a direct declaration, as shown here:
declare
    variable_name [constant] DATATYPE [DEFAULT value |DEFAULT NULL];
begin
    ...

The keyword constant means that the variable's value can't be changed in the body of the program.
If you declare a variable as a constant, you must assign a default value to it by using the optional DEF
value clause.
The following shows an example of correct declarations of variables:
SQL> declare
  2      v_sal   NUMBER;
  3      v_name  VARCHAR2(10) DEFAULT 'KING';
  4      v_start_dt DATE := SYSDATE; -- same as DEFAULT SYSDATE
  5  begin
  6      NULL;
  7  end;
  8  /

PL/SQL procedure successfully completed.

SQL>

Declaring a Variable by Reference


declare
    variable_name  table.column%TYPE;
    variable_name2 variable_name%TYPE;
    variable_row   table%ROWTYPE;
begin

The following code shows some examples of defining datatypes:

SQL> -- create demo table
SQL> create table Employee(
  2    ID                 VARCHAR2(4 BYTE)         NOT NULL,
  3    First_Name         VARCHAR2(10 BYTE),
  4    Last_Name          VARCHAR2(10 BYTE),
  5    Start_Date         DATE,
  6    End_Date           DATE,
  7    Salary             Number(8,2),
  8    City               VARCHAR2(10 BYTE),
  9    Description        VARCHAR2(15 BYTE)
 10  )
 11  /

Table created.

SQL>
SQL> -- prepare data
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_
  2               values ('01','Jason',    'Martin',  to_date('19960725','YYYYMMDD'), to_d
  3  /

1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_
  2                values('02','Alison',   'Mathews', to_date('19760321','YYYYMMDD'), to_d
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_
  2                values('03','James',    'Smith',   to_date('19781212','YYYYMMDD'), to_d
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_
  2                values('04','Celia',    'Rice',    to_date('19821024','YYYYMMDD'), to_d
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_
  2                values('05','Robert',   'Black',   to_date('19840115','YYYYMMDD'), to_d
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_
  2                values('06','Linda',    'Green',   to_date('19870730','YYYYMMDD'), to_d
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_
  2                values('07','David',    'Larry',   to_date('19901231','YYYYMMDD'), to_d
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_
  2                values('08','James',    'Cat',     to_date('19960917','YYYYMMDD'), to_d
  3  /

1 row created.

SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
  2  /

ID   FIRST_NAME           LAST_NAME            START_DAT END_DATE      SALARY CITY       D
---- -------------------- -------------------- --------- --------- ---------- ---------- -
01   Jason                Martin               25-JUL-96 25-JUL-06    1234.56 Toronto    P
02   Alison               Mathews              21-MAR-76 21-FEB-86    6661.78 Vancouver  T
03   James                Smith                12-DEC-78 15-MAR-90    6544.78 Vancouver  T
04   Celia                Rice                 24-OCT-82 21-APR-99    2344.78 Vancouver  M
05   Robert               Black                15-JAN-84 08-AUG-98    2334.78 Vancouver  T
06   Linda                Green                30-JUL-87 04-JAN-96    4322.78 New York   T
07   David                Larry                31-DEC-90 12-FEB-98    7897.78 New York   M
08   James                Cat                  17-SEP-96 15-APR-02    1232.78 Vancouver  T

8 rows selected.

SQL>
SQL>
SQL> declare
  2      v_empno1 employee.id%TYPE;
  3      v_empno2 v_empno1%TYPE;
  4      v_emp_rec employee%ROWTYPE;
  5  begin
  6      NULL;
  7  end;
  8  /

PL/SQL procedure successfully completed.

SQL>
SQL> -- clean the table
SQL> drop table Employee
  2  /

Table dropped.

SQL>
SQL>
SQL>

There are some restrictions on the declaration of variables:


There is no forward declaration.
Multiple declarations are supported in PL/SQL, as shown here:
SQL> declare
  2      --v1, v2 NUMBER; -- INVALID
  3      -- VALID
  4      v1 NUMBER;
  5      v2 NUMBER;
  6  begin
  7      NULL;
  8  end;
  9  /

PL/SQL procedure successfully completed.

Assigning values to variables


SQL>
SQL>
SQL> declare
  2      v_length NUMBER DEFAULT 5;
  3      v_height NUMBER := 4;
  4      v_width  NUMBER;
  5      v_volume NUMBER;
  6      v_min    NUMBER;
  7  begin
  8      v_width := 3;
  9      v_volume:= v_length*v_width*v_height;
 10      v_min := least(v_length,v_width,v_height);
 11  end;
 12  /

PL/SQL procedure successfully completed.

SQL>

Assign SQL query results to PL/SQL variables


SQL>
SQL>
SQL> -- create demo table
SQL> create table Employee(
  2    ID                 VARCHAR2(4 BYTE)         NOT NULL,
  3    First_Name         VARCHAR2(10 BYTE),
  4    Last_Name          VARCHAR2(10 BYTE),
  5    Start_Date         DATE,
  6    End_Date           DATE,
  7    Salary             Number(8,2),
  8    City               VARCHAR2(10 BYTE),
  9    Description        VARCHAR2(15 BYTE)
 10  )
 11  /

Table created.

SQL>
SQL> -- prepare data
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_
  2               values ('01','Jason',    'Martin',  to_date('19960725','YYYYMMDD'), to_d
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_
  2                values('02','Alison',   'Mathews', to_date('19760321','YYYYMMDD'), to_d
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_
  2                values('03','James',    'Smith',   to_date('19781212','YYYYMMDD'), to_d
  3  /
1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_
  2                values('04','Celia',    'Rice',    to_date('19821024','YYYYMMDD'), to_d
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_
  2                values('05','Robert',   'Black',   to_date('19840115','YYYYMMDD'), to_d
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_
  2                values('06','Linda',    'Green',   to_date('19870730','YYYYMMDD'), to_d
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_
  2                values('07','David',    'Larry',   to_date('19901231','YYYYMMDD'), to_d
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_
  2                values('08','James',    'Cat',     to_date('19960917','YYYYMMDD'), to_d
  3  /

1 row created.

SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
  2  /

ID   FIRST_NAME           LAST_NAME            START_DAT END_DATE      SALARY CITY       D
---- -------------------- -------------------- --------- --------- ---------- ---------- -
01   Jason                Martin               25-JUL-96 25-JUL-06    1234.56 Toronto    P
02   Alison               Mathews              21-MAR-76 21-FEB-86    6661.78 Vancouver  T
03   James                Smith                12-DEC-78 15-MAR-90    6544.78 Vancouver  T
04   Celia                Rice                 24-OCT-82 21-APR-99    2344.78 Vancouver  M
05   Robert               Black                15-JAN-84 08-AUG-98    2334.78 Vancouver  T
06   Linda                Green                30-JUL-87 04-JAN-96    4322.78 New York   T
07   David                Larry                31-DEC-90 12-FEB-98    7897.78 New York   M
08   James                Cat                  17-SEP-96 15-APR-02    1232.78 Vancouver  T

8 rows selected.

SQL>
SQL>
SQL> declare
  2      v_name VARCHAR2(256);
  3  begin
  4      select first_name
  5        into v_name
  6        from employee
  7      where id=7;
  8      DBMS_OUTPUT.put_line('v_name:'||v_name);
  9
 10  end;
 11  /
v_name:David

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
  2  /

Table dropped.

SQL>
SQL>

Literals as variable values


Literals are explicit numeric, character, string, or Boolean values not represented by an identifier.
Two types of numeric literals exist:
1. Integer literals represent optionally signed numeric values without decimal points.
2. Real literals represent optionally signed whole or fractional numbers with decimal points.

Examples of Integer and Real Literals

SQL>
SQL>
SQL> declare
  2      v_int1 BINARY_INTEGER :=5;  -- integer
  3      v_int2 BINARY_INTEGER :=-5; -- integer
  4      v_int3 BINARY_INTEGER :=0;  -- integer
  5      v_int4 BINARY_INTEGER :=+5; -- integer
  6
  7      v_real1 NUMBER :=1.0;       -- real
  8      v_real2 NUMBER :=1.;        -- real
  9      v_real3 NUMBER :=-7.113;    -- real
 10      v_real4 NUMBER :=0.2;       -- real
 11      v_real5 NUMBER :=.3;        -- real
 12      v_real6 NUMBER :=0.1;       -- real
 13      v_real7 NUMBER :=2/6;       -- real
 14  begin
 15     NULL;
 16  end;
 17  /

PL/SQL procedure successfully completed.

SQL>

Numeric literals cannot contain dollar signs or commas, but they can
written in scientific notation
SQL>
SQL> declare
  2  --    v_real1 NUMBER:=$123456.00; -- INVALID
  3  --    v_real2 NUMBER:=123,456.00; -- INVALID
  4        v_real3 NUMBER:=5e10;       -- VALID
  5        v_real3 NUMBER:=5e-3;       -- VALID
  6  begin
  7       NULL;
  8  end;
  9  /

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL>

Oracle supports scientific notation for numbers between 1E-130 and 1E+126, where E stands for "time
the power of".

Character and string literals in the Oracle world are enclosed by sing
quotes
SQL>
SQL> declare
  2      v_char  CHAR(1):='H';
  3      v_text1 VARCHAR2(10) :='Hello';
  4      v_text2    CHAR(1) :=''; -- the same as NULL
  5  begin
  6     NULL;
  7  End;
  8  /

PL/SQL procedure successfully completed.

SQL>

Вам также может понравиться