简体   繁体   中英

How can i call a function via trigger in Oracle SQL?

(I am using Oracle Database XE 18c and SQL Developer)

We have a table named "courses" (cid: varchar2, cname: varchar2, credit: integer, did: integer) and we have a table named "course_history" with same attributes.

When an update is made on credit attribute at the courses table, a trigger should call a function, and the function should insert the old attribute values to "course_history".

Trigger and function:

CREATE OR REPLACE FUNCTION fun RETURN VOID IS
BEGIN
    INSERT INTO course_history
    VALUES(:OLD.cid, :OLD.cname, :OLD.credit, :OLD.did);
END;



CREATE OR REPLACE TRIGGER trig
AFTER UPDATE OF CREDIT ON COURSE
FOR EACH ROW
BEGIN
    -- ???
END;

How can i call a function inside a trigger? Also, is it possible that a function to return void in PLSQL?

Thank you.

  1. Don't use a function. A function is used to read data and return a value. Use a procedure. Procedures are used to modify data.

  2. Your procedure will need to take parameters. It won't have access to the :new pseudorecord.

It sounds like you want something like this

CREATE OR REPLACE procedure insert_history( 
  p_cid in course.cid%type,
  p_cname in course.cname%type,
  p_credit in course.credit%type,
  p_did in course.did%type
)
as
BEGIN
    INSERT INTO course_history( cid, cname, credit, did )
    VALUES(p_cid, p_cname, p_credit, p_did);
END;



CREATE OR REPLACE TRIGGER trig
AFTER UPDATE OF CREDIT ON COURSE
FOR EACH ROW
BEGIN
   insert_history( :new.cid, :new.cname, :new.credit, :new.did );
END;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM