Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

PLSQL

Uploaded by

abhiramragu51
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

PLSQL

Uploaded by

abhiramragu51
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

PL/SQL

PL/SQL stands for Procedural Language extension of SQL.


 PL/SQL is a combination of SQL along with the procedural features of programming languages.
 It was developed by Oracle Corporation in the early 90’s to enhance the capabilities of SQL.
 The basic units (procedures, functions, and anonymous blocks) that make up a PL/SQL program are logical
blocks, which can contain any number of nested sub-blocks.
 Each logical block corresponds to a problem or sub problem to be solved.PL/SQL fully integrates modern
software engineering features such as data encapsulation, information hiding, overloading, and exception
handling, and so brings state-of-the-art programming to the ORACLE Server and a variety of ORACLE tools. It
is not case sensitive.
 Each PL/SQL program consists of SQL and PL/SQL statements which form a PL/SQL block.
 PL/SQL is a block structured language. The programs of PL/SQL are logical blocks that can
contain any number of nested sub-blocks.
A PL/SQL Block consists of three sections:
1. Declaration section (optional).
2. Execution section (mandatory).
3. Exception (or Error) Handling section (optional).

ADVANTAGES OF PL/SQL:
1. Block Structures: PL SQL consists of blocks of code, which can be nested within each other. Each block
forms a unit of a task or a logical module. PL/SQL Blocks can be stored in the database and reused.

2. Procedural Language Capability: PL SQL consists of procedural language constructs such as conditional
statements (if else statements) and loops like (FOR loops).

3. Better Performance: PL SQL engine processes multiple SQL statements simultaneously as a single block,
thereby reducing network traffic.

4. Error Handling: PL/SQL handles errors or exceptions effectively during the execution of a PL/SQL
program. Once an exception is caught, specific actions can be taken depending upon the type of the exception or
it can be displayed to the user with a message

1.write a pl/sql code using basic variable, anchoreddeclarations


PL/SQL Variables
1. It needs to declare the variable first in the declaration section of a PL/SQL block
before using it.
2. By default, variable names are not case sensitive. A reserved PL/SQL keyword
cannot be used as a variable name.

How to declare variable in PL/SQL


You must declare the PL/SQL variable in the declaration section or in a package as a global
variable. After the declaration, PL/SQL allocates memory for the variable's value and the
storage location is identified by the variable name.
Syntax for declaring variable:
variable_name [CONSTANT] datatype [NOT NULL] [:= | DEFAULT initial_value]

Example:
Radius Number := 5;

Date_of_birth date;

 Variables belonging to the same datatype cannot be declared in thesame


statement.
N1,N2,N3 Number;
It is an illegal declaration.
 EX:

After the execution, this will produce the following result:

Value of c: 70
Value of f: 33.333333333333333333

PL/SQL procedure successfully completed.


1A) Write a PL/SQL program to read or declare basic variables and print them
declare
a number;
begin
a:=&a;
dbms_output.put_line('value of a is '||a);
end;
/
OUTPUT:

1 b) BIND VARIABLES
PL/SQL Program to declare bind variables
Bind Variables are the variables that are declared with Keyword VARIABLE and are
declared outside of DECLARE, BEGIN and END sections. To display these variables, use
keyword PRINT

variable A number;
declare
begin
:A:=10;
END;
/
print A;

1 c) PL/SQL Program to add 2 numbers


DECLARE
a NUMBER ;
b NUMBER ;
c NUMBER ;
BEGIN
a:=&a;
b:=&b;
c:=a+b;
DBMS_OUTPUT.PUT_LINE('sum is' ||c);
END;
/
1d) Write a PL/SQL program for anchored declarations
Assigning SQL Query Results to PL/SQL Variables

You can use the SELECT INTO statement of SQL to assign values toPL/SQL variables. For
each item in the SELECT list, there must be a corresponding, type-compatible variable in
the INTO list. The following example illustrates the concept. Let us create a table named
CUSTOMERS –

CREATE TABLE CUSTOMERS(


ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);

Table Created

Let us now insert some values in the table −

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (2, 'Khilan', 25, 'Delhi', 1500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (3, 'kaushik', 23, 'Kota', 2000.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (4, 'Chaitali', 25, 'Mumbai', 6500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (5, 'Hardik', 27, 'Bhopal', 8500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (6, 'Komal', 22, 'MP', 4500.00 );
The following program assigns values from the above table toPL/SQL variables using
the SELECT INTO clause of SQL –
SYNTAX FOR ANCHORED DECLARATIONS:

Variable-name Typed-attribute %Type;

DECLARE
c_id customers.id%type := 1;
c_name customers.name%type;
c_addr customers.address%type;
c_sal customers.salary%type;
BEGIN
SELECT name, address, salary INTO c_name, c_addr,
c_sal
FROM customers
WHERE id = c_id;
dbms_output.put_line
('Customer ' ||c_name || ' from ' || c_addr || '
earns ' || c_sal);
END;
/
When the above code is executed, it produces the following result

Customer Ramesh from Ahmedabad earns 2000

PL/SQL procedure completed successfully


PROGRAM TO FIND BIGGEST OF 3 NUMBERS
DECLARE
a NUMBER(3) := &a;
b NUMBER(3) := &b;
c NUMBER(3) := &c;
BEGIN
IF (a > b AND a > c) THEN
DBMS_OUTPUT.PUT_LINE('A is big: ' || a);
ELSIF (b > a AND b > c) THEN
DBMS_OUTPUT.PUT_LINE('B is big: ' || b);
ELSE
DBMS_OUTPUT.PUT_LINE('C is big: ' || c);
END IF;
END;
output:-
set serveroutput on
old 2: a number(3):=&a;
new 2: a number(3):=3;
old 3: b number(3):=&b;
new 3: b number(3):=4;
old 4: c number(3):=&c;
new 4: c number(3):=5;
C is big5
PL/SQL procedure successfully completed

PROGRAM TO FIND THE RADIUS OF A CIRCLE


DECLARE
pi CONSTANT NUMBER(5,2) := 3.14;
r NUMBER(5) := &r;
a NUMBER(7,2);
BEGIN
a := pi * r * r;
DBMS_OUTPUT.PUT_LINE('Area of the circle is: ' || a);
END;
Output:
set serveroutput on
old 3: r number(5):=&r;
new 3: r number(5):=4;
Area of the Circle is 50.24
PL/SQL procedure successfully completed
2.
3..CURSORS
A cursor is a temporary work area created in the system memory when a SQL statement is executed. A cursor contains
information on a select statement and the rows of data accessed by it. This temporary work area is used to store the
data retrieved from the database, and manipulate this data. A cursor can hold more than one row, but can process only
one row at a time. The set of rows the cursor holds is called the active set.
There are two types of cursors in PL/SQL:
Implicit cursors:
These are created by default when DML statements like, INSERT, UPDATE, and DELETE statements are executed. They
are also created when a SELECT statement that returns just one row is executed

. Explicit cursors:
They must be created when you are executing a SELECT statement that returns more than one row. Even though the
cursor stores multiple records, only one record can be processed at a time, which is called as current row. When you
fetch a row the current row position moves to next row.
Both implicit and explicit cursors have the same functionality, but they differ in the way they are accessed.
/*Write a PL/SQL Block using Cursor to display the particular student no,
Name and Age in student table from a given row id*/
set serveroutput on;
declare
en student.SID%type;
cursor c is select * from student where SID>=505;
e student%rowtype;
begin
open c;
dbms_output.put_line('Student No------Student Name----------age');
loop
fetch c into e;
exit when c%notfound;
dbms_output.put_line(e.SID||' '||'-----------'||' '||e.sname||'--------------------------'||e.age);
end loop;
end;
output:
4.FUNCTIONS AND PROCEDURES:

Functions are another type of stored code and are very similar to procedures. The Significant difference between is that
function is a pl/sql block which returns a single Value. Functions accept one, many, or no parameters. But a function
must have a return clause in the executable section of the function.
Syntax:
CREATE OR REPLACE FUNCTION functionname [(parameter [, parameter...])] RETURN DATATYPE IS
[Local declarations]
BEGIN
Executable statements
EXCEPTION
Exception handlers
END [name];
Program that implements a function by checking sid values of sailors table
CREATE OR REPLACE FUNCTION sailors_check(id NUMBER) RETURN VARCHAR2 IS
x VARCHAR2(100);
BEGIN
SELECT name INTO x FROM sailors WHERE sid = id;
RETURN 'EXIST';
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN 'NOTEXISTS';
END;
/
OUTPUT:
Function created.

select sailor_check(22)”STATUS” from sailors;


STATUS
-------------
EXIST
select sailor_check(122)”STATUS” from sailors;
STATUS
-------------
NOTEXISTS

Write a PL/SQL Program to implement functions


create or replace function fact(a number) return number is f number: =1;
i number: =1;
begin;
while(i<=a) loop
f:=f*i;
i:=i+1;
end loop;
return f;
end;
/ Function created.

OUTPUT:
SQL> select fact(5) from dual; Fact(5)
120.

You might also like