PLSQL
PLSQL
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
Example:
Radius Number := 5;
Date_of_birth date;
Value of c: 70
Value of f: 33.333333333333333333
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;
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 –
Table Created
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
−
. 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.
OUTPUT:
SQL> select fact(5) from dual; Fact(5)
120.