0% found this document useful (0 votes)
62 views41 pages

PLSQL 01 - 10 - Sept - 2020

The document provides an introduction to PL/SQL and discusses its key features and advantages. It describes PL/SQL as a procedural language extension of SQL that allows developers to perform tasks like: defining blocks of code with control structures; declaring variables, constants, and subtypes; and using SQL statements within PL/SQL blocks. Some key benefits highlighted include better performance, tighter integration with SQL, and higher developer productivity.

Uploaded by

devasheesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views41 pages

PLSQL 01 - 10 - Sept - 2020

The document provides an introduction to PL/SQL and discusses its key features and advantages. It describes PL/SQL as a procedural language extension of SQL that allows developers to perform tasks like: defining blocks of code with control structures; declaring variables, constants, and subtypes; and using SQL statements within PL/SQL blocks. Some key benefits highlighted include better performance, tighter integration with SQL, and higher developer productivity.

Uploaded by

devasheesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Enterprise to the Power of DigitalTM

PLSQL
Mahendra Pachpor

9/10/2020
Course Code • Day-1 / PLSQL

Version Number:1.0

2 9/10/2020
Session Objectives

• At the end of this session, you will be able to:

• Understand the PL/SQL Block terminology and features of PL/SQL

• Use various PL/SQL types, variables and subtypes

• Use SQL statements inside PL/SQL Block

• Use various PL/SQL control structures

• Write and execute anonymous blocks

3 9/10/2020
About SQL

• The purpose of SQL is to provide an interface to a relational database such as Oracle Database,
and all SQL statements are instructions to the database.

• The strengths of SQL provide benefits for all types of users, including application
programmers, database administrators, managers, and end users.
Why PLSQL????/
• Now you must be having few questions in mind

If SQL is so
powerful, why
we require to
use PL/SQL?

Let us think
about few
scenarios…
Why PL/SQL?

How can we have a chain of SQL statements


which produce result according to the output
of previous queries?

How can we take any smart decisions based


on users input or based on output on previous
queries..?

How can we automate any task using SQL?


Introduction
• The PL/SQL programming language was developed by Oracle Corporation in the late 1980s as
procedural extension language for SQL and the Oracle relational database.

• PL/SQL, supplement SQL with standard programming-language features like:

• Block (modular) structure


• Flow-control statements and loops
• Variables, constants, and types
• Structured data PL/SQL is available in Oracle Database
• Customized error handling (since version 7)
Why PL/SQL?

• The purpose of PL/SQL is to combine database language and procedural programming


language.

• By extending SQL, PL/SQL offers a unique combination of power and ease of use.

• PL/SQL fully supports all SQL data manipulation statements.

• We can write procedures and functions which can be invoked from different applications.
Advantages of PL/SQL
Tight
Integration
Access to with SQL
Better
Pre-defined
Performance
Packages

Supports both
Tight Security static and
Advantages dynamic SQL.
Of
PL/SQL

Support for
Higher
Object-Oriented
Productivity
Programming

Support for
Full Portability Developing Web
Applications
Tight Integration With SQL

• PL/SQL lets you use all the SQL commands as well as all the SQL functions and
operators.

• This extensive SQL support lets you manipulate Oracle data flexibly and safely.

• PL/SQL fully supports SQL data types, which reduces the need to convert the data
passed between your applications and the database.

• Running a SQL query and processing the result set is as easy in PL/SQL.
Better Performance

Database
Application

Processes each SQL


Sends multiple SQL SQL Statement and returns
Statements results appropriately

Sends a single PL/SQL Processes entire block


block containing PL/SQL returns a results
multiple SQL Statements Block
Executes procedure
Procedure containing multiple SQL
Sends a single Call statements and returns a
procedure call results
Better Performance

• Without PL/SQL, Oracle must process SQL statements one at a time.

• Each SQL statement results in another call to Oracle and higher performance overhead.

• Every time a SQL statement is issued, it must be sent over the network, creating more traffic.

• With PL/SQL, an entire block of statements can be sent to Oracle at one time.

• This can drastically reduce communication between your application and Oracle.
Higher Productivity Full Portability

• PL/SQL lets you write very compact code for • Applications written in PL/SQL can run on
manipulating data any operating system and platform where
the Oracle database runs.
• PL/SQL can query, transform, and update
data in a database. • With PL/SQL, you can write portable
program libraries and reuse them in
• PL/SQL offers object-oriented features and different environments.
data types which in turn saves time on
design and debugging.
Access to Pre-defined Packages Support for Object-Oriented Programming

• Oracle provides product-specific packages • PL/SQL offers object-oriented features such as


that define APIs you can call from PL/SQL to • Exception handling
perform many useful tasks. • Encapsulation
• Data hiding
• To list a few , DBMS_FILE for reading and • Data types
writing operating system (OS) text files,
• Which in turn saves time on design and
• DBMS_OUTPUT for displaying output from debugging
PL/SQL blocks and subprograms
Advantages of PL/SQL

Supports both static and dynamic SQL

• PL/SQL lets you use all the SQL data manipulation, and transaction control commands in a pl/sql
block.

• Unlike static SQL, which remains the same in each execution, PL/SQL supports dynamic SQL which
enables you to build SQL statements as character strings at runtime.
PL/SQL Architecture
PL/SQL Architecture
PL/SQL Architecture

PL/SQL Engine SQL Engine


• SQL Engine executes SQL statements
• Compiles and executes PL/SQL blocks and received from PL/SQL Engine and returns
subprograms. result back to the pl/sql engine.

• Can also run on client machines that are


not running a database server but that can
talk to the database server machine over a
network.

• Splits the plsql block it executes procedural


statements and sends SQL queries to the
SQL engine
PL/SQL Environment
PLSQL Environment
• PL SQL does not have its own environment.
• There are various tools available to write code such as TOAD, SQL Developer, SQL*Plus i.e to see the result
outside of PL/SQL requires another program
SQL*PLUS

• Oracle's command-line tool


• Connects to an Oracle server where you can run SQL and PL/SQL statements.
• Provides a command-line interpreter for both SQL and PL/SQL.
• There are several different styles of executing SQL*Plus:

• As a console program
• As a pseudo-GUI
• (This form of SQL*Plus is available only on Microsoft Windows)
PLSQL Environment

SQL Developer

• Database administration and query tool that provides a single consistent interface for various databases

• It allows to visually navigate through your database structure, create and execute SQL queries and scripts the
easy way

TOAD

• Tool for Oracle Application Developers (TOAD)

• Software application from Quest Software

• Used for development and administration of various relational databases using SQL
PL/SQL Block
PL/SQL Block
• PL/SQL is a block-structured language.
• Each program written in PL/SQL is written as a block.
• Blocks can also be nested.
• Each block is meant for a particular task.

PL/SQL
Block types

Anonymous Named
Block Block
PL/SQL Block

Anonymous Block Named Block

• Anonymous block doesn’t have name. • Named block do have names.

• After compilation, it is not getting stored on • Once compiled, Named blocks are getting stored
oracle server. on oracle server.

• We can not call anonymous block in any other • We can call them in any other pl/sql block
pl/sql block
PL/SQL Anonymous Block
PL/SQL Named Block Structure

Header (named blocks only)


• A PL/SQL named block has
the following structure
IS
Declare Section

BEGIN

Execution Section

EXCEPTION

Exception Section

END;
PL/SQL Block Structure

Header Section
• Relevant for named blocks only
• Stored procedures (used to perform repetitive code.)
• Stored functions (used to return value to the calling block),
• Packages (allows related objects to be stored together),
• Triggers (pl/sql block executed implicitly whenever the triggering event
takes place).

• Determines the way that the named block or program must be called.

• Includes the name, parameter list, and RETURN clause (for a function
only).
PL/SQL Block Structure

• Header Section: Example

• Below is header section of stored procedure:

CREATE OR REPLACE PROCEDURE


print ( p_num NUMBER ) …

• Below is header section of stored function:

CREATE OR REPLACE FUNCTION


add ( p_num1 NUMBER, p_num2 NUMBER )
RETURN NUMBER …
PL/SQL Block Structure
Declare Section

• Relevant for anonymous blocks.

• Contains declarations of variables, constants, cursors,


user-defined exceptions and types.

• Optional section , but if you have one, it must come


before the execution and exception sections.

DECLARE
v_name VARCHAR2(35);
v_id NUMBER := 0;
PL/SQL Block Structure
Executable Section

• Mandatory section of PLSQL block


• Contains SQL statements to manipulate data in the database
• Contains PL/SQL statements to manipulate data in the block.
• Every block must have at least one executable statement in the
execution section.

BEGIN
SELECT ename
INTO v_ename
FROM emp
WHERE empno = 7900 ;
DBMS_OUTPUT.PUT_LINE
(‘Employee name :’ || v_ename);
END;
PL/SQL Block Structure

Exception Section

• The last section of the PL/SQL block.

• It contains statements that are executed when a runtime error occurs within a block.

• An optional section.

• Control is transferred to this section when an run time error is encountered and it is
handled

EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE
(‘ There is no employee with Employee no 7900 ’);
END;
PL/SQL Datatypes
PL/SQL Data Types
• The PL/SQL data types are divided into four categories

• Single values with no internal components.


Scalar
• For Example, NUMBER, DATE, or BOOLEAN.

• Pointers to large objects that are stored separately from other


data items.
Large Object (LOB)
• For Example, text, graphic images, video clips, and sound
waveforms.

• Items that have internal components that can be accessed


Composite Data individually.
• For example, collections and records.

• Pointers to other data items.


Reference
• For Example, REF cursor
PL/SQL Data Types
PL/SQL Variables & Datatypes
PL/SQL Variables

• These are placeholders that store the values that can change through the PL/SQL Block

• PL/SQL lets you declare constants and variables, then use them in SQL and procedural
statements anywhere an expression can be used

• A constant or variable must be declared before referencing it in other statements


PL/SQL Variables

• Syntax for declaring variables


variable_name [CONSTANT] datatype
[NOT NULL] [:= expr | DEFAULT expr]
Note: Square brace indicates optional

• Variable_name is the name of the variable.


• Datatype is a valid PL/SQL datatype.
• NOT NULL is an optional specification on the variable.
• A value or DEFAULT value is also an optional specification, where you can initialize a
variable.
• Each variable declaration is a separate statement and must be terminated by a
semicolon.
• CONSTANT keyword is used to declare constants.
PL/SQL Variables

• Valid variable declarations


DECLARE
v_Activedate DATE;
V_cust_id NUMBER(2) NOT NULL := 10;
V_Address VARCHAR2(13) := ’Pune’;
V_sr_id NUMBER DEFAULT 201;
V_Name VARCHAR2(20) DEFAULT ‘Aditya’

• Valid constant declaration


c_constant CONSTANT NUMBER := 1400;

• Invalid Declarations
v_cust_id NUMBER(2) NOT NULL;
v_name VARCHAR2 DEFAULT ‘Sachin’;

c_constant CONSTANT NUMBER ;


c_constant NUMBER CONSTANT;
Guidelines for Declaring PL/SQL Variables
• Follow the naming Rules
✓ The variable name must be less than 31 characters
✓ The starting of a variable must be an ASCII letter
✓ It can be either lowercase or uppercase
✓ A variable name can contain numbers, underscore, and dollar sign characters followed by the first character

• Follow the naming conventions

• Initialize variables designated as NOT NULL and CONSTANT

• Declare one identifier per line

• Initialize identifiers by using the assignment operator (:=) or the reserved word “DEFAULT”
Questions

40 9/10/2020
10SSEP2013
Thank You

www.birlasoft.com

9/10/2020
41

You might also like