CREATE OR REPLACE TRIGGER trg_emp_insert
AFTER INSERT ON emp
FOR EACH ROW
BEGIN
— Insert into audit table
INSERT INTO emp (emp_id, emp_name,salary)
VALUES (:NEW.emp_name,:NEW.salary,:NEW.emp_id);
— Display message
DBMS_OUTPUT.PUT_LINE(‘Employee inserted successfully.’);
END;
insert into emp(emp_name,salary,emp_id)values(‘rohan’,3000,3);
select * from emp;
Example:
CREATE OR REPLACE TRIGGER trg_emp_insert
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
-- Insert into audit table
INSERT INTO emp_audit (emp_id, emp_name, created_date)
VALUES (:NEW.emp_id, :NEW.emp_name, SYSDATE);
-- Display message
DBMS_OUTPUT.PUT_LINE('Employee inserted successfully.');
END;
/
When you execute:
INSERT INTO employees (emp_id, emp_name)
VALUES (101, 'John');
The trigger:
- Inserts a record into
emp_audit. - Displays:
Employee inserted successfully.
Important
DBMS_OUTPUT.PUT_LINEonly displays messages ifSERVEROUTPUTis enabled in your SQL client:SET SERVEROUTPUT ON;- In many applications (such as Oracle Forms, APEX, or Java/.NET applications),
DBMS_OUTPUTis not shown to end users.
If you want to show an actual popup or stop the insert with an error message, use:
RAISE_APPLICATION_ERROR(-20001, 'Employee inserted successfully.');