使用单个SQL触发器从单个删除中复制多个行?

时间:2016-06-16 20:34:57

标签: oracle triggers

我正在尝试从表中删除单行,这可以触发并将所有相关信息(多行)复制到第三表。我在下面构建了一个简单的例子:

表:学生

// Make the app remove an entry if the user "LONG CLICKS" the entry
    if (listViewList != null) {
        listViewList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                String strTemp = listViewList.getItemAtPosition(position).toString();
                Toast.makeText(CustomListActivity.this, "Removed " + strTemp, Toast.LENGTH_SHORT).show();
                strWords.remove(position);
                ArrayAdapter<String> adapter = new ArrayAdapter<>(CustomListActivity.this, R.layout.spinner_list, strWords);
                listViewList.setAdapter(adapter);
                return false;
            }
        });
    }

表:成绩

NAME     AGE
-----    ------
Mark     19
Jim      21
Scott    22

表:备份(我要触发的内容)

NAME     GRADE
-----    ------
Mark     95
Mark     98
Jim      60
Scott    100
Scott    79
Scott    77

在上面的示例中,删除“Mark ... 30”行也会删除第二个表中的两行,并以“Mark”作为名称。我不知道该怎么做。我能够编译触发器代码,但调用“DELETE FROM STUDENTS WHERE NAME ='MARK'”会返回太多行错误。以下是我的代码

我的代码

SQL> DELETE FROM STUDENTS WHERE NAME='MARK';
SQL> SELECT * FROM BACKUP;

NAME     AGE      GRADE
-----    ------   ------
Mark     19       95
Mark     19       98

1 个答案:

答案 0 :(得分:0)

您获得的行太多,因为您选择了只接受一行结果的select。尝试组合选择和插入

INSERT INTO backup
(name, age, grade)
SELECT :old.name, :old.age, grade FROM Grades WHERE Names=:old.name;