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

PLSQL

PL/SQL is a combination of SQL and procedural language features developed by Oracle. It allows developers to perform operations like conditional control and loops. PL/SQL code is organized into anonymous blocks with three sections: declaration, execution, and exception handling. It offers advantages like nested blocks, error handling, and better performance over SQL.

Uploaded by

inianece
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

PLSQL

PL/SQL is a combination of SQL and procedural language features developed by Oracle. It allows developers to perform operations like conditional control and loops. PL/SQL code is organized into anonymous blocks with three sections: declaration, execution, and exception handling. It offers advantages like nested blocks, error handling, and better performance over SQL.

Uploaded by

inianece
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

What is PL/SQL?

PL/SQL is a combination of SQL along


with the procedural features of programming
languages. It was developed by Oracle
Corporation .
A Simple PL/SQL Block:

• A PL/SQL Block consists of three sections:


• The Declaration section (optional).
• The Execution section (mandatory).
• The Exception (or Error) Handling section (optional).

SYNTAX
DECLARE
     Variable,Constant,Records & Cursor declaration
BEGIN
     SQL , Loop,Condetional Statement
EXCEPTION
     Exception handling
END;
/ EXECUTE THE ABOVE PL/SQL BLOCK
Advantages of PL/SQL

• Block Structures: PL SQL consists of blocks of code,


which can be nested within each other. PL/SQL Blocks can
be stored in the database and reused.
• Procedural Language Capability: PL SQL consists of
procedural language constructs such as conditional
statements (if else statements) and loops like (FOR loops).
• Better Performance: PL SQL engine processes multiple
SQL statements simultaneously as a single block, thereby
reducing network traffic.
• Error Handling: PL/SQL handles errors or exceptions
effectively during the execution of a PL/SQL program
PL/SQL Placeholders

• Placeholders are temporary storage area. Placeholders


can be any of Variables, Constants and Records.
• Few of the datatypes used to define placeholders are as
given below.
Number (n,m) , Char (n) , Varchar2 (n) , Date , Long ,
Long raw, Raw, Blob, Clob, Nclob, Bfile
PL/SQL Variables

• General Syntax to declare a variable is:

variable_name datatype [NOT NULL := value ];

DECLARE
  var_salary number(6);
 var_emp_id number(6) = 1116;
BEGIN  
SELECT salary  INTO var_salary  FROM employee  WHERE emp_id =
var_emp_id;  
dbms_output.put_line(var_salary);  dbms_output.put_line('The employee '
|| var_emp_id || ' has salary ' || var_salary);
END;
/
Scope of Variables

• Local variables
• Global variables

DECLARE
var_num1 number;
  var_num2 number;
BEGIN
  var_num1 := 100;
 var_num2 := 200;
 DECLARE
   var_mult number;
 BEGIN
   var_mult := var_num1 * var_num2;
  END;
END;
/
PL/SQL Constants

• General Syntax :
constant_name CONSTANT datatype := VALUE;

DECLARE  
salary_increase CONSTANT number(3);
BEGIN
  salary_increase := 100;
dbms_output.put_line(salary_increase);
END;
/
PL/SQL Records

• Records are composite datatypes, which means it is a


combination of different scalar datatypes like char, varchar,
number etc.  Each scalar data types in the record holds a
value. A record can be visualized as a row of data.
• General Syntax :
a emp%rowtype
We can access the record like (a.ename,a.sal,a.job,…)
a emp.ename%type
we can access only employee name field.
Conditional Statements in PL/SQL

• The programming constructs are similar to how you use in


programming languages like Java and C++
IF
condition 1 THEN  
statement 1;  
statement 2;
ELSIF
condtion2 THEN  
statement 3;
ELSE  statement 4;
END IF
Iterative Statements in PL/SQL

• Three types of loops in PL/SQL:


• Simple Loop
• While Loop
• For Loop

• Basic loop strecture :


loop
/* statements */
end loop;
Simple Loop

• General Syntax :

LOOP
statements;
EXIT;
{or EXIT WHEN condition;}
END LOOP;
PL/SQL WHILE Loop
declare
v_test varchar2(8) := 'RUN';
n_numb number := 2;
begin
while v_test <> 'STOP' loop
if n_numb > 5
then v_test := 'STOP';
end if;
dbms_output.put_line (v_test||': '||n_numb);
n_numb := n_numb + 1;
end loop;
v_test := 'DOWN';
while n_numb > 1 AND v_test = 'DOWN' loop
dbms_output.put_line (v_test||': '||n_numb);
n_numb := n_numb - 1;
end loop;
while 7 = 4 loop
NULL; -- never get here
end loop;
end;
/
For Loop
declare
     idx number := 100
begin
  dbms_output.put_line (idx);
     for idx in 2..5 loop
       dbms_output.put_line (idx);
  end loop;
      dbms_output.put_line (idx);
end;
   /
What are 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.
• 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.
Parameters:SQL%FOUND , SQL%NOTFOUND , SQL%ROWCOUNT
• 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.
Implicit Cursors:

DECLARE
var_rows number(5);
BEGIN
UPDATE employee SET salary = salary + 1000;
IF SQL%NOTFOUND THEN
dbms_output.put_line('None of the salaries where updated');
ELSIF SQL%FOUND THEN
var_rows := SQL%ROWCOUNT;
dbms_output.put_line('Salaries for ' || var_rows || 'employees are
updated');
END IF;
END;
/
Explicit Cursors
• There are four steps in using an Explicit Cursor.
DECLARE the cursor in the declaration section.
OPEN the cursor in the Execution Section.
FETCH the data from cursor into PL/SQL variables or records in the
Execution Section.
CLOSE the cursor in the Execution Section before you end the PL/SQL
Block.
DECLARE
variables;
records;
create a cursor;
BEGIN
OPEN cursor;
FETCH cursor;
process the records;
CLOSE cursor;
END;
Explicit Cursors Example
DECLARE
emp_rec emp_tbl%rowtype;
CURSOR emp_cur IS
SELECT * FROM
WHERE salary > 1000;
BEGIN
OPEN emp_cur;
FETCH emp_cur INTO emp_rec;
  dbms_output.put_line (emp_rec.first_name || '  ' ||
emp_rec.last_name);
  CLOSE emp_cur;
END;
/

You might also like