I have data as below in csv format and uploading this data into table:
TABLE NAME: MYDATA
+----+-------+-------+---------+
| ID | NAME | STATE | CODE |
+----+-------+-------+---------+
| 1 | P&P | AQ | BIN1234 |
| 2 | ABC | AQ | BIN5678 |
| 3 | G-I | AQ | BIN3457 |
| 4 | MC-DO | AQ | BIN3462 |
| 5 | TEC | AQ | ERP9756 |
| 6 | CBA | BT | ERP4353 |
| 7 | W&X | BT | ERP5456 |
| 8 | P-Q | GH | ERP3457 |
+----+-------+-------+---------+
Trying to achieve below conditions based on data:
- Delete values from STATE column where value is not equal to 'AQ'.
- Delete values from CODE column where value like '%ERP%'.
- Replace special characters AMPERSAND '&' and HYPHEN '-' with UNDERSCORE '_' from NAME column.
- Trim whitespaces from NAME column.
Wrote below trigger but getting error:
CREATE OR replace TRIGGER BI_MYDATA_TR
BEFORE INSERT OR UPDATE ON MYDATA
BEGIN
DELETE FROM MYDATA
WHERE upper(state) <> 'AQ'
OR upper(code) LIKE '%ERP%';
:NEW.name := trim(regexp_replace(:NEW.name, '&|-', '_'));
END;
Below is the error I am getting:
Error at line 3: PL/SQL: Statement ignored Error at line 3: PLS-00201: identifier 'NEW.NAME' must be declared
- create or replace trigger BI_MYDATA_TR
- before insert or update on MYDATA
- begin
- DELETE FROM MYDATA WHERE upper(state) <> 'AQ' OR upper(code) LIKE '%ERP%';
- :NEW.name := trim(regexp_replace(:NEW.name, '&|-', '_'));
Appreciate if I get any help on how to combine these multiple conditions in one trigger.
Thanks in advance.
Thanks,
Richa
Solution posted by @Florin worked and updating my code below since adding code in comments section was not formatting:
CREATE OR replace TRIGGER BIU_MYDATA_TR
BEFORE INSERT OR UPDATE ON MYDATA
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
DELETE FROM MYDATA
WHERE upper(STATE) <> 'AQ'
OR upper(CODE) LIKE '%ERP%';
:NEW.NAME := trim(regexp_replace(:NEW.NAME, '&|-', '_'));
END;
:new
(with colon) to reference the values of row being inserted. But the target table doesn't contain the data you are inserting, sodelete
statement will not affect new data