background
Due to data deletion, the company's business was affected to a certain extent, but in the end no one admitted that because it was a public account, name, and the data was deleted, the important data needed to be retrieved quickly, so a basic solution came out Yes, it is required to back up [important table data] before deleting it. We use the important table deletion action to set up Trigger, create a backup table, and back up the data.
environment
- 2022-09-05
- Database connection tool DBeaver
problems encountered
- 报错 ORA-04098: trigger ‘RMSTEST.NEWMKD_LIST’ is invalid and failed re-validation
- 04098 ==》 in Oracle trigger describes how to check the reason for failure.
- It's because the fields in the table where Trigger is to be created are mixed up, that is, the fields do not exist.
- solved
- ORA-00933: SQL command not properly ended
- PLS-00103: Encountered the symbol “end-of-file” when expecting one of the following: ( begin case declare end exception exit for goto if loop mod null pragma raise return select update while with << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge
- I actually looked at this a lot, and finally I asked my colleague to help me look at it, because the tools he used were different from mine, and I knew what the problem was as soon as I entered it (he used Oracle’s official tool SQL Developer) because he knew how to do it. There is a reddish dotted line, and you probably know where the problem is. Mine is that after the insert statement ends, there is no semicolon (;) added! ! ! ! However, the DBeaver I used did not reflect it at all, and only reported an error during compilation.
- I actually looked at this a lot, and finally I asked my colleague to help me look at it, because the tools he used were different from mine, and I knew what the problem was as soon as I entered it (he used Oracle’s official tool SQL Developer) because he knew how to do it. There is a reddish dotted line, and you probably know where the problem is. Mine is that after the insert statement ends, there is no semicolon (;) added! ! ! ! However, the DBeaver I used did not reflect it at all, and only reported an error during compilation.
step
- First create a [backup table] with the same structure as the main table and add an additional column for deletion time (OP_REMARK VARCHAR2(30))
- CreateTrigger
- code
create or replace TRIGGER 你的trigger的名字
这里可以是BEFORE或者AFTER都可以,一个是操作前,一个是操作后 DELETE ON 原来的表格
FOR EACH ROW
DECLARE
V_MESSAGE VARCHAR(50);
BEGIN
V_MESSAGE := to_char(sysdate,'yyyy/mm/dd HH24:MI:SS') || ' DELETE';
INSERT INTO
TABLE_BACK (
"NO",
ASSISTANT_EMP,
,OP_REMARK
)
VALUES
(
:OLD.NO,
:OLD.ASSISTANT_EMP
,V_MESSAGE
);
END;