Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
991 views
in Technique[技术] by (71.8m points)

oracle - SQL - Find statement that insert specific values

I'm working with an Oracle database, interacting with it both from Oracle SQL Developer and from R. This database is shared between several people, but because of some company rules we all use the same user to interact with it.

In one of our tables we found a strange behaviour, were a line is constantly inserted without any order that we know of.

Say, on Table1, sometimes there is a line with value Name = "Test" created >8K times, and we can't figure out why is this happening.

What I wanted to do is to create some sort of automatism (triger) that, when a line with Name = "Test" is inserted on Table1 saves the query that did it and the date/time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If there are archive logs for the search period. You must use the utility LogMiner. Example 1.

EXECUTE DBMS_LOGMNR.add_logfile(LOGFILENAME => '/oracle/app/oracle/product/11.2/admin/edcu/arc_redo_log/1_39306_769799469.dbf', OPTIONS => DBMS_LOGMNR.NEW); 
EXECUTE DBMS_LOGMNR.add_logfile(LOGFILENAME => '/oracle/app/oracle/product/11.2/admin/edcu/arc_redo_log/1_39307_769799469.dbf', OPTIONS => DBMS_LOGMNR.addfile); 
EXECUTE DBMS_LOGMNR.add_logfile(LOGFILENAME => '/oracle/app/oracle/product/11.2/admin/edcu/arc_redo_log/1_39308_769799469.dbf', OPTIONS => DBMS_LOGMNR.addfile); 
EXECUTE DBMS_LOGMNR.add_logfile(LOGFILENAME => '/oracle/app/oracle/product/11.2/admin/edcu/arc_redo_log/1_39309_769799469.dbf', OPTIONS => DBMS_LOGMNR.addfile); 
EXECUTE DBMS_LOGMNR.START_LOGMNR(OPTIONS => DBMS_LOGMNR.DICT_FROM_ONLINE_CATALOG);

SELECT 
to_char(timestamp,'DD-MM-YYYY HH24:MI:SS'),
operation,username,  
os_username,
machine_name,
session_info,
sql_redo

 FROM v$logmnr_contents
where 
seg_owner='MANAGER' and  seg_name='TEST2'

14-12-2017 09:40:20 DDL     MANAGER DeminDV KRWIVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRWIVC-PTK-DEMIN  create table test2 (p1 number);
14-12-2017 09:40:47 INSERT  MANAGER DeminDV KRWIVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRWIVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1") values (HEXTORAW('c117'));
14-12-2017 09:40:53 INSERT  MANAGER DeminDV KRWIVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRWIVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1") values (HEXTORAW('c119'));
14-12-2017 09:40:57 INSERT  MANAGER DeminDV KRWIVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRWIVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1") values (HEXTORAW('c137'));
14-12-2017 09:41:01 INSERT  MANAGER DeminDV KRWIVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRWIVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1") values (HEXTORAW('c20219'));
14-12-2017 09:41:45 DDL     MANAGER DeminDV KRWIVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRWIVC-PTK-DEMIN  alter table test2 add (p2 varchar2(200));
14-12-2017 09:42:12 INSERT  MANAGER DeminDV KRWIVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRWIVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1","COL 2") values (HEXTORAW('c20219'),HEXTORAW('746573743220746573743120'));
14-12-2017 09:42:24 INSERT  MANAGER DeminDV KRWIVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRWIVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1","COL 2") values (HEXTORAW('c2021a'),HEXTORAW('746573743420746573743420'));
14-12-2017 09:46:24 INSERT  MANAGER DeminDV KRWIVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRWIVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1","COL 2") values (HEXTORAW('c2021a'),HEXTORAW('746573743420746573743420'));
14-12-2017 09:46:25 INSERT  MANAGER DeminDV KRWIVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRWIVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1","COL 2") values (HEXTORAW('c2021a'),HEXTORAW('746573743420746573743420'));
14-12-2017 09:46:26 INSERT  MANAGER DeminDV KRWIVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRWIVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1","COL 2") values (HEXTORAW('c2021a'),HEXTORAW('746573743420746573743420'));
14-12-2017 09:46:27 INSERT  MANAGER DeminDV KRWIVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRWIVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1","COL 2") values (HEXTORAW('c2021a'),HEXTORAW('746573743420746573743420'));
14-12-2017 09:46:28 INSERT  MANAGER DeminDV KRWIVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRWIVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1","COL 2") values (HEXTORAW('c2021a'),HEXTORAW('746573743420746573743420'));
14-12-2017 09:54:37 DDL     MANAGER DeminDV KRWIVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRWIVC-PTK-DEMIN   comment on table test2 is  'test';
14-12-2017 10:16:36 DDL     MANAGER DeminDV KRWIVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRWIVC-PTK-DEMIN  alter table test2 add  (p3 varchar2(100));
14-12-2017 10:17:07 INSERT  MANAGER DeminDV KRWIVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRWIVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("P1","P2","P3") values ('125','test6','test4 ');
14-12-2017 10:17:08 INSERT  MANAGER DeminDV KRWIVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRWIVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("P1","P2","P3") values ('125','test6','test4 ');
'

Example 2. Using Flashback Features

   update manager.test2 set p2='test10' where p1=125;
    commit;

    SELECT versions_xid XID, versions_startscn START_SCN,
      versions_endscn END_SCN, versions_operation OPERATION,
      p1, p2, p3 FROM manager.test2
    VERSIONS BETWEEN SCN MINVALUE AND MAXVALUE;

XID                                         START_SCN           END_SCN       OPERATION       P1     P2     P3                                                                                                   
---------------------- ------------------------ ---------------------- --------------- ---------- -------- ------
08000E000DD70900        1374388524564                                                 U          125 test10   test4                                                                                                
08000E000DD70900        1374388524564                                                 U          125 test10   test4                                                                                                
08000E000DD70900        1374388524564                                                 U          125 test10                                                                                                            
08000E000DD70900        1374388524564                                                 U          125 test10                                                                                                                       
                                                               1374388524564                     125 test4 test4    
                                                               1374388524564                     125 test4 test4    
                                                               1374388524564                     125 test6 test4 
                                                               1374388524564                     125 test6 test4      



            SELECT  xid, start_scn , commit_scn COMMIT,operation OP, logon_user ,undo_sql FROM flashback_transaction_query
                   WHERE xid = HEXTORAW('08000E000DD70900');

    XID                      START_SCN  COMMIT                OP    LOGON_USER                           UNDO_SQL                                                                                             
-------------------------------- -------------------------------- -------------------------------------- --------------------------------- ------------------------------------                                                        
        08000E000DD70900    1374388524537   1374388524564   UPDATE  MANAGER update "MANAGER"."TEST2" set "P2" = 'test6' where ROWID = 'AAA2wgAAbAAAAEsAAM';
        08000E000DD70900    1374388524537   1374388524564   UPDATE  MANAGER update "MANAGER"."TEST2" set "P2" = 'test6' where ROWID = 'AAA2wgAAbAAAAEsAAL';
        08000E000DD70900    1374388524537   1374388524564   UPDATE  MANAGER update "MANAGER"."TEST2" set "P2" = 'test4 test4 ' where ROWID = 'AAA2wgAAbAAAAEsAAK';
        08000E000DD70900    1374388524537   1374388524564   UPDATE  MANAGER update "MANAGER"."TEST2" set "P2" = 'test4 test4 ' where ROWID = 'AAA2wgAAbAAAAEsAAJ';
        08000E000DD70900    1374388524537   1374388524564   UPDATE  MANAGER update "MANAGER"."TEST2" set "P2" = 'test4 test4 ' where ROWID = 'AAA2wgAAbAAAAEsAAI';
        08000E000DD70900    1374388524537   1374388524564   UPDATE  MANAGER update "MANAGER"."TEST2" set "P2" = 'test4 test4 ' where ROWID = 'AAA2wgAAbAAAAEsAAH';
        08000E000DD70900    1374388524537   1374388524564   UPDATE  MANAGER update "MANAGER"."TEST2" set "P2" = 'test4 test4 ' where ROWID = 'AAA2wgAAbAAAAEsAAG';
        08000E000DD70900    1374388524537   1374388524564   UPDATE  MANAGER update "MANAGER"."TEST2" set "P2" = 'test4 test4 ' where ROWID = 'AAA2wgAAbAAAAEsAAF';
        08000E000DD70900    1374388524537   1374388524564   BEGIN   MANAGER 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...