0% found this document useful (0 votes)
26 views

1 Introduction

Uploaded by

Md. Ziaul Hoque
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)
26 views

1 Introduction

Uploaded by

Md. Ziaul Hoque
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/ 9

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. Following are notable
facts about PL/SQL:

 PL/SQL is a completely portable, high-performance transaction-processing language.


 PL/SQL provides a built-in interpreted and OS independent programming environment.
 PL/SQL can also directly be called from the command-line SQL*Plus interface.
 Direct call can also be made from external programming language calls to database.
 PL/SQL's general syntax is based on that of ADA and Pascal programming language.
 Apart from Oracle, PL/SQL is available in TimesTen in-memory database and IBM DB2.

Features of PL/SQL

PL/SQL has the following features:

 PL/SQL is tightly integrated with SQL.


 It offers extensive error checking.
 It offers numerous data types.
 It offers a variety of programming structures.
 It supports structured programming through functions and procedures.
 It supports object-oriented programming.
 It supports developing web applications and server pages.

Advantages of PL/SQL

PL/SQL has the following advantages:

 SQL is the standard database language and PL/SQL is strongly integrated with SQL. PL/SQL
supports both static and dynamic SQL. Static SQL supports DML operations and transaction
control from PL/SQL block. Dynamic SQL is SQL allows embedding DDL statements in
PL/SQL blocks.
 PL/SQL allows sending an entire block of statements to the database at one time. This
reduces network traffic and provides high performance for the applications.
 PL/SQL gives high productivity to programmers as it can query, transform, and update data
in a database.
 PL/SQL saves time on design and debugging by strong features, such as exception handling,
encapsulation, data hiding, and object-oriented data types.
 Applications written in PL/SQL are fully portable.
 PL/SQL provides high security level.
 PL/SQL provides access to predefined SQL packages.
 PL/SQL provides support for Object-Oriented Programming.
 PL/SQL provides support for Developing Web Applications and Server Pages.

CHARACTERSTICS
1 Highly structured, readable and accessible language.
2 Standard and Portable language.
3 Embedded language.
4 Improved execution authority.
Page 1 of 9
STANDARD PACKAGE
Oracle has defined in this special package. Oracle defines quite a few identifiers
in this package, including built-in exceptions, functions and subtypes.
You can reference the built-in form by prefixing it with STANDARD.
The basic unit in any PL/SQL program is block. All PL/SQL programs are
composed of blocks which can occur sequentially or nested.
BLOCK STRUCTURE
Declare
-- declarative section
Begin
-- executable section
Exception
-- exception section
End;
In the above, declarative and exception sections are optional.

SQL> DECLARE
A NUMBER (2);
B NUMBER (2);
C NUMBER (2);
BEGIN
A:=88;
B:=0;
C:=A/B;
DBMS_OUTPUT.PUT_LINE(C);
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE ('DIVISION BY ZERO PROVIDE AN IMAGINARY
NUMBER’);
END;
/
DIVISION BY ZERO PROVIDE AN IMAGINARY NUMBER

PL/SQL procedure successfully completed.

SQL>

BLOCK TYPES
1 Anonymous blocks
2 Named blocks
Labeled blocks
Subprograms
Triggers

Page 2 of 9
ANONYMOUS BLOCKS
Anonymous blocks imply basic block structure.
Ex:
BEGIN
Dbms_output.put_line(‘My first program’);
END;

LABELED BLOCKS
Labeled blocks are anonymous blocks with a label which gives a name to the
block.
Ex:
<<my_bloock>>
BEGIN
Dbms_output.put_line(‘My first program’);
END;
SUBPROGRAMS
Subprograms are procedures and functions. They can be stored in the database
as stand-alone objects, as part of package or as methods of an object type.
TRIGGERS
Triggers consist of a PL/SQL block that is associated with an event that occurs
in the database.
NESTED BLOCKS
A block can be nested within the executable or exception section of an outer
block.
IDENTIFIERS
Identifiers are used to name PL/SQL objects, such as variables, cursors, types
and subprograms. Identifiers consists of a letter, optionally followed by any
sequence of characters, including letters, numbers, dollar signs, underscores,
and pound signs only. The maximum length for an identifier is 30 characters.

COMMENTS
Comments improve readability and make your program more understandable.
They are ignored by the PL/SQL compiler. There are two types of comments
available.
1 Single line comments
2 Multiline comments
SINGLE LINE COMMENTS
A single-line comment can start any point on a line with two dashes and
continues until the end of the line.
Ex:
BEGIN
Dbms_output.put_line(‘hello’); -- sample program
END;
Page 3 of 9
MULTILINE COMMENTS
Multiline comments start with the /* delimiter and ends with */ delimiter.
Ex:
BEGIN
Dbms_output.put_line(‘hello’); /* sample program */
END;

VARIABLE DECLERATIONS
Variables can be declared in declarative section of the block;
Ex:
DECLARE
a number;
b number := 5;
c number default 6;
____________________________________
SQL> DECLARE
A NUMBER(4);
B NUMBER(4);
C NUMBER(7,2);
BEGIN
SELECT SAL INTO A
FROM EMP
WHERE EMPNO=1;
B:=&B;
C:=B+A;
DBMS_OUTPUT.PUT_LINE(A);
DBMS_OUTPUT.PUT_LINE(B);
DBMS_OUTPUT.PUT_LINE(C);
END;
/

Enter value for b: 2000


old 9: B:=&B;
new 9: B:=2000;
5000
2000
7000

PL/SQL procedure successfully completed.

Page 4 of 9
CONSTANT DECLERATIONS
To declare a constant, you include the CONSTANT keyword, and you must
supply a default value.
Ex:
DECLARE
b constant number := 5;
c constant number default 6;

SQL> DECLARE
pi CONSTANT NUMBER(3,2):=3.14;
r NUMBER(2);
a NUMBER(15,2);
BEGIN
r:=&r;
a:=pi*r*r;
DBMS_OUTPUT.PUT_LINE(a);
END;
/
Enter value for r: 3
old 6: r:=&r;
new 6: r:=3;
28.26

PL/SQL procedure successfully completed.

SQL>

NOT NULL CLAUSE


You can also specify that the variable must be not null.
Ex:
DECLARE
b constant number not null:= 5;
c number not null default 6;
ANCHORED DECLERATIONS
PL/SQL offers two kinds of anchoring.
1 Scalar anchoring
2 Record anchoring

Page 5 of 9
SCALAR ANCHORING:
Use the %TYPE attribute to define your variable based on table’s column of
some other PL/SQL scalar variable.
Ex:
DECLARE
dno dept.deptno%type;
Subtype t_number is number;
a t_number;
Subtype t_sno is student.sno%type;
V_sno t_sno;

SQL> declare
my_name emp.ename %TYPE;
begin
select ename into my_name from emp
where empno=1;
dbms_output.put_line('MY NAME:'||my_name);
end;
/
MY NAME:SHAKIL

PL/SQL procedure successfully completed.

RECORD ANCHORING:
Use the %ROWTYPE attribute to define your record structure based on a table.
Ex:
`DECLARE
V_dept dept%rowtype;

SQL> DECLARE
emp_info emp%ROWTYPE;
BEGIN
SELECT* INTO emp_info
FROM EMP
WHERE empno=1;
DBMS_OUTPUT.PUT_LINE('EMPLOYEE NO:'||emp_info.empno);
DBMS_OUTPUT.PUT_LINE('EMPLOYEE NAME:'||emp_info.ename);
DBMS_OUTPUT.PUT_LINE('EMPLOYEE HIRE DATE:'||emp_info.hiredate);
DBMS_OUTPUT.PUT_LINE('EMPLOYEE SALARY:'||emp_info.sal);
END;
/
EMPLOYEE NO:1
EMPLOYEE NAME:SHAKIL
EMPLOYEE HIRE DATE:01-JUL-17
EMPLOYEE SALARY:5000

PL/SQL procedure successfully completed.


Page 6 of 9
BENEFITS OF ANCHORED DECLARATIONS
1 Synchronization with database columns.
2 Normalization of local variables.
PROGRAMMER-DEFINED TYPES
With the SUBTYPE statement, PL/SQL allows you to define your own subtypes
or aliases of predefined data types, sometimes referred to as abstract data
types.
There are two kinds of subtypes.
1 Constrained
2 Unconstrained

CONSTRAINED SUBTYPE
A subtype that restricts or constrains the values normally allowed by the data
type itself.
Ex:
Subtype positive is binary integer range 1..2147483647;

In the above declaration a variable that is declared as positive can store only
integer greater than zero even though binary integer ranges from -
2147483647..+2147483647.

UNCONSTRAINED SUBTYPE
A subtype that does not restrict the values of the original datatype in variables
declared with the subtype.
Ex:
Subtype float is number;
DATATYPE CONVERSIONS
PL/SQL can handle conversions between different families among the datatypes.
Conversion can be done in two ways.
1 Explicit conversion
2 Implicit conversion

EXPLICIT CONVERSION
This can be done using the built-in functions available.

Page 7 of 9
IMPLICIT CONVERSION
PL/SQL will automatically convert between datatype families when possible.
Ex:
DECLARE
a varchar(10);
BEGIN
select deptno into a from dept where dname='ACCOUNTING';
END;
In the above variable a is char type and deptno is number type even though,
oracle will automatically converts the numeric data into char type assigns to the
variable.
PL/SQL can automatically convert between
1 Characters and numbers
2 Characters and dates
VARIABLE SCOPE AND VISIBILITY
The scope of a variable is the portion of the program in which the variable can
be accessed. For PL/SQL variables, this is from the variable declaration until the
end of the block. When a variable goes out of scope, the PL/SQL engine will free
the memory used to store the variable.
The visibility of a variable is the portion of the program where the variable can
be accessed without having to qualify the reference. The visibility is always
within the scope. If it is out of scope, it is not visible.
DECLARE DECLARE
a number; -- scope of a a number;
BEGIN b number;
-------- BEGIN
DECLARE -- a , b available here
b number; -- scope of b DECLARE
BEGIN b char(10);
----- BEGIN
END; -- a and char type b is available here
------ END;
END; -----
END;
<<my_block>>
DECLARE
a number;
b number;
BEGIN
-- a , b available here
DECLARE
b char(10);
BEGIN
-- a and char type b is available here
-- number type b is available using <<my_block>>.b
END;
------
END;
Page 8 of 9
PL/SQL OPERATOR:

1. Arithmatic Operator (+,-,*,/)


2. Relational Operatot (>,<,>=,<=,=,<>,!=)
3. Comparison Operator (BETWEEN, IN, LIKE, IS NULL)
4. Logical Operator(AND, OR, NOT)
5. String Operator

EXCEPTION:

OTACLE DATABASE এ সাধারণত দুই ধরননর Exception দদখা যায়ঃ


1. Pre-defined Exception
2. User Defined Exception

1. Pre-Defined Exception:
 No_data_found
 Too_many_rows
 Value_error
 Zero_divide
 Dup_val_on_index
 Invalid_number
 Other

2. User Defined Exception

Page 9 of 9

You might also like