DBMS - PLSQL
DBMS - PLSQL
X+2Y-3Z+2W=2
2X+5Y-8Z+6W=5
3X+4Y-5Z+2W=4
DECLARE
det NUMBER;
detX NUMBER;
detY NUMBER;
detZ NUMBER;
detW NUMBER;
x NUMBER;
y NUMBER;
z NUMBER;
w NUMBER;
BEGIN
c1 * (a2 * b3 - b2 * d3);
r2 * (b1 * d3 - b3 * d1) +
r3 * (b1 * d2 - b2 * d1);
a2 * (r1 * d3 - r3 * d1) +
a3 * (r1 * d2 - r2 * d1);
b1 * (a2 * r3 - a3 * r2) +
c1 * (a2 * b3 - a3 * b2);
c1 * (a2 * r3 - b3 * c3) +
d1 * (b4 * )
IF det != 0 THEN
x := detX / det;
y := detY / det;
z := detZ / det;
DBMS_OUTPUT.PUT_LINE('Solution:');
ELSE
END;
employee_name VARCHAR2(100),
salary NUMBER,
last_modified DATE
);
BEGIN
NEW.last_modified := SYSDATE;
END;
UPDATE employees
WHERE employee_id = 1;
FROM employees
WHERE employee_id = 1;
3. Write a PL/SQL program to arrange the number of two variable in such a way that the
small number will store in num_small variable and large number will store in num_large
variable.
DECLARE
num_small NUMBER;
num_large NUMBER;
BEGIN
num_small := num1;
num_large := num2;
ELSE
num_small := num2;
num_large := num1;
END IF;
END;
3x-7y=4
-9x+21y=4
1. DECLARE
x NUMBER;
y NUMBER;
BEGIN
-- Solve 3x - 7y = 4
y := (3 * x - 4) / 7;
END;
5. Write a PL/SQL procedure to calculate the incentive on a target achieved and display
the message either the record updated or not.
employee_name VARCHAR2(100),
target NUMBER,
actual NUMBER,
incentive_amount NUMBER
);
COMMIT;
IS
incentive NUMBER;
rows_updated NUMBER;
BEGIN
ELSE
END IF;
UPDATE employees
rows_updated := SQL%ROWCOUNT;
ELSE
END IF;
END;
emp_name VARCHAR2(50),
emp_salary NUMBER,
last_modified DATE
);
BEGIN
:NEW.last_modified := SYSDATE;
END;
UPDATE employees
WHERE emp_id = 1;
5x + 15y =14x - 7y
-9x = -22y
22y = 9x
y = (9/22)x
DECLARE
x NUMBER;
y NUMBER;
BEGIN
x := 22;
y := (9/22) * x;
END;
DECLARE
n NUMBER := 64;
r NUMBER;
BEGIN
r := MOD(n, 2);
IF r = 0 THEN
ELSE
END IF;
END;
9. Write a code in PL/SQL to create a trigger that prevents updates on a certain column
during specific hours of the day.
order_date TIMESTAMP,
order_amount NUMBER
);
DECLARE
current_hour NUMBER;
BEGIN
END IF;
END;
x+2y-3z+2w=2,
2x+5y-8z+6z=5,
3x+4y-5z+2w=4.
DECLARE
x NUMBER;
y NUMBER;
z NUMBER;
w NUMBER;
temp1 NUMBER;
temp2 NUMBER;
temp3 NUMBER;
BEGIN
y := 1;
z := -1;
w := 1;
DBMS_OUTPUT.PUT_LINE('Solution:');
END;
/
11. Write a PL/SQL procedure to calculate the incentive on a specific target otherwise a
general incentive to be paid using IF-THEN-ELSE.
12. Write a code in PL/SQL to implement a trigger that maintains a transaction history
log whenever a row is deleted from a table.
14. Write a PL/SQL program to check whether a date falls on weekend i.e. SATURDAY or
SUNDAY.
DECLARE
day_of_week NUMBER;
BEGIN
ELSE
END IF;
END;
15. Write a code in PL/SQL to develop a trigger that enforces referential integrity by
preventing the deletion of a parent record if child records exist.
DECLARE
num NUMBER := #
i NUMBER := 2;
BEGIN
is_prime := FALSE;
ELSE
IF MOD(num, i) = 0 THEN
is_prime := FALSE;
EXIT;
END IF;
i := i + 1;
END LOOP;
END IF;
IF is_prime THEN
ELSE
END IF;
END;
17. Write a PL/SQL procedure to calculate incentive achieved according to the specific
sale limit.
DECLARE
IS
incentive NUMBER := 0;
BEGIN
incentive := 1800;
incentive := 800;
ELSE
incentive := 500;
END IF;
DBMS_OUTPUT.NEW_LINE;
DBMS_OUTPUT.PUT_LINE (
);
END test1;
BEGIN
test1(45000);
test1(36000);
test1(28000);
END;
18. Write a code in PL/SQL to create a trigger that checks for duplicate values in a
specific column and raises an exception if found.
CREATE TABLE employees (
employee_name VARCHAR2(100),
department_id NUMBER(3)
);
DECLARE
duplicate_count NUMBER;
BEGIN
FROM employees
END IF;
END;
DECLARE
BEGIN
ELSE
factorial := factorial * i;
END LOOP;
END IF;
END;
20. Write a PL/SQL program to count number of employees in department 50 and check
whether this department have any vacancies or not. There are 45 vacancies in this
department.
employee_name VARCHAR2(50),
department_id NUMBER,
salary NUMBER
);
INSERT INTO employees (employee_id, employee_name, department_id, salary) VALUES
(1, 'John Doe', 50, 50000);
DECLARE
emp_count NUMBER;
BEGIN
SELECT COUNT(*)
INTO emp_count
FROM employees
ELSE
END IF;
END;
21. Write a code in PL/SQL to create a trigger that restricts the insertion of new rows if
the total of a column's values exceeds a certain threshold.
sale_amount NUMBER(10, 2)
);
DECLARE
BEGIN
END IF;
END;
/
22. PL/SQL Program to Print Table of a Number
DECLARE
n NUMBER;
BEGIN
n := 5;
END LOOP;
END;
DECLARE
Grade CHAR(1);
Des VARCHAR2(50);
BEGIN
Grade := 'A';
CASE Grade
Des := 'Excellent';
Des := 'Good';
Des := 'Fail';
ELSE
END CASE;
END;
DECLARE
description VARCHAR2(20)
BEGIN
CASE
END CASE;
END;
24. Write a code in PL/SQL to design a trigger that captures changes made to specific
columns and logs them in an audit table.
name VARCHAR2(100),
salary NUMBER
);
employee_id NUMBER,
old_salary NUMBER,
new_salary NUMBER,
changed_by VARCHAR2(50)
);
BEGIN
END;
INSERT INTO employees (employee_id, name, salary) VALUES (1, 'John Doe', 50000);
INSERT INTO employees (employee_id, name, salary) VALUES (2, 'Jane Smith', 60000);
COMMIT;
COMMIT;
BEGIN
END LOOP;
END;
DECLARE
remainder NUMBER;
BEGIN
END LOOP;
END;
26. Write a PL/SQL program to count number of employees in a specific department and
check whether this department have any vacancies or not. If any vacancies, how many
vacancies are in that department.
employee_name VARCHAR2(50),
department_id NUMBER,
salary NUMBER
);
DECLARE
emp_count NUMBER;
BEGIN
SELECT COUNT(*)
INTO emp_count
FROM employees
END IF;
END
27. Write a code in PL/SQL to implement a trigger that records user activity (inserts,
updates, deletes) in an audit log for a given set of tables.
table_name VARCHAR2(100),
operation VARCHAR2(10),
user_name VARCHAR2(30),
timestamp TIMESTAMP,
old_data CLOB,
new_data CLOB,
primary_key_value VARCHAR2(100)
);
first_name VARCHAR2(50),
last_name VARCHAR2(50),
department VARCHAR2(50)
);
DECLARE
v_operation VARCHAR2(10);
v_old_data CLOB;
v_new_data CLOB;
v_pk_value VARCHAR2(100);
BEGIN
IF INSERTING THEN
v_operation := 'INSERT';
v_operation := 'UPDATE';
v_operation := 'DELETE';
END IF;
v_pk_value := :OLD.employee_id; -- Adjust to match your primary key column
table_name,
operation,
user_name,
timestamp,
old_data,
new_data,
primary_key_value
) VALUES (
'employees',
v_operation,
USER,
SYSTIMESTAMP,
v_old_data,
v_new_data,
v_pk_value
);
END;
UPDATE employees
WHERE employee_id = 1;
WHERE employee_id = 2;
SELECT *
FROM audit_log
DECLARE
BEGIN
END LOOP;
END;
/
29. Write a PL/SQL program to display the description against a grade using CASE
statement.
DECLARE
v_grade CHAR(1);
v_description VARCHAR2(100);
BEGIN
v_grade = 'B';
END;
END;
30. Write a code in PL/SQL to implement a trigger that automatically calculates and
updates a running total column for a table whenever new rows are inserted.
sale_amount NUMBER,
running_total NUMBER
);
DECLARE
v_total NUMBER;
BEGIN
INTO v_total
declare
str1 varchar2(50):='&str';
str2 varchar2(50);
len number;
i number;
begin
len:=length(str1);
loop
str2:=str2 || substr(str1,i,1);
end loop;
end;
DECLARE
str1 varchar2(50):='&str';
str2 varchar2(50);
len number;
i number;
BEGIN
len:=length(str1);
loop
str2:=str2 || substr(str1,i,1);
END loop;
END;
/
32. Write a PL/SQL program to display the description against a grade using CASE
statement with EXCEPTION.
DECLARE
grd CHAR(1);
BEGIN
grd := '&new_grd';
CASE
END CASE;
EXCEPTION
END;
33. Write a code in PL/SQL to create a trigger that validates the availability of items
before allowing an order to be placed, considering stock levels and pending orders.
DECLARE
v_available_stock NUMBER;
v_pending_orders NUMBER;
BEGIN
FROM orders
END IF;
END;
DECLARE
v_fib1 NUMBER := 0;
v_fib2 NUMBER := 1;
v_fib_next NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('Fibonacci Series:');
DBMS_OUTPUT.PUT_LINE(v_fib1);
v_fib1 := v_fib2;
v_fib2 := v_fib_next;
END LOOP;
END;
35. Write a PL/SQL program to check whether a given number is positive, negative or
zero.
DECLARE
v_number NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('Enter a number:');
v_number := &user_input;
ELSE
END IF;
END;
/
36. Design a trigger that sends an email notification to a predefined address whenever
an error occurs during a specific operation
DECLARE
num NUMBER;
result VARCHAR2(10);
BEGIN
num := 10;
IF MOD(num, 2) = 0 THEN
result := 'Even';
ELSE
result := 'Odd';
END IF;
END;
38. Write a PL/SQL program to check whether a given character is letter or digit.
DECLARE
input_char CHAR(1);
is_letter BOOLEAN;
is_digit BOOLEAN;
BEGIN
input_char := 'A';
is_letter := (input_char BETWEEN 'A' AND 'Z') OR (input_char BETWEEN 'a' AND 'z');
IF is_letter THEN
ELSE
END IF;
END;
39. Write a PL/SQL block to handle the exception when a division by zero occurs.
DECLARE
denominator NUMBER := 2;
result NUMBER;
BEGIN
EXCEPTION
WHEN ZERO_DIVIDE THEN
END;
DECLARE
reversed_string :=''
VARCHAR2(100);
i NUMBER;
BEGIN
reversed_string := ''
END LOOP;
END;
Output
reversed_string VARCHAR2(10):=’ ‘;
BEGIN
LOOP
reversed_string:=reversed_string ||
END LOOP;
DBMS_OUTPUT.PUT_LINE(‘original_
DBMS_OUTPUT.PUT_LINE(‘reversed_
END;
41. Write a PL/SQL program to convert a temperature in scale Fahrenheit to Celsius and
vice versa.
DECLARE
v_celsius NUMBER;
BEGIN
42. Handle the NO_DATA_FOUND exception when retrieving a row from a table and no
matching record is found.
DELIMITER $$
BEGIN
FROM employees
ELSE
END IF;
END$$
DELIMITER
DECLARE
original_number NUMBER;
reversed_number NUMBER := 0;
remainder NUMBER;
temp_number NUMBER;
BEGIN
-- Input: You can replace this with any number you want to check
temp_number := original_number;
END LOOP;
ELSE
END IF;
END;
DECLARE
input_date DATE;
day_of_week VARCHAR2(20);
BEGIN
day_of_week := TRIM(day_of_week);
EXCEPTION
END;
45. Handle the TOO_MANY_ROWS exception when retrieving multiple rows instead of a
single row from a table.
BEGIN
reversed_num := TO_CHAR(i);
reversed_num := REVERSE(reversed_num);
palindrome_found := TRUE;
END IF;
END LOOP;
END IF;
END generate_palindrome_series
BEGIN
generate_palindrome_series(1, 100);
END;
47. Write a program in PL/SQL to print the value of a variable inside and outside a loop
using LOOP EXIT statement.
DECLARE
input_string VARCHAR2(100);
numeric_value NUMBER;
BEGIN
input_string := '1234.56';
numeric_value := TO_NUMBER(input_string)
EXCEPTION
END;
DECLARE
counter NUMBER := 1;
BEGIN
LOOP
counter := counter + 1;
EXIT;
END IF;
END LOOP;
END;
declare
num1 number;
num2 number;
temp number;
begin
num1:=1000;
num2:=2000;
dbms_output.put_line('before');
temp := num1;
num1 := num2;
num2 := temp;
dbms_output.put_line('after');
end;
50. Write a program in PL/SQL to print the value of a variable inside and outside a loop
using LOOP WHEN EXIT statement.
DECLARE
n NUMBER := 0;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE ('The value of n inside the loop is: ' ||
TO_CHAR(n));
n := n + 1;
IF n > 5 THEN
EXIT;
END IF;
END LOOP;
END;
51. Handle the DUP_VAL_ON_INDEX exception when inserting a duplicate value into a
table with a unique constraint
name varchar(20));
DECLARE
BEGIN
EXCEPTION
END;
DECLARE
temp NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('Before Swapping:');
DBMS_OUTPUT.PUT_LINE('num1 = ' || num1 || ', num2 = ' || num2 || ', num3 = ' || num3);
-- Swapping logic
temp := num1;
num1 := num2;
num2 := num3;
num3 := temp;
DBMS_OUTPUT.PUT_LINE('After Swapping:');
DBMS_OUTPUT.PUT_LINE('num1 = ' || num1 || ', num2 = ' || num2 || ', num3 = ' || num3);
END;
DECLARE
outer_counter NUMBER := 1;
inner_counter NUMBER;
BEGIN
inner_counter := 1;
-- Inner loop runs 2 times for each iteration of the outer loop
WHILE inner_counter <= 2 LOOP
inner_counter := inner_counter + 1;
END LOOP;
outer_counter := outer_counter + 1;
END LOOP;
END;
DECLARE
num NUMBER;
BEGIN
BEGIN
EXCEPTION
END;
-- Continue the rest of the program after handling the exception
END;
DECLARE
n1 NUMBER(5);
s NUMBER(5) := 0;
m NUMBER(5);
r NUMBER(5);
BEGIN
n1 := &n;
m := n1;
r := MOD(n1, 10);
s := s + (r * r * r);
n1 := TRUNC(n1 / 10);
END LOOP;
IF s = m THEN
ELSE
DBMS_OUTPUT.PUT_LINE('The Given Number ' || m || ' is Not an Armstrong
Number');
END IF;
END;
56. Write a program in PL/SQL to update the salary of a specifc employee by 8% if the
salary exceeds the mid range of the salary against this job and update up to mid range if
the salary is less than 'The
COMMIT;
SET SERVEROUTPUT ON
DECLARE
v_salary NUMBER;
v_new_salary NUMBER;
BEGIN
SELECT salary INTO v_salary
FROM employees
UPDATE employees
v_new_salary := v_mid_salary;
UPDATE employees
ELSE
COMMIT;
END;
COMMIT;
SET SERVEROUTPUT ON
DECLARE
v_salary NUMBER;
v_new_salary NUMBER;
BEGIN
UPDATE employees
v_new_salary := v_mid_salary;
UPDATE employees
ELSE
END IF;
COMMIT;
END;
DECLARE
num NUMBER := 1;
BEGIN
EXCEPTION
END;
DECLARE
biggest NUMBER;
BEGIN
biggest := num1;
biggest := num2;
ELSE
biggest := num3;
END IF;
END;
60. Write a program in PL/SQL using nested loop with EXIT WHEN statement.
DECLARE
outer_counter NUMBER := 1;
inner_counter NUMBER;
BEGIN
LOOP
inner_counter := 1;
LOOP
inner_counter := inner_counter + 1;
END LOOP;
END LOOP;
END;
61. Handle the CURSOR_ALREADY_OPEN exception when trying to open a cursor that is
already open.
FirstName VARCHAR2(50),
LastName VARCHAR2(50),
Department VARCHAR2(50),
JobTitle VARCHAR2(50),
HireDate DATE,
Salary NUMBER(10, 2)
);
DECLARE
CURSOR cur_emp IS
emp_rec cur_emp%ROWTYPE;
cursor_already_open EXCEPTION;
BEGIN
INSERT INTO Employees (EmployeeID, FirstName, LastName, Department, JobTitle,
HireDate, Salary)
VALUES (1, 'John', 'Doe', 'Sales', 'Sales Manager', DATE '2018-01-01', 80000.00);
VALUES (3, 'Bob', 'Johnson', 'IT', 'Network Administrator', DATE '2020-03-01', 60000.00);
VALUES (4, 'Alice', 'Williams', 'HR', 'HR Manager', DATE '2017-09-01', 75000.00);
VALUES (5, 'Mike', 'Davis', 'Finance', 'Financial Analyst', DATE '2020-01-01', 65000.00);
COMMIT;
OPEN cur_emp;
cursor_open := TRUE;
ELSE
RAISE cursor_already_open;
END IF;
DBMS_OUTPUT.PUT_LINE('---------------------------');
END LOOP;
IF cursor_open THEN
CLOSE cur_emp;
cursor_open := FALSE;
END IF;
EXCEPTION
CLOSE cur_emp;
cursor_open := FALSE;
END IF;
END;
/
DECLARE
BEGIN
IF num1 > num2 AND num1 > num3 AND num1 > num4 AND num1 > num5 THEN
ELSIF num2 > num3 AND num2 > num4 AND num2 > num5 THEN
ELSE
END IF;
END;
DECLARE
i NUMBER := 1;
BEGIN
LOOP
IF i = 5 THEN
i := i + 1;
CONTINUE;
END IF;
i := i + 1;
IF i > 10 THEN
EXIT;
END IF;
END LOOP;
END;
FirstName VARCHAR(50),
LastName VARCHAR(50),
Department VARCHAR(50),
JobTitle VARCHAR(50),
HireDate DATE,
Salary DECIMAL(10, 2)
);
HireDate, Salary)
VALUES
COMMIT;
DECLARE
CURSOR cur_emp IS
emp_rec cur_emp%ROWTYPE;
BEGIN
OPEN cur_emp;
LOOP
DBMS_OUTPUT.PUT_LINE('---------------------------');
END LOOP;
EXCEPTION
DBMS_OUTPUT.
END;
66. Write a program in PL/SQL to print 1st n numbers with a difference of 3 and
starting from 1.
69. Write a program in PL/SQL to show the value of a same variable declared as
local and global.
70. Handle the CASE_NOT_FOUND exception when no branch matches the value
of a CASE statement.
**
***
****
*****
******
*******
********
declare
n number:=8;
i number;
j number;
begin
for i in 1..n
loop
for j in 1..i
loop
dbms_output.put('*');
end loop;
dbms_output.new_line;
end loop;
end;
/
DECLARE
i NUMBER := 1;
j NUMBER;
BEGIN
DBMS_OUTPUT.PUT('*');
END LOOP;
DBMS_OUTPUT.PUT_LINE('');
END LOOP;
END;
/
72. Write a program in PL/SQL to explain the uses of nested for loop with label.
DECLARE
i NUMBER;
j NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('----------------------');
END;
DECLARE
-- Declare variables
ex_invalid_transaction EXCEPTION;
BEGIN
RAISE ex_invalid_transaction;
ELSE
END IF;
EXCEPTION
END;
*
**
***
****
*****
******
*******
********
declare
n number:=5;
i number;
j number;
begin
for i in 1..n
dbms_output.put(' ');
loop
for j in 1..i
loop
dbms_output.put('*');
end loop;
dbms_output.new_line;
end loop;
end;
/
75. Write a program in PL/SQL to print the prime numbers between 1 to 50.
dbms_output.put('
');
end loop;
for k in 1..i
loop
dbms_output.put('*');
end loop;
dbms_output.new_line;
end loop;
end;
/
78. Write a program in PL/SQL to check whether a number is prime or not using
goto statement with for loop.
81. Write a program in PL/SQL to insert records from one table to another.
82. Handle the ACCESS_INTO_NULL exception when trying to access a NULL
record.
84. Write a program in PL/SQL to insert a row if the featched value for a
component is specified.
c1 INTEGER,
c2 INTEGER);
DECLARE
n1 NUMBER;
n2 NUMBER;
BEGIN
IF n2=4 THEN
ELSE
INSERT INTO TEST VALUES(n2+15,n1+15);
END IF;
END;
employee_id integer
first_name varchar(25)
last_name varchar(25)
email archar(25)
phone_number varchar(15)
hire_date date
job_id varchar(25)
salary integer
commission_pct decimal(5,2)
manager_id integer
department_id integer
PL/SQL Code:
Explain
DECLARE v_employee_idemployees.employee_id%TYPE;
v_first_nameemployees.first_name%TYPE;
v_last_nameemployees.last_name%TYPE; CURSOR employee_cursor IS
SELECT employee_id, first_name, last_name FROM employees; BEGIN
OPEN employee_cursor; LOOP FETCH employee_cursor INTO
v_employee_id, v_first_name, v_last_name;
DBMS_OUTPUT.PUT_LINE(v_employee_id || ' - ' || v_first_name || '
' || v_last_name); IF employee_cursor%NOTFOUND THEN RAISE
NO_DATA_NEEDED; END IF; END LOOP; EXCEPTION WHEN NO_DATA_NEEDED
THEN DBMS_OUTPUT.PUT_LINE('Caught NO_DATA_NEEDED exception: More
data retrieved than required.'); WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END; /;
*
***
*****
*******
*********
*******
*****
***
*
declare
i number:=9;
j number;
k number;
begin
while i>=1
loop
j:=9;
while j>i
loop
dbms_output.put(' ');
j:=j-2;
end loop;
for k in 1..i
loop
dbms_output.put('*');
end loop;
dbms_output.new_line;
i:=i-2;
end loop;
end;
/
87. Write a program in PL/SQL using loop with CONTINUE WHEN statement.
DECLARE
n NUMBER := 0;
BEGIN
n := n + 1;
DBMS_OUTPUT.PUT_LINE
EXIT WHEN n = 6;
END LOOP;
END;
/
88. Write a PL/SQL code to create a package that includes a procedure to
calculate the factorial of a number and a function to check if a number is prime.
COMMIT;
DECLARE
v_emp_count NUMBER;
v_vacancies NUMBER := 45; -- predefined vacancies in department 50
BEGIN
SELECT COUNT(*) INTO v_emp_count
FROM employees
WHERE department_id = 50; DBMS_OUTPUT.PUT_LINE('Number of employees in
department 50: ' || v_emp_count);
IF v_emp_count < v_vacancies THEN
DBMS_OUTPUT.PUT_LINE('There are vacancies in department 50.');
ELSE
DBMS_OUTPUT.PUT_LINE('No vacancies in department 50.');
END IF;
END;
89.Write a code in pl/sql to create a trigger that restrict the insertion of rows if the total of
a columns value exceeds a certain threshold
CREATE TABLE sales (
sale_id INSERT INTO sales (sale_id, product_name, sale_amount) VALUES (1,
'Product A', 30000);
INSERT INTO sales (sale_id, product_name, sale_amount) VALUES (2, 'Product B',
40000);
INSERT INTO sales (sale_id, product_name, sale_amount) VALUES (3, 'Product C',
25000);
COMMIT;
CREATE OR REPLACE TRIGGER check_sales_threshold
BEFORE INSERT ON sales
FOR EACH ROW
DECLARE
v_total_sales NUMBER;
v_threshold NUMBER := 100000;
BEGIN
SELECT SUM(sale_amount) INTO v_total_sales
FROM sales;
IF (v_total_sales + :NEW.sale_amount) > v_threshold THEN
RAISE_APPLICATION_ERROR(-20001, 'Total sales amount exceeds the threshold
of 100,000.');
END IF;
END;