PLSQL
PLSQL
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('--------------------------------');
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 || ':');
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;
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
BEGIN
-- Check the employee's salary
check_salary(v_employee_salary);
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;
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;