Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views16 pages

PLQL Practicals

The document provides an overview of PL/SQL, covering its basic concepts, variable usage, control structures, conditional statements, and cursors. It includes examples of how to declare variables, write executable statements, and implement loops and conditional logic. Additionally, it explains the types of cursors in PL/SQL, including implicit, explicit, and parameterized cursors, along with their usage and features.

Uploaded by

imtishal_ali3263
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)
12 views16 pages

PLQL Practicals

The document provides an overview of PL/SQL, covering its basic concepts, variable usage, control structures, conditional statements, and cursors. It includes examples of how to declare variables, write executable statements, and implement loops and conditional logic. Additionally, it explains the types of cursors in PL/SQL, including implicit, explicit, and parameterized cursors, along with their usage and features.

Uploaded by

imtishal_ali3263
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/ 16

PL/SQL – PRACTICAL No – 1

PL/SQL Basics

What is PL/SQL?

PL/SQL (Procedural Language for SQL) is an extension of SQL provided by Oracle that
allows procedural programming. It combines SQL with procedural features like loops,
conditions, and exception handling.

1. Use of Variables in PL/SQL

Variables store and manipulate data in PL/SQL. They are declared in the DECLARE
section and used in the BEGIN section.

Declaring Variables:

DECLARE

v_name VARCHAR2(100);

v_salary NUMBER;

BEGIN

v_name := 'Robert';

v_salary := 85000;

-- Display output

DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_name);

DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);

END;

2. Writing Executable Statements in PL/SQL

PL/SQL blocks consist of three sections:

1. Declaration Section – Variables are declared here.

2. Executable Section – Contains SQL queries, loops, and logic.


3. Exception Handling Section – Handles errors.

Example:

DECLARE

TYPE emp_record IS RECORD (

emp_id VARCHAR2(10),

emp_name VARCHAR2(100),

emp_salary NUMBER

);

employee emp_record;

BEGIN

employee.emp_id := 'E001';

employee.emp_name := 'John Doe';

employee.emp_salary := 50000;

-- Display employee details

DBMS_OUTPUT.PUT_LINE('Employee ID: ' || employee.emp_id);

DBMS_OUTPUT.PUT_LINE('Employee Name: ' || employee.emp_name);

DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || employee.emp_salary);

END;

3. Interacting with the Oracle Server

PL/SQL allows executing SQL statements within a block:

• SELECT – Retrieves data

• INSERT, UPDATE, DELETE – Modifies data

• COMMIT – Saves changes

• ROLLBACK – Reverts changes

Example:

BEGIN
INSERT INTO employees (id, name, salary) VALUES (101, 'Alice', 60000);

COMMIT;

END;

4. Creating an Anonymous PL/SQL Block

An anonymous block is a PL/SQL block that runs without a name and isn’t stored in the
database.

Example:

DECLARE

v_name VARCHAR2(100);

v_salary NUMBER;

BEGIN

v_name := 'Robert';

v_salary := 85000;

-- Display output

DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_name);

DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);

END;

5. Sequences in PL/SQL

A sequence generates unique numbers automatically, often used for primary keys.

Creating a Sequence:

CREATE SEQUENCE emp_seq

START WITH 1

INCREMENT BY 1

MINVALUE 1

MAXVALUE 5
NOCYCLE;

Using a Sequence in a Table:

CREATE TABLE tea (

empid NUMBER(10) PRIMARY KEY,

empname VARCHAR2(50)

);

INSERT INTO tea VALUES (emp_seq.NEXTVAL, 'Wasim');

INSERT INTO tea VALUES (emp_seq.NEXTVAL, 'Arif');

INSERT INTO tea VALUES (emp_seq.NEXTVAL, 'Azim');

SELECT * FROM tea;

Dropping a Sequence:

DROP SEQUENCE emp_seq;


PL/SQL – PRACTICAL No – 2

Control Structures in PL/SQL

Control structures help in executing repetitive tasks and decision-making in PL/SQL.

1. WHILE Loop

The WHILE loop executes a block of code as long as a condition is TRUE.

Syntax:

WHILE condition LOOP

-- Code to execute

END LOOP;

Example: Print numbers from 1 to 5

DECLARE

v_num NUMBER := 1;

BEGIN

WHILE v_num <= 5 LOOP

DBMS_OUTPUT.PUT_LINE('Number: ' || v_num);

v_num := v_num + 1;

END LOOP;

END;

2. DO Loop (LOOP-END LOOP)

The DO loop (LOOP-END LOOP) runs at least once and continues until an EXIT
condition is met.

Syntax:

LOOP

-- Code to execute

EXIT WHEN condition;


END LOOP;

Example: Print numbers from 1 to 5

DECLARE

v_num NUMBER := 1;

BEGIN

LOOP

DBMS_OUTPUT.PUT_LINE('Number: ' || v_num);

v_num := v_num + 1;

EXIT WHEN v_num > 5;

END LOOP;

END;

3. FOR Loop

The FOR loop runs a specific number of times based on a given range.

Syntax:

FOR counter IN start_value..end_value LOOP

-- Code to execute

END LOOP;

Example: Print numbers from 1 to 5

BEGIN

FOR v_num IN 1..5 LOOP

DBMS_OUTPUT.PUT_LINE('Number: ' || v_num);

END LOOP;

END;

4. GOTO Statement

The GOTO statement jumps to a specific labeled section in the program.


Syntax:

GOTO label;

<<label>>

-- Code to execute

Example: Skip printing 3

DECLARE

v_num NUMBER := 0;

BEGIN

LOOP

v_num := v_num + 1;

IF v_num = 3 THEN

GOTO skip;

END IF;

DBMS_OUTPUT.PUT_LINE('Number: ' || v_num);

EXIT WHEN v_num = 5;

<<skip>>

END LOOP;

END;
PL/SQL – PRACTICAL No – 3
PL/SQL Conditional Statements

• IF statement

• IF-ELSE statement

• ELSIF ladder

• CASE expression

1. IF Statement

The IF statement executes code only if a condition is TRUE.

Syntax:

IF condition THEN

-- Statements to execute

END IF;

Example: Check if sales exceed 100K

DECLARE

n_sales NUMBER := 2000000;

BEGIN

IF n_sales > 100000 THEN

DBMS_OUTPUT.PUT_LINE('Sales revenue is greater than 100K');

END IF;

END;

Output:

Sales revenue is greater than 100K

2. IF-ELSE Statement

Executes one block of code if the condition is TRUE and another block if FALSE.

Syntax:

IF condition THEN
-- Code if condition is TRUE

ELSE

-- Code if condition is FALSE

END IF;

Example: Check if a number is less than 20

DECLARE

a NUMBER(3) := 500;

BEGIN

IF a < 20 THEN

DBMS_OUTPUT.PUT_LINE('a is less than 20');

ELSE

DBMS_OUTPUT.PUT_LINE('a is not less than 20');

END IF;

DBMS_OUTPUT.PUT_LINE('Value of a is: ' || a);

END;

Output:

a is not less than 20

Value of a is: 500

3. ELSIF Ladder

Allows multiple conditions to be checked sequentially.

Syntax:

IF condition1 THEN

-- Code for condition1

ELSIF condition2 THEN

-- Code for condition2

ELSIF condition3 THEN

-- Code for condition3


ELSE

-- Default code if no conditions match

END IF;

Example: Assign letter grade based on score

DECLARE

v_Score NUMBER := 85;

v_LetterGrade CHAR(1);

BEGIN

IF v_Score >= 90 THEN

v_LetterGrade := 'A';

ELSIF v_Score >= 80 THEN

v_LetterGrade := 'B';

ELSIF v_Score >= 70 THEN

v_LetterGrade := 'C';

ELSIF v_Score >= 60 THEN

v_LetterGrade := 'D';

ELSE

v_LetterGrade := 'E';

END IF;

DBMS_OUTPUT.PUT_LINE('Your Letter Grade is: ' || v_LetterGrade);

END;

Output:

Your Letter Grade is: B

4. CASE Expression

The CASE statement works like an IF-ELSE ladder but uses a selector (expression)
instead of multiple Boolean conditions.
Syntax:

CASE selector

WHEN value1 THEN statement1

WHEN value2 THEN statement2

ELSE default_statement

END CASE;

Example: Print grade description

DECLARE

grade CHAR(1) := 'A';

BEGIN

CASE grade

WHEN 'A' THEN DBMS_OUTPUT.PUT_LINE('Excellent');

WHEN 'B' THEN DBMS_OUTPUT.PUT_LINE('Very good');

WHEN 'C' THEN DBMS_OUTPUT.PUT_LINE('Well done');

WHEN 'D' THEN DBMS_OUTPUT.PUT_LINE('You passed');

WHEN 'F' THEN DBMS_OUTPUT.PUT_LINE('Better try again');

ELSE DBMS_OUTPUT.PUT_LINE('No such grade');

END CASE;

END;

Output:

Excellent
PL/SQL – PRACTICAL No – 3
Aim:

To create and use different types of cursors in PL/SQL:

• Implicit Cursor

• Explicit Cursor

• Parameterized Cursor

Introduction to Cursors in PL/SQL

A cursor is a pointer to a memory area known as the context area, which holds the
result of an SQL query. Cursors allow row-by-row processing of query results in PL/SQL.

1. Implicit Cursor

PL/SQL automatically creates an implicit cursor for SQL statements like INSERT,
UPDATE, DELETE, and SELECT INTO when no explicit cursor is used.

Example: Implicit Cursor

Step 1: Create a Table and Insert Data

CREATE TABLE exp5(

ID INT PRIMARY KEY,

NAME VARCHAR(20) NOT NULL,

AGE INT NOT NULL,

ADDRESS CHAR(25),

SALARY DECIMAL(18,2)

);

INSERT INTO exp5 VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00);

INSERT INTO exp5 VALUES (2, 'Khilan', 25, 'Delhi', 1500.00);

INSERT INTO exp5 VALUES (3, 'Kaushik', 23, 'Kota', 2000.00);

INSERT INTO exp5 VALUES (4, 'Chaitali', 25, 'Mumbai', 6500.00);


INSERT INTO exp5 VALUES (5, 'Hardik', 27, 'Bhopal', 8500.00);

INSERT INTO exp5 VALUES (6, 'Komal', 22, 'MP', 4500.00);

Step 2: Implicit Cursor Example (Updating Salaries)

DECLARE

total_row NUMBER(2);

BEGIN

UPDATE exp5

SET salary = salary + 3000;

IF SQL%NOTFOUND THEN

DBMS_OUTPUT.PUT_LINE('No customers updated');

ELSIF SQL%FOUND THEN

total_row := SQL%ROWCOUNT;

DBMS_OUTPUT.PUT_LINE(total_row || ' customers updated');

END IF;

END;

Output:

Statement processed.

6 customers updated

Key Features of Implicit Cursors:

• Automatically created by PL/SQL.

• Used when a single-row query is processed.

• Does not require OPEN, FETCH, CLOSE commands.

• Can be monitored using SQL%FOUND, SQL%NOTFOUND, SQL%ROWCOUNT.

2. Explicit Cursor
An explicit cursor is manually declared, opened, fetched, and closed to handle
multiple rows from a query.

Steps for Using an Explicit Cursor:

1. Declare the cursor with a SELECT statement.

2. Open the cursor to allocate memory.

3. Fetch rows into variables.

4. Close the cursor to release memory.

Example: Explicit Cursor

DECLARE

id exp5.id%TYPE;

name exp5.name%TYPE;

add exp5.address%TYPE;

CURSOR a IS

SELECT id, name, address FROM exp5; -- Cursor declaration

BEGIN

OPEN a; -- Open cursor

LOOP

FETCH a INTO id, name, add; -- Fetch data

EXIT WHEN a%NOTFOUND; -- Exit when no more rows

DBMS_OUTPUT.PUT_LINE(id || ' ' || name || ' ' || add);

END LOOP;

CLOSE a; -- Close cursor

END;

/
Output:

1 Ramesh Ahmedabad

2 Khilan Delhi

3 Kaushik Kota

...

Key Features of Explicit Cursors:

• Used for multi-row queries.

• Requires OPEN, FETCH, CLOSE commands.

• Can be monitored using %FOUND, %NOTFOUND, %ROWCOUNT.

3. Parameterized Cursor

A parameterized cursor allows passing arguments to filter data dynamically when it is


opened.

Example: Parameterized Cursor (Filtering Employees by Salary)

DECLARE

record exp5%ROWTYPE;

CURSOR c1 (max_wage NUMBER) IS

SELECT * FROM exp5 WHERE salary < max_wage;

BEGIN

OPEN c1(5000); -- Open cursor with max salary filter

LOOP

FETCH c1 INTO record;

EXIT WHEN c1%NOTFOUND;

DBMS_OUTPUT.PUT_LINE('Name = ' || record.name || ', Salary = ' || record.salary);

END LOOP;

CLOSE c1;
END;

Output:

Name = Ramesh, Salary = 2000.00

Name = Khilan, Salary = 1500.00

Name = Kaushik, Salary = 2000.00

...

Key Features of Parameterized Cursors:

• Allows filtering of data dynamically.

• Requires parameters while opening the cursor.

Summary

Cursor Type Description Example Use Case

Automatically created for SELECT COUNT(*) INTO v_count


Implicit Cursor
simple queries FROM employees;

Manually declared for multi-


Explicit Cursor Iterating through customer records
row queries

Parameterized Accepts parameters for Fetching employees with salary <


Cursor dynamic queries 5000

You might also like