SQL merge and after insert or update on ... for each row fires too often?
Hello,
there is a base table, which has a companion history table
- lets say USER_DATA & USER_DATA_HIST.
For each update on USER_DATA there has to be recorded the old condition of the USER_DATA record into the USER_DATA_HIST (insert new record)
- to have the history of changes to USER_DATA.
The first approach was to do the insert for the row trigger:
trigger user_data_tr_aiu after insert or update on user_data for each rowBut the performance was bad, because for a bulk update to USER_DATA, there have been individual inserts per records.
So i tried a trick:
Instead of doing the real insert into USER_DATA_HIST, i collect the USER_DATA_HIST data into a pl/sql collection first.
And later i do a bulk insert for the collection in the USER_DATA_HIST table with stmt trigger:
trigger user_data_tr_ra after insert or update on user_dataBut sometimes i recognize, that the list of entries saved in the pl/sql collection are more than my USER_DATA records being updated.
(BTW, for the update i use SQL merge, because it's driven by another table.)
As there is a uniq tracking_id in USER_DATA record, i could identify, that there are duplicates.
If i sort for the tracking_id and remove duplicate i get exactly the #no of records updated by the SQL merge.
So how comes, that there are duplicates?
I can try to make a sample 'sqlplus' program, but it will take some time.
But maybe somebody knows already about some issues here(?!)
- many thanks!
best regards,
Frank
Hello
Not sure really. Although it shouldn't take long to do a test case - it only took me 10 mins....
SQL>
SQL> create table USER_DATA
2 ( id number,
3 col1 varchar2(100)
4 )
5 /
Table created.
SQL>
SQL> CREATE TABLE USER_DATA_HIST
2 ( id number,
3 col1 varchar2(100),
4 tmsp timestamp
5 )
6 /
Table created.
SQL>
SQL> CREATE OR REPLACE PACKAGE pkg_audit_user_data
2 IS
3
4 PROCEDURE p_Init;
5
6 PROCEDURE p_Log
7 ( air_UserData IN user_data%ROWTYPE
8 );
9
10 PROCEDURE p_Write;
11 END;
12 /
Package created.
SQL> CREATE OR REPLACE PACKAGE BODY pkg_audit_user_data
2 IS
3
4 TYPE tt_UserData IS TABLE OF user_data_hist%ROWTYPE INDEX BY BINARY_INTEGER;
5
6 pt_UserData tt_UserData;
7
8 PROCEDURE p_Init
9 IS
10
11 BEGIN
12
13
14 IF pt_UserData.COUNT > 0 THEN
15
16 pt_UserData.DELETE;
17
18 END IF;
19
20 END;
21
22 PROCEDURE p_Log
23 ( air_UserData IN user_data%ROWTYPE
24 )
25 IS
26 ln_Idx BINARY_INTEGER;
27
28 BEGIN
29
30 ln_Idx := pt_UserData.COUNT + 1;
31
32 pt_UserData(ln_Idx).id := air_UserData.id;
33 pt_UserData(ln_Idx).col1 := air_UserData.col1;
34 pt_UserData(ln_Idx).tmsp := SYSTIMESTAMP;
35
36 END;
37
38 PROCEDURE p_Write
39 IS
40
41 BEGIN
42
43 FORALL li_Idx IN INDICES OF pt_UserData
44 INSERT
45 INTO
46 user_data_hist
47 VALUES
48 pt_UserData(li_Idx);
49
50 END;
51 END;
52 /
Package body created.
SQL>
SQL> CREATE OR REPLACE TRIGGER preu_s_user_data BEFORE UPDATE ON user_data
2 DECLARE
3
4 BEGIN
5
6 pkg_audit_user_data.p_Init;
7
8 END;
9 /
Trigger created.
SQL> CREATE OR REPLACE TRIGGER preu_r_user_data BEFORE UPDATE ON user_data
2 FOR EACH ROW
3 DECLARE
4
5 lc_Row user_data%ROWTYPE;
6
7 BEGIN
8
9 lc_Row.id := :NEW.id;
10 lc_Row.col1 := :NEW.col1;
11
12 pkg_audit_user_data.p_Log
13 ( lc_Row
14 );
15
16 END;
17 /
Trigger created.
SQL> CREATE OR REPLACE TRIGGER postu_s_user_data AFTER UPDATE ON user_data
2 DECLARE
3
4 BEGIN
5
6 pkg_audit_user_data.p_Write;
7
8 END;
9 /
Trigger created.
SQL>
SQL>
SQL> insert
2 into
3 user_data
4 select
5 rownum,
6 dbms_random.string('u',20)
7 from
8 dual
9 connect by
10 level <=10
11 /
10 rows created.
SQL> select * from user_data
2 /
ID COL1
1 GVZHKXSSJZHUSLLIDQTO
2 QVNXLTGJXFUDUHGYKANI
3 GTVHDCJAXLJFVTFSPFQI
4 CNVEGOTDLZQJJPVUXWYJ
5 FPOTZAWKMWHNOJMMIOKP
6 BZKHAFATQDBUVFBCOSPT
7 LAQAIDVREFJZWIQFUPMP
8 DXFICIPCBCFTPAPKDGZF
9 KKSMMRAQUORRPUBNJFCK
10 GBLTFZJAOPKFZFCQPGYW
10 rows selected.
SQL> select * from user_data_hist
2 /
no rows selected
SQL>
SQL> MERGE
2 INTO
3 user_data a
4 USING
5 ( SELECT
6 rownum + 8 id,
7 dbms_random.string('u',20) col1
8 FROM
9 dual
10 CONNECT BY
11 level <= 10
12 ) b
13 ON (a.id = b.id)
14 WHEN MATCHED THEN
15 UPDATE SET a.col1 = b.col1
16 WHEN NOT MATCHED THEN
17 INSERT(a.id,a.col1)
18 VALUES (b.id,b.col1)
19 /
10 rows merged.
SQL> select * from user_data_hist
2 /
ID COL1 TMSP
9 XGURXHHZGSUKILYQKBNB 05-AUG-11 10.04.15.577989
10 HLVUTUIFBAKGMXBDJTSL 05-AUG-11 10.04.15.578090
SQL> select * from v$version
2 /
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for Linux: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - ProductionHTH
David
Similar Messages
-
Before and After insert or update rowcount
Hi
I have several extract objects procs which are calling various build objects procs which in turn are inserting or updating the tables...now when i run these objects sometimes i get no errors and everything seems to be running perfectly but the tables do not get updated ? now what i am trying to achieve here is get some kind of summary where i can see the before and after rowcount..for that i have created a table of every extract object proc and another table with the coressponding tables being updated by tht proc...can anyone pls tell me how should i look into these procs and how do i get the rowcount for a particular table before and after the procedure was run ?
Thanks a lot in advanceHi,
On which version of RDBMS are you working , because if you're on 10g then just enable auditing or even FGA on the tables were you want to see the changes,
Then query the DBA-AUDIT (not sure of the name anymore) view.
If not then create a audit package yourself.
something like
create or replace package pck$audit as
procedure prc$check_tablecount(p_tablename IN VARCHAR2, p_status IN VARCHAR2);
end;
create or replace package body pck$audit as
procedure prc$check_tablecount(p_tablename IN VARCHAR2,p_status IN VARCHAR2) IS
sqlstr VARCHAR2(100);
v_count NUMBER;
begin
sqlstr := 'SELECT COUNT(*) FROM '||p_tablename;
EXECUTE IMMEDIATE sqlstr into v_count; --could by 'using v_count' check syntax for this
INSERT INTO audit_table(table_name,total_count,status,time_stamp)
VALUES(p_tablename,v_count,p_status,SYSTIMESTAMP);
COMMIT;
end;
end;
you can elaborate on this creating additional procedure checking differences in total_count on same table at same time.
Now call this procedure before starting your insert-delete- on your table make sure you put the parameters correct eg p_status => 'begin procedure'
and call it again after your commit in your procedure.
Hope this helps you out
Erwin -
AFTER INSERT OR UPDATE TRIGGER
All of my tables have a dateTime field which is used to track when a record was inserted/updated. I would like a trigger on each table that updates the dateTime field with the current date and time after each insert or update. I keep getting a mutating error and I can't quit wrap my brain around how to fix it. Could anyone provide a example.
I know I am getting the error because I am trying to update the row that is currently being inserted or updated - what is the best way to handle this?
ThanksHi,
A trigger before insert or update is better for your case :
For example :
SQL> desc tab_param
DELAI_RETENTION_FLUX NOT NULL NUMBER(5)
DATE_DEBUT_ALARME NOT NULL DATE
DATE_DEBUT_HITSTORIQUE NOT NULL DATE
NB_FLUX_TOTAL NOT NULL NUMBER(5)
NB_FLUX_PAGE NOT NULL NUMBER(5)
DATE_COL DATE
CREATE OR REPLACE TRIGGER TEST_TRG BEFORE INSERT OR
UPDATE OF DATE_DEBUT_ALARME, DATE_DEBUT_HITSTORIQUE,
DELAI_RETENTION_FLUX, NB_FLUX_PAGE, NB_FLUX_TOTAL
ON TAB_PARAM REFERENCING OLD AS old NEW AS new
FOR EACH ROW
begin
:new.date_col:=sysdate;
end;
Nicolas. -
After insert or update calculate column
question about a table.
I have a table called table1
AFTER data is inserted or updated into the table1
I want to run an update on the same table (table1) with a case statement
update table1
set column2 = case
WHEN column1 = 'abc' THEN 'testl'
WHEN column1 = 'def' THEN 'test2'
ELSE 'test3'
END;
commit;
END;
i cant use a trigger because i get error since the insert would be on same table.
What else can i use for this update ? I cant use a stored procedure that runs daily or something like that because the column2 in the table needs to be populated immediately after an insert or update
thanks for any ideasSQL> CREATE TABLE TABLE1
2 (
3 COLUMN1 VARCHAR2(10),
4 COLUMN2 VARCHAR2(10)
5 );
Table created.
SQL> CREATE OR REPLACE TRIGGER TRG_TABLE1
2 BEFORE INSERT OR UPDATE OF COLUMN1 ON TABLE1
3 FOR EACH ROW
4 BEGIN
5
6 IF :NEW.COLUMN1 = 'abc' THEN
7 :NEW.COLUMN2 := 'test1';
8 ELSIF :NEW.COLUMN1 = 'def' THEN
9 :NEW.COLUMN2 := 'test2';
10 ELSE
11 :NEW.COLUMN2 := 'test3';
12 END IF;
13
14 END;
15 /
Trigger created.
SQL> INSERT INTO TABLE1(COLUMN1) VALUES('abc');
1 row created.
SQL> SELECT * FROM TABLE1;
COLUMN1 COLUMN2
abc test1
SQL> UPDATE TABLE1 SET COLUMN1 = 'def';
1 row updated.
SQL> SELECT * FROM TABLE1;
COLUMN1 COLUMN2
def test2
SQL>
{code} -
AFTER INSERT OR UPDATE TRIGGER the insert not working
The update works but the insert is not working. I have two version that I have tried. I amd executing the inserts/update from Oracle Applications and the table row is either being inserted or updated correctly.
DROP TRIGGER APPS.CCC_HZ_ORG_CONTACTS_ARU;
CREATE OR REPLACE TRIGGER APPS.CCC_HZ_ORG_CONTACTS_ARU
/* --Created By: SKELLEHER
--Creation Date: 07/15/09
--Last Updated By:
--Last Update Date:
AFTER INSERT OR UPDATE OF department, job_title
ON apps.hz_org_contacts
FOR EACH ROW
WHEN (
NEW.department <> OLD.department OR
NEW.job_title <> OLD.job_title
DECLARE
v_ChangeType VARCHAR2(10);
BEGIN
/* Use 'I' for Insert, 'U'' for Update, and'D' for delete(not part of app at this time). */
IF INSERTING THEN
v_ChangeType := 'INSERT';
ELSIF UPDATING THEN
v_ChangeType := 'UPDATE';
ELSE
v_ChangeType := 'DELETE';
END IF;
INSERT INTO cust.ccc_tca_po_tf_event_tbl
VALUES (ccc_tca_po_tf_event_tbl_s.NEXTVAL
, 'hz_org_contacts'
, v_ChangeType
, 'org_contact_id'
, :NEW.org_contact_id
, 'PENDING'
, NULL
, 0
, 'N'
, 'Y'
, :NEW.LAST_UPDATED_BY
, :NEW.LAST_UPDATE_DATE
, -1
, SYSDATE
, 'DEPARTMENT_JOBTITLE_UPDATE'
,'LDAP'
END;
AND I TRIED ANOTHER VERSION:I have simplified it so it's just checking for insert but it's not picking up. Is the syntax IF INSERTING correct?
CREATE OR REPLACE TRIGGER APPS.CCC_HZ_ORG_CONTACTS_ARU
AFTER INSERT OR UPDATE
ON ar.hz_org_contacts
FOR EACH ROW
DECLARE
BEGIN
IF INSERTING THEN
INSERT INTO cust.ccc_tca_po_tf_event_tbl
VALUES (ccc_tca_po_tf_event_tbl_s.NEXTVAL
, 'hz_org_contacts'
, 'UPDATE'
, 'org_contact_id'
, :NEW.org_contact_id
, 'PENDING'
, NULL
, 0
, 'N'
, 'Y'
, :NEW.LAST_UPDATED_BY
, :NEW.LAST_UPDATE_DATE
, -1
, SYSDATE
, 'DEPARTMENT_JOBTITLE_UPDATE'
,'LDAP'
END IF;
END;
/ -
:new., lob columns and after insert ... for each row trigger
I'm having this little problem with
my update/insert triggers and lob columns.
I have this little trigger:
CREATE OR REPLACE TRIGGER BLOB_DATA_INSERT
AFTER INSERT ON BLOB_DATA
FOR EACH ROW
BEGIN
INSERT INTO BLOB_DATA_CHANGE VALUES(CHANGE_SEQ.NEXTVAL, 'I', :NEW.ID,:NEW.DATA);
END;
Which works except when the DATA column
is a BLOB or CLOB (There is no data in :NEW.DATA. I even tried some of the DBMS_LOB package procedures)
The one thing that is different for the lob columns is that the application updating
the data is using a bind statement with returning, like:
INSERT INTO BLOB_DATA
VALUES(:ID,EMPTY_BLOB())
RETURNING DATA INTO :DATA;
COMMIT;
Thanks for any helpI'm having this little problem with
my update/insert triggers and lob columns.
I have this little trigger:
CREATE OR REPLACE TRIGGER BLOB_DATA_INSERT
AFTER INSERT ON BLOB_DATA
FOR EACH ROW
BEGIN
INSERT INTO BLOB_DATA_CHANGE VALUES(CHANGE_SEQ.NEXTVAL, 'I', :NEW.ID,:NEW.DATA);
END;
Which works except when the DATA column
is a BLOB or CLOB (There is no data in :NEW.DATA. I even tried some of the DBMS_LOB package procedures)
The one thing that is different for the lob columns is that the application updating
the data is using a bind statement with returning, like:
INSERT INTO BLOB_DATA
VALUES(:ID,EMPTY_BLOB())
RETURNING DATA INTO :DATA;
COMMIT;
Thanks for any help -
had an update for a facetime bug and after clicking the update my phone locked up with an icone to plug phone into itunes. I tried to reinstall but no luck
You would want to follow this link to help with this issue
http://support.apple.com/kb/HT1808 -
Difference between Before INSERT and After INSERT trigger?
What is difference between Before INSERT and After INSERT triggers? Can anyone give me a simple example from SCOTT schema for both of these triggers.
The documentation gives a good explanation have you looked at....
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm#sthref1175 -
Revision: 5473
Author: [email protected]
Date: 2009-03-20 16:02:58 -0700 (Fri, 20 Mar 2009)
Log Message:
Fix for bug around handling of constraints during and after effects were run, both for old and new effects. Fix was to correctly record and handle styles that were set or not set in either state and to temporarily set width/height values for the duration of an effect if we disabled the relevant styles that affect width/height.
QE Notes: None
Doc Notes: None
Bugs: SDK-17809
Reviewer: Jason
Testing: checkintests, Mustella Effects, ListDataEffects
Ticket Links:
http://bugs.adobe.com/jira/browse/SDK-17809
Modified Paths:
flex/sdk/trunk/frameworks/projects/flex4/src/mx/effects/effectClasses/FxAnimateInstance.a s
flex/sdk/trunk/frameworks/projects/framework/src/mx/effects/Effect.asRevision: 5473
Author: [email protected]
Date: 2009-03-20 16:02:58 -0700 (Fri, 20 Mar 2009)
Log Message:
Fix for bug around handling of constraints during and after effects were run, both for old and new effects. Fix was to correctly record and handle styles that were set or not set in either state and to temporarily set width/height values for the duration of an effect if we disabled the relevant styles that affect width/height.
QE Notes: None
Doc Notes: None
Bugs: SDK-17809
Reviewer: Jason
Testing: checkintests, Mustella Effects, ListDataEffects
Ticket Links:
http://bugs.adobe.com/jira/browse/SDK-17809
Modified Paths:
flex/sdk/trunk/frameworks/projects/flex4/src/mx/effects/effectClasses/FxAnimateInstance.a s
flex/sdk/trunk/frameworks/projects/framework/src/mx/effects/Effect.as -
Hi, I have an iPod touch 2G with an iOS of 4.2.1 and there are no updates available for my device, I want to download an older version of iBooks that is compatible with my device. Pls what can I do?
- The 2G iPod can only go as high as iOS 4.2.1. It does not have the hardware to support a higher iOS version.
- Your only way is if you have the version that was compatible on your computer such as in the recycle bin/trash or in a computer file backup. In that case delete the current version that is in your iTunes App library and replace it with the older version. Then sync.
- You can only download the current version from the App store. -
Hi, I am using a iphone 3g and i did not update it for 4 years now i have updated the software and the phone is not connecting to itunes with the error flashing error 11556
https://discussions.apple.com/thread/4977025
-
Update button in sql report for each row can only get the report value
I have to added Update for each row of records like:
select a.*,'Update' button from (
select c.*,d.login from country c,champ d
where c.champ_id=d.champ_id(+)
order by c.name) a
then go to the report->edit button ->Column Link
I added some items P40_REGION, P40_CHAMPID,P40_ACTIVE,P40_CODE and assign the value like #region#,#active# ,#code# to these item, but when I click the update button in the report I found the values such as #region#,#active# ,#code# is the report value not the select list selected value(the region, active column are displayed as select list lov).
Can anyone tell me how to sort this knid of problem?
Thanks a lot!Sorry, it doesn't work. I also tried apex_application.g_f01(i), but not everytime I can get the value or get the incorrect value. If you have time you can try to create it like what I have described. Our team use two days now but still can not sort this problem.
-
I just bought and installed logic pro x. and after instal it is asking for synapse audio DUNE registration.. where do I get that?
http://www.synapse-audio.com/
-
Update Button for every Row of Report
hello,
is it possible to get an update button for every row of a report?
After clicking this button, only this row of the report should be updated.
thanks in advance
daveCreate a hidden item on your page. Call it somehow - P1_KEY. Create other hidden fields
for the rest of the columns you want to update. Create a link per report line. This link will be
inserting the primary key of your record into P1_KEY and the other values you want to
update in the other hidden fields. It will branch to the same page and contain a request - i.e. UPDATE_ROW. Then
you create a process - On Load Before Header - which will
take the value from your item P1_KEY and update the record
in the database by setting the the column values to the
values in your hidden fields. This process will fire upon
request you specified. However, this way you will not be
able to update your text areas, since you will not be able
to transfer this over a link. Also you need to be carefull
about special characters.
Maybe there is a better proposal than this.
Denes Kubicek -
Recording a last updated time for each record in a table
hi
i have a table with 5 columns. and the table has 100 rows of data.
i need to add a sixth column and this column should contain the last updated time of that particular row.
i am allowed to use triggers.
for example, if the 55th row undergoes a change, the 6th column's value for the 55th row should update itself with that particular time.
can any one of you come up with an optimized solution for this please ???
thanx a million in advance and best regards,
novice DBA
:)Hi,
Here's an example:
CREATE OR REPLACE TRIGGER table_x_biu
BEFORE INSERT OR UPDATE ON table_x
FOR EACH ROW
BEGIN
:NEW.modified_dt := SYSDATE;
END table_x_biu
SHOW ERRORSwhere table_x is your table name, and modified_dt is the DATE column to be populated.
You won't have to specify modified_dt in any INSERT or UPDATE statements. In fact, if you do, the value you give will be ignored, and the trigger will always substitute SYSDATE.
WARNING: if you do get an error message, the line number reported will usually be relative to the first DECLARE or BEGIN statement in the trigger. For example, if I had mis-spelled SYSDATE in the example above, the error message would say there was a problem in line 2, not line 5.
Maybe you are looking for
-
ITunes was unable to load dataclass information from Sync Services-Solution
Hello all. I recently upgraded to iTunes 9.1.1.12 on a laptop with Windows XP Pro and the latest iPhone OS (as of May 16,2010). Right after that, I started getting the "iTunes was unable to load dataclass information from Sync Services" error message
-
Opening pdf in jsf whithout normal download
hi all i need to open pdf file when i click the menu in my project. i dont want a normal link since i need to set the response with cache enabled only when i click the link. i dont want a normal download too. when i click the menu it should open dire
-
Interface builder, running script when app opens
Hi! I am trying to expand my wake script, so I have thrown it into xcode. What my wake script does is that it opens itunes and start to slowly turn up the volume, then it updates my ipod and does a few other things. Now this works great in script edi
-
Calculated MIC from the results of MICs from different operations
Hi , I have requirement in which Operation 0010 have one MIC "A" and operation 0020 have MIC "B" and the third operation have an calculated MIC "C" which is caluclated by value of MIC A - MIC B. How to map this in standard SAP. I know this can be map
-
Only the first of multiple cutaways show (PNGs)
I'm trying to overlay one PNG at a time across a timeline, using the "cutaway" feature. But only the first PNG appears, and then iMovie ignores the rest during playback. All the PNGs have transparency. Any ideas what might be happening? I'm using t