Auditing from a trigger in oracle - oracle

I am working with a big database and it had many generators of data basically have alot of data being inserted and updated per day. I have a trigger that updates each row every time there is an update or insert and i use the following code to input the person's name from the apex application (the user from apex)
NVL(v('APP_USER'),USER)
My problem comes when there is heavy data entry, for example 500,000 records are being generated by one person (John) and when john generated this data, each row is audited but as john generated more than one person who are users in the apex application shows up in the audit.
So scenario is that john clicks a button to generate data and in the audit fields, more than one users name show up (Mary, John, Peter)
Does anybody have any idea why this is happening?
the entire code, it is very generic
TRIGGER trg_tableA before insert or update
on tableA REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
begin
:new.insert_date:=sysdate;
:new.inserted_by:= nvl(V('APP_USER'),USER);
:new.modified_date:=sysdate;
:new.modified_by:= nvl(V('APP_USER'),USER);
end trg_tableA;
Thank you in advance

As per this link Use v('APP_USER') as default value for column in Oracle Apex There are other options than V('APP_USER'). Since Apex 5, the APP_USER is stored in the sys_context and that is a lot more performant than the V() function. It is available as SYS_CONTEXT('APEX$SESSION','APP_USER').
Please try the below and see if your issue is getting resolved.
TRIGGER trg_tableA before insert or update
on tableA REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
begin
:new.insert_date:=sysdate;
:new.inserted_by:= nvl(sys_context('APEX$SESSION','APP_USER'),user);
:new.modified_date:=sysdate;
:new.modified_by:= nvl(sys_context('APEX$SESSION','APP_USER'),user);
end trg_tableA;

Related

Insert Into from another table causing Can't update 'table' in stored function/trigger because it is already used by statement which invoked

I'm pretty new to SQL so if you can keep it as as simple as possible that would be appreciated.
Currently trying to add all the contact IDs from the contact table (civicrm_contacts) to a new custom field table which is a button to show/hide a contact from the main website. At the moment whenever I run my query I get back the following:
"SQL Error [1442] [HY000]: Can't update table 'civicrm_contact' in stored function/trigger because it is already used by statement which invoked this stored function/trigger."
My query is going something like this:
INSERT INTO civicrm_value_appears_on_we_69 (entity_id)
SELECT cc.id
FROM civicrm_contact cc
WHERE contact_type='Organization'
There are 3 columns in total for the 'civicrm_value_appears_on_we_69': 'ID', 'entity_id', and 'appears_on_website_45' which is a 0 or 1 for the button value. All I want is to insert the contact IDs for all the Organisation contacts, I don't mind about nulls for now as long as it makes the rows.
I've looked up a bit about the error and I'm unsure why it's happening. As far as I can gather this errors happpens when you're using a trigger that using the same table as you're inserting into/from. I'm not using a trigger at the moment, or perhaps I have this wrong.
Any suggestions?

Oracle Apex Apex_application.G_fNN processing not pulling all the values

I'm very much new to oracle apex and have been constantly failing on creating tabular form for inserting data into my table. My scenario is as follows.
i have two tables
1. tasks(task_id, task_Name)
2. Efforts (Eff_id,Task_id,Hours_spent,NOtes).
I want to create a tabular form to insert data into Efforts table with the tasks that I've in Tasks table. I created the tabular form with the query below.
SELECT APEX_ITEM.CHECKBOX(1,TASK_ID) "TASK_ID",
TASK_NAME,
APEX_ITEM.TEXT(2,'')"HOURS_SPENT",
APEX_ITEM.TEXT(3,'')"NOTES"
FROM TASKS;
but when I create the "after Submit" process to read the values of this report and tried to insert into my table effortS with the following code, something strange is happening. When i select the checkbox of row-1,2 and 3 then only row 1 is getting inserted into my efforts table but for the rest, only task id values are getting inserted not the whole row. I was missing "hours_spent and NOtes". in my efforts table for rows-2 and 3..here is my plsql block
BEGIN
FOR i in APEX_APPLICATION.G_F01.COUNT LOOP
INSERT INTO EFFORTS(EFF_ID,TASK_ID,HOURS_SPENT,NOTES)
VALUES (SEQ_EFFORTS.NEXTVAL,APEX_APPLICATION.G_F01(i),APEX_APPLICATION.G_F02(i),APEX_APPLICATION.G_F03(i))
END LOOP;
commit;
END;
Can Someone help me! if there's another way of doing my requirement, then I'm more than happy to do it..
Welcome to the APEX community.
This is a classic problem, due to the nature of HTML
Get the values of the selected rows with checked checkbox
Note, the usage of tabular forms is also waning in favour of Interactive Grids.

How to write trigger in oracle to check for one specific condiition

I have one table name as user_count_details. There are total 3 columns in this table.
msisdn=Which uniquely defines row for one specific user
user_count= Which stores the count of user.
last_Txn_id= Which stores the last transfer id of txn which user has performed.
The user_count column of this table user_count_details is gets updated with every transaction performed by the user.
But here the logic of my system is
select sum(user_count ) from user_count_details
will always gives us the 0 and it is considered as the system is in stable state and everything is fine.
Now i want to write trigger which will check first when new request to update user_count come ,will hamper the sum(user_count )=0 or not and if it hampers that msisdn details will be captured in separate update table.
Based on your last comments, check if this works. Replace the other_table_name as per your scenario.
CREATE TRIGGER trgCheck_user_sum
BEFORE INSERT
ON user_count_details FOR EACH ROW
BEGIN
IF (select sum(user_count) from user_count_details) > 0 THEN
insert into other_table_name(msisdn) values(new.msisdn)
END IF
END

Populate a column on update (create too?), and why "FOR EACH ROW"?

I have a table of people who belong to various sites. These sites can change, but don't very often. So when we create an attendance record (a learner_session object) we don't store the site. But this has cause a problem in reporting how many training hours a site has, because some people have changed sites over the years. Not by much, but we'd like to get this right.
So I've added a site_at_the_time column to the learner_session table. I want to auto-populate this with the site the person was at when they attended the session. But I'm not sure how to reference this. For some reason (I'm guessing to speed development or something) the learner_id is allowed to be null. So I'm currently planning to do an update trigger. The learner_id shouldn't ever get updated, and if it ever did somehow, the entire record would be junk so I'm not worried about it overwriting it.
The trigger I have now is
create trigger set_site_at_the_time
after update of learner_id on lrn_session
begin
:new.site_at_the_time:= (select site_id from learner who where :new.learner_id = who.learner_id);
end;
which leads me to the following error:
ORA-04082: NEW or OLD references not allowed in table level triggers
Now, I've done some research and found I need to use a FOR EACH ROW - and I'm wondering what exactly this FOR EACH ROW does - is it every row captured by the trigger? Or is it every row in the table?
Also, will this trigger when I create a record too? So if I do insert into learner_session(id,learner_id,...) values(learner_session_id_seq.nextval,1234,...) will this capture that appropriately?
And while I'm here, I might as well see if there's something else I'm doing wrong with this trigger. But I'm mainly asking to figure out what the FOR EACH ROW is supposed to do and if it triggers properly. =)
FOR EACH ROW means that the trigger will fire once for each row that is updated by your SQL statement. Without this clause, the trigger will only fire once, no matter how many rows are affected. If you want to change values as they're being inserted, you have to use FOR EACH ROW, because otherwise the trigger can't know which :new and :old values to use.
As written, the trigger only fires on update. To make it also fire upon insert, you'd need to change the definition:
CREATE TRIGGER set_site_at_the_time
BEFORE INSERT OR UPDATE OF learner_id
ON lrn_session
FOR EACH ROW
BEGIN
SELECT site_id into :new.site_at_the_time
FROM learner who
WHERE :new.learner_id = who.learner_id);
END set_site_at_the_time;

trigger insert and update oracle error

Friend, I have question about cascade trigger.
I have 2 tables, table data that has 3 attributes (id_data, sum, and id_tool), and table tool that has 3 attributes (id_tool, name, sum_total). table data and tool are joined using id_tool.
I want create trigger for update info sum_total. So , if I inserting on table data, sum_total on table tool where tool.id_tool = data.id_tool will updating too.
I create this trigger, but error ora-04090.
create or replace trigger aft_ins_tool
after insert on data
for each row
declare
v_stok number;
v_jum number;
begin
select sum into v_jum
from data
where id_data= :new.id_data;
select sum_total into v_stok
from tool
where id_tool=
(select id_tool
from data
where id_data= :new.id_data);
if inserting then
v_stok := v_stok + v_jum;
update tool
set sum_total=v_stok
where id_tool=
(select id_tool
from data
where id_data= :new.id_data);
end if;
end;
/
please give me opinion.
Thanks.
The ora-04090 indicates that you already have an AFTER INSERT ... FOR EACH ROW trigger on that table. Oracle doesn't like that, because the order in which the triggers fire is unpredictable, which may lead to unpredictable results, and Oracle really doesn't like those.
So, your first step is to merge the two sets of code into a single trigger. Then the real fun begins.
Presumably there is only one row in data matching the current value of id_data (if not your data model is rally messed up and there's no hope for your situation). Anyway, that means the current row already gives you access to the values of :new.sum and :new.id_tool. So you don't need those queries on the data table: removing those selects will remove the possibility of "mutating table" errors.
As a general observation, maintaining aggregate or summary tables like this is generally a bad idea. Usually it is better just to query the information when it is needed. If you really have huge volumes of data then you should use a materialized view to maintain the summary, rather than hand-rolling something.

Resources