How to create a last_modified field in oracle 9i? - oracle

I'm using Oracle 10g Express or Oracle XE. In the EMPLOYEES table I want to add another column call last_modified that automatically generate current date or date/time if it has been modified. I know there is this rowscn(timestamp) thing that's only available from oracle 10g onwards but I want to manually create one because my client is using oracle 9i but I only have 10g to do the testing. Also I've never use oracle before. This is for some integration project with Lotus Notes. Therefore if possible I want the date or date/time to recognizable by LotusScript.

Add a column of type TIMESTAMP to the table (might not be available in the de-supported 9i version, but then just use DATE instead).
alter table employees add (modified_at timestamp);
Then create a trigger that is fired on update which simply sets that column to sysdate:
create or replace trigger update_modified
before update on employees
for each row
begin
:new.modified_at := sysdate;
end;
/

The usual way is to
add a modify_date / modified_by column to your table
create a trigger that updates this column whenever the row is modified
Example:
CREATE OR REPLACE TRIGGER TR_AUD_EMPLOYEE
BEFORE INSERT OR UPDATE OR DELETE ON EMPLOYEE
FOR EACH ROW
begin
if UPDATING then
:new.MODIFIED_BY := user;
:new.MODIFY_DATE := sysdate;
end if;
end;

Related

Control a trigger using when clause in Oracle

I am trying to create trigger TRIG_USER in Oracle db. Is there a way to control this trigger using the when clause.I want the trigger to activate only if there a entry in the table trigger_switch
I am looking something like this.
CREATE TRIGGER TRIG_USER
AFTER INSERT OR UPDATE OR DELETE ON USER_TABLE
FOR EACH ROW WHEN (IF EXISTS (SELECT 1 FROM trigger_switch))
BEGIN
[...]
END;

Is there a way to log table creation and column modification in a table with the one who execute it, in oracle schema?

I am searching for a way, to store only the tables and columns that are added in the database with the one who created it (nachine_name) in a log table.
I tried to add a trigger on sys table user_tab_cols but I cannot do that Why cannot I create triggers on objects owned by SYS?
The system table user_objects will give me the date when a table created, but I want also to know which machine created it. and I also want to track the column creation and modification and log them in a table.
Is that possible ? is there a way for that ?
you can create a database event trigger:
CREATE OR REPLACE TRIGGER log_ddl_event_trg
AFTER DDL
ON DATABASE
DECLARE
v_sql_list ora_name_list_t;
v_sql_txt VARCHAR2(2500);
BEGIN
FOR i in 1..ORA_SQL_TXT(v_sql_list) LOOP
v_sql_txt := v_sql_txt || v_sql_list(i);
EXIT WHEN length(v_sql_txt ) >= 2000;
END LOOP;
...
END;
/
in the Trigger, you can get the executed ddl-statement using the ORA_SQL_TXT() Funktion, and then log it in the table together with the other data (log_date, user etc.).

DDL Triggers invoked on DROP COLUMN in Oracle 11.2.0.4.0

If you run the following code snippet below the trigger on the table is invoked when a clumn is dropped which had been added after the table creation and has a default value and not null defined. This is behaviour I could only reproduce in Oracle 11.2.0.4.0 but not in 11.2.0.3.0. COuld somebody please help me with this? Is this expected behaviour? How can I drop the column without running the trigger?
CREATE TABLE T (
DESCRIPTION VARCHAR2(50)
);
INSERT INTO T (DESCRIPTION) VALUES ('asd');
COMMIT;
CREATE OR REPLACE TRIGGER BEFORE_T_U
BEFORE UPDATE ON T
REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
BEGIN
RAISE_APPLICATION_ERROR(-20003, 'This before update trigger should not be invoked!');
END BEFORE_T_U;
/
ALTER TABLE T ADD AMOUNT NUMBER DEFAULT 0 NOT NULL;
ALTER TABLE T DROP COLUMN AMOUNT;

ODBC with Oracle Trigger Key Column

I'm trying to update some existing code that is supposed to write data to a variety of Databases (SQL, Access, Oracle) via ODBC, but I'm having a few problems with Oracle and am looking for any suggestions.
I've set my Oracle database up using a Trigger (basic tutorial online, which I'd like to support).
CREATE TABLE TABLE1 (
RECORDID NUMBER NOT NULL PRIMARY KEY,
ID VARCHAR(40) NULL,
COUNT NUMBER NULL
);
GO
CREATE SEQUENCE TABLE1_SEQ
GO
CREATE or REPLACE TRIGGER TABLE1_TRG
BEFORE INSERT ON TABLE1
FOR EACH ROW
WHEN (new.RECORDID IS NULL)
BEGIN
SELECT TABLE1_SEQ.nextval
INTO :new.RECORDID
FROM dual;
end;
GO
I then populate a DataTable using a SELECT * FROM TABLE1. The first problem is that this DataTable doesn't know that the RecordId column is auto-generated. If I have data in my table then I can't alter it because I get a error
Cannot change AutoIncrement of a DataColumn with type 'Double' once it
has data.
If I continue, ignoring this, then I quickly get stuck. If I create a new DataRow and try to insert it, I can't set RecordID to DBNull.Value because it complains that the column has to be non-null (NoNullAllowedException). I can't however generate a value myself, because I don't know what value I should be using really, and don't want to screw up the trigger by using the next available value.
Any suggestions on how I should insert data without ODBC complaining?
It does not appear that your first problem is with an Oracle database. There is no such thing as an "Autoincrement" column in Oracle. Are you sure that message is coming from an Oracle database?
With Oracle, you should be able to provide any dummy value on insert for the primary key, and the trigger will overwrite it.
There is also nothing in your provided description that would prevent you from updating this value in Oracle (since your trigger is on insert only) unless you have foreign key references to the key.

Automatically populate date in oracle table

I have created a table in oracle XE, and I have a field with type date. I would like if possible when I insert a row, that it automatically fills that field with the current date from the system.
I am inserting the rows from the SQL prompt.
Thanks
Here is how, you need to format your table properly:
create table test (first number
, second timestamp default systimestamp
, third varchar2(12));
And your default value is always current system time formatted as timestamp.
change the field after creating the table
ALTER TABLE table MODIFY time_collumn TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
Or you could also use a trigger:
CREATE OR REPLACE TRIGGER date_trigger
BEFORE INSERT
ON table_name
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT sysdate INTO :NEW.column_name FROM dual;
END;
The below snippet might be helpful if we forget to add the constraint while creating the table:
ALTER TABLE TABLE_NAME
ADD CONSTRAINT CONSTRAINT_NAME
COLUMN_NAME DATA_TYPE DEFAULT CURRENT_DATE;

Resources