0% found this document useful (0 votes)
6 views15 pages

PLSQL

The document contains a series of PL/SQL programs demonstrating various programming concepts such as loops, exception handling, cursor usage, and triggers. Each program includes declarations, logic for execution, and output statements to display results or handle errors. Key examples include calculating simple interest, printing multiplication tables, and managing employee records with error handling for unique constraints and data retrieval.

Uploaded by

jiyag267
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

PLSQL

The document contains a series of PL/SQL programs demonstrating various programming concepts such as loops, exception handling, cursor usage, and triggers. Each program includes declarations, logic for execution, and output statements to display results or handle errors. Key examples include calculating simple interest, printing multiplication tables, and managing employee records with error handling for unique constraints and data retrieval.

Uploaded by

jiyag267
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

1.

Write a pl/sql program which executes a


loop 3 times using exit statement
DECLARE
counter NUMBER := 0; -- Initialize the counter
BEGIN
LOOP
counter := counter + 1; -- Increment the counter
DBMS_OUTPUT.PUT_LINE('Iteration: ' || counter); -- Output the current iteration

-- Exit the loop if the counter reaches 3


IF counter = 3 THEN
EXIT; -- Exit the loop
END IF;
END LOOP;

DBMS_OUTPUT.PUT_LINE('Loop exited after ' || counter || ' iterations.');


END;
/
2.Write a pl/sql program which executes a
loop 3 times using exit when statement
DECLARE
v_counter NUMBER := 0; -- Initialize a counter variable
BEGIN
LOOP
v_counter := v_counter + 1; -- Increment the counter

-- Output the current value of the counter


DBMS_OUTPUT.PUT_LINE('Iteration: ' || v_counter);

-- Exit the loop when the counter reaches 3


EXIT WHEN v_counter = 3;
END LOOP;

DBMS_OUTPUT.PUT_LINE('Loop has exited after ' || v_counter || ' iterations.');


END;
/
3. Write a pl/sql program to print the series of number
starting from number n

DECLARE
n NUMBER := 5; -- Starting number (you can change this to any number)
limit NUMBER := 10; -- Number of terms to print
v_counter NUMBER; -- Counter variable
BEGIN
-- Loop to print numbers starting from n
FOR v_counter IN 0 .. limit - 1 LOOP
DBMS_OUTPUT.PUT_LINE(n + v_counter);
END LOOP;
END;
/
4. Write a pl/sql program to print the table of number

DECLARE
n NUMBER := 7; -- The number for which the multiplication table will be printed
BEGIN
DBMS_OUTPUT.PUT_LINE('Multiplication Table for ' || n);
DBMS_OUTPUT.PUT_LINE('--------------------------------');

-- Loop to print the multiplication table


FOR i IN 1 .. 10 LOOP
DBMS_OUTPUT.PUT_LINE(n || ' x ' || i || ' = ' || (n * i));
END LOOP;
END;
/
5.Write a pl/sql program to calculate the
simple interest
DECLARE
v_principal NUMBER := 1000; -- Principal amount
v_rate NUMBER := 5; -- Rate of interest (in percentage)
v_time NUMBER := 3; -- Time period (in years)
v_simple_interest NUMBER; -- Variable to store the calculated simple interest
BEGIN
-- Calculate simple interest
v_simple_interest := (v_principal * v_rate * v_time) / 100;

-- Output the result


DBMS_OUTPUT.PUT_LINE('Principal Amount: ' || v_principal);
DBMS_OUTPUT.PUT_LINE('Rate of Interest: ' || v_rate || '%');
DBMS_OUTPUT.PUT_LINE('Time Period: ' || v_time || ' years');
DBMS_OUTPUT.PUT_LINE('Simple Interest: ' || v_simple_interest);
END;
/
6. Write a pl/sql program to print the series

DECLARE
n NUMBER := 5; -- Starting number
step NUMBER := 3; -- Step value for increment
count NUMBER := 10; -- Number of terms to print
v_value NUMBER; -- Variable to hold the current value in the series
BEGIN
DBMS_OUTPUT.PUT_LINE('Series starting from ' || n || ' with step ' || step || ':');

-- Loop to print the series


FOR i IN 0 .. count - 1 LOOP
v_value := n + (i * step); -- Calculate the current value in the series
DBMS_OUTPUT.PUT_LINE(v_value); -- Print the current value
END LOOP;
END;
/
7.Write a pl/sql program to handle
too_many_rows exceptions
DECLARE
v_employee_id NUMBER := 101; -- Example employee ID
v_employee_name VARCHAR2(100);
v_employee_salary NUMBER;
BEGIN
-- Attempt to select employee details
SELECT employee_name, salary
INTO v_employee_name, v_employee_salary
FROM employees
WHERE employee_id = v_employee_id;

-- Output the employee details


DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_employee_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || v_employee_salary);

EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID: ' || v_employee_id);
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Error: More than one employee found with ID: ' ||
v_employee_id);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unexpected error occurred: ' || SQLERRM);
END;
/
8.Write a pl/sql program that shows the
functioning of pragma exception_init
DECLARE
-- Define a user-defined exception
unique_violation EXCEPTION;

-- Associate the user-defined exception with the ORA-00001 error


PRAGMA EXCEPTION_INIT(unique_violation, -1); -- ORA-00001 is represented as -1

v_employee_id NUMBER := 101; -- Example employee ID


v_employee_name VARCHAR2(100) := 'John Doe'; -- Example employee name
BEGIN
-- Attempt to insert a new employee record
INSERT INTO employees (employee_id, employee_name)
VALUES (v_employee_id, v_employee_name);

DBMS_OUTPUT.PUT_LINE('Employee inserted successfully.');

EXCEPTION
WHEN unique_violation THEN
DBMS_OUTPUT.PUT_LINE('Error: Unique constraint violation. Employee ID ' ||
v_employee_id || ' already exists.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unexpected error occurred: ' || SQLERRM);
END;
/
9.Write a pl/sql program to demonstrate the
use of raise_application_error
DECLARE
v_employee_id NUMBER := 101; -- Example employee ID
v_employee_name VARCHAR2(100) := 'John Doe'; -- Example employee name
v_employee_salary NUMBER := 25000; -- Example employee salary
v_min_salary NUMBER := 30000; -- Minimum valid salary
v_max_salary NUMBER := 100000; -- Maximum valid salary

-- Custom procedure to check salary


PROCEDURE check_salary(p_salary NUMBER) IS
BEGIN
IF p_salary < v_min_salary OR p_salary > v_max_salary THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary must be between ' || v_min_salary ||
' and ' || v_max_salary || '.');
END IF;
END check_salary;

BEGIN
-- Check the employee's salary
check_salary(v_employee_salary);

-- If salary is valid, insert the employee record


INSERT INTO employees (employee_id, employee_name, salary)
VALUES (v_employee_id, v_employee_name, v_employee_salary);

DBMS_OUTPUT.PUT_LINE('Employee inserted successfully.');

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
/
10. write a pl/sql program to show cursor
DECLARE
-- Define a cursor to select employee details
CURSOR employee_cursor IS
SELECT employee_id, employee_name, salary
FROM employees;

-- Variables to hold the values fetched from the cursor


v_employee_id employees.employee_id%TYPE;
v_employee_name employees.employee_name%TYPE;
v_salary employees.salary%TYPE;
BEGIN
-- Open the cursor
OPEN employee_cursor;

-- Fetch each row from the cursor


LOOP
FETCH employee_cursor INTO v_employee_id, v_employee_name, v_salary;

-- Exit the loop when no more rows are found


EXIT WHEN employee_cursor%NOTFOUND;

-- Output the employee details


DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_employee_id ||
', Name: ' || v_employee_name ||
', Salary: ' || v_salary);
END LOOP;

-- Close the cursor


CLOSE employee_cursor;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
-- Ensure the cursor is closed in case of an error
IF employee_cursor%ISOPEN THEN
CLOSE employee_cursor;
END IF;
END;
/
11. write a pl/sql program to show implicit
cursor
DECLARE
v_employee_id NUMBER := 1; -- Example employee ID
v_employee_name VARCHAR2(100);
v_salary NUMBER;
BEGIN
-- Perform a SELECT statement to retrieve employee details
SELECT employee_name, salary
INTO v_employee_name, v_salary
FROM employees
WHERE employee_id = v_employee_id;

-- Output the employee details


DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_employee_id);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_employee_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || v_salary);

EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID: ' || v_employee_id);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unexpected error occurred: ' || SQLERRM);
END;
/
12. write a pl/sql program to show explicit
cursor
DECLARE
-- Define an explicit cursor to select employee details
CURSOR employee_cursor IS
SELECT employee_id, employee_name, salary
FROM employees;

-- Variables to hold the values fetched from the cursor


v_employee_id employees.employee_id%TYPE;
v_employee_name employees.employee_name%TYPE;
v_salary employees.salary%TYPE;
BEGIN
-- Open the cursor
OPEN employee_cursor;

-- Fetch each row from the cursor


LOOP
FETCH employee_cursor INTO v_employee_id, v_employee_name, v_salary;

-- Exit the loop when no more rows are found


EXIT WHEN employee_cursor%NOTFOUND;

-- Output the employee details


DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_employee_id ||
', Name: ' || v_employee_name ||
', Salary: ' || v_salary);
END LOOP;

-- Close the cursor


CLOSE employee_cursor;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
-- Ensure the cursor is closed in case of an error
IF employee_cursor%ISOPEN THEN
CLOSE employee_cursor;
END IF;
END;
/
13. Write a pl/sql program to show trigger
CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
employee_name VARCHAR2(100),
salary NUMBER,
last_updated TIMESTAMP
);
CREATE OR REPLACE TRIGGER trg_update_last_updated
BEFORE UPDATE ON employees
FOR EACH ROW
BEGIN
:NEW.last_updated := SYSTIMESTAMP; -- Set the last_updated column to the current
timestamp
END;
/
-- Insert a sample employee record
INSERT INTO employees (employee_id, employee_name, salary, last_updated)
VALUES (1, 'Alice', 50000, SYSTIMESTAMP);

-- Update the employee's salary


UPDATE employees
SET salary = 55000
WHERE employee_id = 1;

-- Select the employee record to see the last_updated timestamp


SELECT * FROM employees WHERE employee_id = 1;

You might also like