Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

sql - Oracle Trigger copy data from one table to another

Student table : student ID, student name, student contact
Evaluation: EvalID, Eval number

student gives an evaluation of each lesson, from 1-7. I need to creat a trigger which copies student information, and the evaluation they gave into another table if they give a evaluation of 7

What i have done is

Create or replace trigger test
After Insert on evaluation
when (eval_number = 0)
Begin

I dont know what to do from here.... any ideas? and is my first bit of trigger correct?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Following tables:

CREATE TABLE student(
studentId INTEGER,
student_name varchar2(100),
student_contact varchar2(100));

CREATE TABLE evaluation(
evalId  INTEGER,
eval_num INTEGER,
student_id INTEGER)

create table eval_audit(
studentId INTEGER,
student_name VARCHAR2(100),
student_contact VARCHAR2(100),
eval_num INTEGER)

Trigger to insert data from student when there is a evaluation with 7:

CREATE OR REPLACE TRIGGER AIEvaludation
AFTER INSERT ON evaluation
FOR EACH ROW
WHEN (new.eval_num = 7)
DECLARE
    CURSOR curStudent IS
        SELECT studentid, student_name, student_contact
          FROM student
         WHERE studentid = :new.student_id;
    --
    vRowStudent curStudent%ROWTYPE;
BEGIN
    OPEN curStudent;
    FETCH curStudent INTO vRowStudent;
    CLOSE curStudent;
    INSERT INTO eval_audit
    (studentid, student_name, student_contact, eval_num)
    VALUES (vRowStudent.studentid, vRowStudent.student_name, vRowStudent.student_contact, :new.eval_num);
END AIEvaludation;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...