Sei sulla pagina 1di 9

Support For PL/SQL

• SQL Data Manipulation Statements: SELECT, INSERT,


DELETE, UPDATE
• SQL Transaction Statements: COMMIT, ROLLBACK [TO],
SAVEPOINT, LOCK TABLE
• SQL Functions: Numeric, Character, Date, Group, Date
Conversion
• SQL Predicates: Simple Logic, AND, OR, NOT, BETWEEN,
IS NULL, LIKE, EXISTS, ANY, ALL
PL/SQL Syntax

• PL/SQL commands all terminate with a semicolon.


• PL/SQL statements are coded in free field format.
• Case of the commands is irrevant.
• Comments:
-- Comment
/* Comment */
PL/SQL Syntax (Continued)

Block Definition Syntax


[DECLARE
Block declarations]
BEGIN
Block statements
END;
Blocks may be embedded
PL/SQL Variables

• Variables: Used just like variables in any other language.

• Variable declarations: Can declare a variable of any


datatype native to ORACLE: Number, Varchar2, Char,
Date, etc. or to PL/SQL (Boolean or Record).

• PL/SQL does not support arrays of any type.


PL/SQL Variable Declaration Syntax

Declaration:
identifier datatype [NOT NULL] [:=
plsql_expression] ;
Examples:
bonus NUMBER (11,2); in_stock
BOOLEAN; total NUMBER
(7, 2) := 0.0;
birthday DATE := ‘09-OCT-1945’;
name CHAR (20);
Assigning Values To Variables

Two methods of assignment.


a)Straight assignment statement (:=)
a)tax := price * tax_rate;
b)bonus := salary * 0.10;
b)Assignment through the SELECT statement

SELECT SAL INTO CUR_SAL


FROM EMP
WHERE EID = ‘E12345’;

SELECT SAL * 0.10


INTO CUR_SAL FROM EMP
WHERE EID = ‘E12345’;
Constants

• Similar to variable declaration except the word


CONSTANT is inserted in the declaration and the
constant value is added to the declaration.
• Examples:
bonus_multiplier CONSTANT NUMBER
(3,2) := 0.10;
Declarations Using Types

• %type attribute provides the datatype of a variable,


constant, or column.

• Useful when declaring a variable that refers to a column


in a table

• Example: Assume a column called BOOK_TITLE is an


attribute of a table named BOOK_INFO. Declare a
variable called TITLE to have the same datatype as
BOOK_TITLE without knowing what type BOOK_TITLE is.
Declarations Using Types (continued)

• Example:
title book_info.book_title%type;
• Advantages:
– Don’t need to know BOOK_TITLE type
– If type changes, no problem
Declarations Using Rows

• %Rowtype is used to declare a record with the same


types as each value retrieved in a SELECT command.
• More on this later.

Potrebbero piacerti anche