Lesson 5
Lesson 5
Overview of PL/SQL
Topic Coved
2-18
3-18
What is PL/SQL?
PL/SQL is an extension to SQL with design
features of programming languages.
Data manipulation and query statements are
included within procedural units of code.
5-18
6-18
8-18
10-18
11-18
2) Executable section
A PL/SQL block has an executable section. An
executable section starts with the keyword BEGIN
and ends with the keyword END.
3)Exception-handling section
12-18
A PL/SQL block has an exception-handling section
that starts with the keyword EXCEPTION
.Oracle Corporation, 1996. All rights reserved سCopyright
PL/SQL anonymous
13-18
1)
BEGIN
DBMS_OUTPUT.put_line ('Hello World!');
END;
2)
DECLARE
l_message VARCHAR2( 255 ) := 'Hello World!';
BEGIN
DBMS_OUTPUT.PUT_LINE( l_message );
14-18
END;
.Oracle Corporation, 1996. All rights reserved سCopyright
PL/SQL Boosts Performance
SQL
SQL
Application Other DBMSs
SQL
SQL
SQL
IF...THEN
SQL
Application ELSE Oracle with
SQL PL/SQL
;END IF
SQL
15-18
16-18
DML IN PL/SQL
DRL IN PL/SQL
17-18
BEGIN
INSERT Statements;
UPDATE statements;
DELETE statements;
END;
/
EXAMPLE:
BEGIN
INSERT INTO TABLE1 VALUES ('C14');
UPDATE TABLE1 SET NO='C12' WHERE NO ='C13';
18-18 DELETE TABLE1 WHERE NO ='C14';
END;
.Oracle Corporation, 1996. All rights reserved سCopyright
DRL IN PL/SQL
DECLARE
NAME VARCHAR2(20);
BEGIN
SELECT first_name INTO NAME
FROM employees WHERE EMPLOYEE_ID=101
DBMS_OUTPUT.PUT_LINE(NAME);
END;
20-18
21-18
END;
END; END;
END; END;
END;
22-18
Oracle Server
23-18
24-18
Types of Variables :
a) Scalar – this data types hold a single value.
(data types that corresponds with column types.
26-18
27-18
DECLARE
l_total_sales NUMBER(15,2);
l_credit_limit NUMBER (10,0);
;l_contact_name VARCHAR2(255)
BEGIN
;NULL
;END
28-18
DECLARE
l_shipping_status VARCHAR2( 25 ) NOT
NULL := 'Shipped';
BEGIN
l_shipping_status := '';
END;
30-18
31-18
32-18
33-21
DECLARE
l_customer_name customers.name%TYPE;
l_credit_limit customers.credit_limit%TYPE;
BEGIN
SELECT name, credit_limit INTO l_customer_name,
l_credit_limit FROM customers WHERE customer_id = 38;
DBMS_OUTPUT.PUT_LINE(l_customer_name || ':' ||
l_credit_limit );
END; /
34-21
Single-line comments
A single-line comment starts with a double hyphen ( --)
that can appear anywhere on a line and extends to the
end of the line.
-- valued added tax 10%
DECLARE co_vat_rate CONSTANT NUMBER := 0.1;
Multi-line comments
A multi-line comment starts with a slash-asterisk ( /* )
and ends with an asterisk-slash ( */ ), and can span
multiple lines:
35-21