how to insert data using trigger - sql

I got question about using trigger to insert data, for instance, I do have two tables, and second table has attributes and records with table, except additional two attributes, like below:
CREATE TABLE dept
(
DEPTNO NUMBER(3) PRIMARY KEY,
DNAME VARCHAR2(16),
LOC VARCHAR2(16)
);
CREATE TABLE dept_shadow
(
DEPTNO NUMBER(3) PRIMARY KEY,
DNAME VARCHAR2(16),
LOC VARCHAR2(16),
USER VARCHAR2(32),
MODTIME CHAR(17)
);
and I want create a trigger to track all inserts into a table.
surprisedly, I got error about creating table:
Error starting at line : 11 in command -
CREATE TABLE dept_shadow
(
DEPTNO NUMBER(3) PRIMARY KEY,
DNAME VARCHAR2(16),
LOC VARCHAR2(16),
USER VARCHAR2(32),
MODTIME CHAR(17)
)
Error report -
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
I have no idea about this error and does anyone can tell me how to do this job by create trigger? Since there is no actual records to insert! Any suggestions are appreciated

Okay, so the error you are getting is because oracle (like all databases) has some reserved words. Now I'm not 100% sure because I don't tend to work with Oracle DB all that often, but I would assume that you cannot use the word USER for that reason. Try using USERNAME or USERDESCRIPTION or something like that instead.
Now for the trigger:
CREATE OR REPLACE TRIGGER trg_shadow
BEFORE INSERT OR UPDATE OR DELETE
ON dept_shadow
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
MODTIME char (17);
BEGIN
IF INSERTING
THEN
-- do something
ELSIF UPDATING
THEN
-- do something
ELSIF DELETING
THEN
-- do something
END IF;
From there, you can access "new" data through :NEW and "old" data through :OLD.
EDIT:
The difference of a BEFORE and AFTER trigger is when they are executed and both have a valid use.
BEFORE triggers may be used to validate data BEFORE inserting or updating. So for instance if you don't want to update the rows that would otherwise have the value 0 in column x.
AFTER triggers may be used to validate the new data AFTER inserting. So for instance if you want to delete all rows that now have the value 0 in column x.
It doesn't really matter in your case though.
Hope that helps!

The error is because of the column name(user) you are using in the dept_shadow table.
USER is predefined that's why it won't work as column name in table. Rename it to 'audit_user' or anything you want, which is not keyword. It will work seamlessly.
And for trigger
CREATE OR REPLACE TRIGGER dept_trigger
AFTER INSERT
ON dept
FOR EACH ROW
DECLARE
v_username varchar2(10);
BEGIN
-- Find username of person performing the INSERT into the table
SELECT user INTO v_username
FROM dual;
-- Insert record into shadow table
INSERT INTO dept_shadow
( DEPTNO,
DNAME,
LOC,
AUDIT_USER,
MODTIME )
VALUES
( :new.DEPTNO,
:new.DNAME,
:new.LOC,
v_username,
:new.MODTIME
);
END;
/
Hope this will work for you.

Related

Update trigger assistance

Evening, needing assistance regarding triggers.
Within my development environment I have two tables, one containing employee data (which contains various data errors that will be amended via ALTER TABLE) and the log table.
How do i go about designing a trigger that will update multiple rows contained within the log table such as 'issue_status','status_update_date' when ALTER TABLE sql is used to amend the data contained in the first data table?
-- employee table
CREATE TABLE emp(
emp_id INTEGER NOT NULL,
emp_name VARCHAR(30),
emp_postcode VARCHAR(20),
emp_registered DATE,
CONSTRAINT pk_emp PRIMARY KEY (emp_id));
-- SQL for the log table
CREATE TABLE data_log
(issue_id NUMBER(2) NOT NULL,
table_name VARCHAR2(20),
row_name VARCHAR2(20),
data_error_code NUMBER(2),
issue_desc VARCHAR2(50),
issue_date DATE,
issue_status VARCHAR2(20),
status_update_date DATE);
-- example log insert
INSERT INTO data_log( SELECT DI_SEQ.nextval, 'emp', emp.emp_id, '1', 'emp_name case size not UPPER', SYSDATE, 'un-fixed', '' FROM emp WHERE emp_name != UPPER(emp_name));
This is the example of the issue inserted into the log table. All i want to do is if I update the emp table to set 'emp_name' to Upper the trigger will register this update and change the rows 'issue_status' and 'status_update_date' to 'fixed' and the 'sysdate' of when the change was made
I've done some browsing however i'm still struggling to understand, any literature recommendations would be appreciated also.
Thanks in advance for the assistance.
Note that we don't use ALTER TABLE to update the rows of a table as you have mentioned.We use Update statement. Your trigger should be a BEFORE UPDATE TRIGGER like this.
CREATE OR REPLACE TRIGGER trig_emp_upd BEFORE
UPDATE ON emp
FOR EACH ROW
WHEN ( new.emp_name = upper(old.emp_name) )
BEGIN
UPDATE data_log
SET
issue_status = 'fixed',
status_update_date = SYSDATE
WHERE
row_name =:new.emp_id;
END;
/

Oracle SQL - PLS-00049 - bad bind variable

I have table defined like this:
create table "nakup" (
"cislo_n" INTEGER not null,
"id_zak" INTEGER not null,
"jm_pobocky" CHAR(15) not null,
"datum_cas" DATE not null
constraint CKC_DATUM_CAS_NAKUP check ("datum_cas" >= TO_DATE('1.01.1994 8:30:25', 'DD.MM.YYYY HH24:MI:SS')),
constraint PK_NAKUP primary key ("cislo_n")
I want to create a trigger that would block inserting a date from the future, my code looks like this:
create or replace TRIGGER TRIGGER1
BEFORE INSERT OR UPDATE ON "nakup"
FOR EACH ROW
BEGIN
if (:new.datum_cas > current_timestamp) then
raise_application_error(-20000, 'Špatně zadané datum a čas.');
end if;
END;
I keep getting error Error(5,7): PLS-00049: chybná vázaná proměnná 'NEW.DATUM_CAS' (bad bind variable in english). What am I doing wrong?
As Gordon Linoff suggested, your trigger will compile if you enclose the column name in double quotes:
create or replace TRIGGER TRIGGER1
BEFORE INSERT OR UPDATE ON "nakup"
FOR EACH ROW
BEGIN
if (:new."datum_cas" > current_timestamp) then
raise_application_error(-20000, 'Špatně zadané datum a čas.');
end if;
END;
/
Trigger TRIGGER1 compiled
Quoted identifiers have to be quoted everywhere they are referenced. You seem to have realised that when you referred to the table name in the trigger definition, but it applies to the column names too.
Your life will be much simpler if you use unquoted identifiers (or quoted uppercase identifiers, which is the same thing as long as they don't contain any invalid characters). Oracle does not recommend using quoted identifiers for database object names. So this works with no double-quotes at all:
create table nakup (
cislo_n INTEGER not null,
id_zak INTEGER not null,
jm_pobocky CHAR(15) not null,
datum_cas DATE not null,
constraint CKC_DATUM_CAS_NAKUP
check (datum_cas >= TO_DATE('1.01.1994 8:30:25', 'DD.MM.YYYY HH24:MI:SS')),
constraint PK_NAKUP primary key (cislo_n)
);
Table NAKUP created.
create or replace TRIGGER TRIGGER1
BEFORE INSERT OR UPDATE ON nakup
FOR EACH ROW
BEGIN
if (:new.datum_cas > current_timestamp) then
raise_application_error(-20000, 'Špatně zadané datum a čas.');
end if;
END;
/
Trigger TRIGGER1 compiled
You can then refer to nakup.datum_cas etc. in your code, instead of having to use "nakup"."datum_cas".
yes...
datum_cas column name is different from datum_cas column name..
Oracle stores column names as uppercase by default,if we use double quotes ("") the column name is stored as-is (upper/lower)
example....
SQL> create table test5 (id number,"id1" number);
table created.
SQL> insert into test5 values(1,2);
1 row created.
SQL> select * from test5;
ID id1
----- ----------
1 2
id is stored as ID and id1 is stored as id1.

sql error 04098 invalid trigger

I need some assistance with troubleshooting the trigger that I'm trying to create/use for logging updates and inserts on a table.
I'm using a customers_history table to track all the changes being made on the customers table.
CREATE TABLE customers (
custID INTEGER PRIMARY KEY,
custFName VARCHAR2(30),
custLName VARCHAR2(30),
custState CHAR(20),
custZip NUMBER(5)
);
-- log inserts and updates on customers table
CREATE TABLE customers_history (
histID INTEGER PRIMARY KEY,
cID INTEGER,
cFName VARCHAR2(30),
cLName VARCHAR2(30),
cState CHAR(20),
cZip NUMBER(5)
);
Also, for the histID I'm using a sequence to auto increment the histID on customers_history table.
CREATE SEQUENCE ch_seq
MINVALUE 1
START WITH 1
INCREMENT BY 1;
CREATE OR REPLACE TRIGGER audit_customers
BEFORE UPDATE
OR INSERT ON customers
FOR EACH ROW
BEGIN
INSERT INTO customers_history(histID,cID,cFName,cLName,cState,cZip)
VALUES(ch_seq.nextval,:NEW.custID,:NEW.custFName,:NEW.custLName,
:NEW.custState,:NEW.custZip);
END;
/
I have been inserting two rows on customers prior to creating the trigger, and they work fine. After I create the trigger, it will not allow me to insert anymore rows on customers and it also throws the ORA-04098: trigger 'SYSTEM.AUDIT_CUSTOMERS' is invalid and failed re-validation 04098. 00000 - "trigger '%s.%s' is invalid and failed re-validation" error message.
I've tried to see if there is any code errors using select * from user_errors where type = 'TRIGGER' and name = 'audit_customers'; and it returned no lines. Not sure if that helps or not. Thanks.
you have created your trigger for multiple DML operations (Insert and Update) so you need to specipy the DML operation by using IF INSERTING THEN....END IF; and IF UPDATING THEN....END IF; for example:
CREATE OR REPLACE TRIGGER audit_customers
BEFORE UPDATE
OR INSERT ON customers
FOR EACH ROW
BEGIN
IF INSERTING THEN
INSERT INTO customers_history(histID,cID,cFName,cLName,cState,cZip)
VALUES(ch_seq.nextval,:NEW.custID,:NEW.custFName,:NEW.custLName,
:NEW.custState,:NEW.custZip);
END IF;
END;
/
I went through and changed a couple things and it seems to work for both insert and update operations. I dropped the tables, sequence, and trigger, then did the tried the following code and it works now. Thank you all for your time and inputs!
CREATE TABLE customers (
custID INTEGER,
custFName VARCHAR2(30),
custLName VARCHAR2(30),
custState CHAR(20),
custZip NUMBER(5)
);
CREATE TABLE customers_history (
histID INTEGER,
cID INTEGER,
cFName VARCHAR2(30),
cLName VARCHAR2(30),
cState CHAR(20),
cZip NUMBER(5)
);
CREATE OR REPLACE TRIGGER audit_customers
BEFORE UPDATE
OR INSERT ON customers
FOR EACH ROW
BEGIN
INSERT INTO customers_history(
histID,
cID,
cFName,
cLName,
cState,
cZip
)
VALUES(
ch_seq.nextval,
:new.custID,
:new.custFName,
:new.custLName,
:new.custState,
:new.custZip
);
END;
/
I used the same sequence code as before, added a couple of rows, and it worked. Then I altered the two tables, by adding the primary keys,
ALTER TABLE customers ADD PRIMARY KEY(custID);
ALTER TABLE customers_history ADD PRIMARY KEY(histID);
inserted a couple other columns, modified a few rows, and it still worked. I'm a happy camper, although I'm not certain what or how it actually got fixed.

sql triggers - want to compare existing row value with value being inserted

I am creating a trigger that should compare of the values being inserted with one that already exists in the table. The old referencer doesn't work here because I am inserting, but how can I reference something that already exists?
Here is my tables and trigger:
create table events(eid char(2) primary key, cid char(2));
create table activities(mid char(2), eid char(2),
primary key (mid, eid),
constraint activities_fk foreign key (eid) references events(eid));
create or replace trigger check_valid
before insert or update on activities
for each row when (old.mid=new.mid)
declare
v_eid char(2);
v_cid char(2);
n_cid char(2);
begin
select eid into v_eid from activities
where mid=:new.mid;
select cid into v_cid from events
where eid=v_eid;
select cid into n_cid from events
where eid=:new.eid;
if v_cid=n_cid then
raise_application_error(-20000, 'Error');
end if;
end check_valid;
/
show errors;
You can't generally select from the table you're inserting into in a trigger. This is the mutating table problem, or as I often call it, the "damn mutating table problem".
Basically, don't do this. It's a bad idea. What happens if you have two sessions operating on the table at once? The trigger fires and neither session sees what the other has done until the commit, which is after the trigger. Then you've got unexpected data in your database.
Tom Kyte says, "when I hit a mutating table error, I've got a serious fatal flaw
in my logic."

SQLPlus AUTO_INCREMENT Error

When I try and run the following command in SQLPlus:
CREATE TABLE Hotel
(hotelNo NUMBER(4) NOT NULL AUTO_INCREMENT,
hotelName VARCHAR(20) NOT NULL,
city VARCHAR(50) NOT NULL,
CONSTRAINT hotelNo_pk PRIMARY KEY (hotelNo));
I get the following error:
(hotelNo NUMBER(4) NOT NULL AUTO_INCREMENT,
*
ERROR at line 2:
ORA-00907: missing right parenthesis
What am I doing wrong?
Many will gripe about this not being a standard feature in Oracle, but when it’s as easy as two more commands after your CREATE TABLE command I can’t see any good reason to use fancy SQL on every insert.
First let’s create a simple table to play with.
SQL> CREATE TABLE test
(id NUMBER PRIMARY KEY,
name VARCHAR2(30));
Table created.
Now we’ll assume we want ID to be an auto increment field. First we need a sequence to grab values from.
SQL> CREATE SEQUENCE test_sequence
START WITH 1
INCREMENT BY 1;
Sequence created.
Now we can use that sequence in a BEFORE INSERT trigger on the table.
CREATE OR REPLACE TRIGGER test_trigger
BEFORE INSERT
ON test
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT test_sequence.nextval INTO :NEW.ID FROM dual;
END;
/
SQL> INSERT INTO test (name) VALUES ('Jon');
1 row created.
SQL> INSERT INTO test (name) VALUES (’Bork’);
1 row created.
SQL> INSERT INTO test (name) VALUES (’Matt’);
1 row created.
SQL> SELECT * FROM test;
ID NAME
———- ——————————
1 Jon
2 Bork
3 Matt
Oracle has no auto_increment, you need to use sequences.
Or - starting with Oracle 12.1 - you can simply have:
CREATE TABLE employee
(
id NUMBER GENERATED by default on null as IDENTITY
....
)