How to create audit triggers and run it dynamically?

DECLARE
V_CRT VARCHAR2(4000);
V_DRP VARCHAR2(200);
BEGIN
FOR tab_rec IN (SELECT TABLE_NAME FROM USER_tabLES WHERE TABLE_NAME NOT LIKE '%TMP' AND TABLE_NAME NOT LIKE '%BAK' AND TABLE_NAME NOT LIKE 'MV_%') loop
v_DRP := 'DROP TABLE AUD_'||tab_rec.tABLE_name ||';' || CHR(13);
v_crt := ' CREATE TABLE AUD_'||tab_rec.tABLE_name
||'( ACTION VARCHAR2(1) ,
CONSTRAINT AUD_'||SUBSTR(TAB_REC.TABLE_NAME, 1,11)||substr(tab_rec.table_name,length(tab_rec.table_name)-4,5)||'_CHKACTION CHECK (ACTION IN (''I'',''U'',''D'')),
ISSUE_TIMESTAMP TIMESTAMP DEFAULT SYSDATE,
ISSUE_USER VARCHAR2(30),
ISSUE_HOST VARCHAR2(40),
FOR col_rec IN (SELECT column_name,data_type,data_length,data_precision,data_scale FROM user_tab_cols WHERE table_name = tab_rec.table_name ORDER BY column_id)
loop
IF col_rec.data_type = 'VARCHAR2' THEN
v_crt := v_crt || col_rec.column_name || ' '|| col_rec.data_type||'('||col_rec.data_length||'),'||chr(13)||' ';
ELSIF col_rec.data_type = 'NUMBER' THEN
v_crt := v_crt || col_rec.column_name || ' '||col_rec.data_type;
IF col_rec.data_precision IS NOT NULL THEN
v_crt:= v_crt||'('||col_rec.data_precision||','||col_rec.data_scale||'),'||chr(13)||' ';
ELSE
v_crt:= v_crt||','||chr(13)||' ';
END IF;
ELSE
v_crt := v_crt || col_rec.column_name || ' '||col_rec.data_type||','||chr(13)||' ';
END IF;
END loop;
v_crt := rtrim(v_crt);
v_crt := substr(v_crt,1,LENGTH(v_crt)-2)||');';
DBMS_OUTPUT.PUT_LINE(v_crt);
v_crt:= '';
END LOOP;
END;
The audit tables are named same as the table prefixed with AUD_. The audit table has two columns ACTION and ISSUE_TIMESTAMP more than the main tables.
This anonymous block creates the triggers in the table which has audit tables dynamically.
The user data is not maintained here in this code.
This code runs.
DECLARE
v_output1 VARCHAR2(5000);
v_insert VARCHAR2(4000);
v_delete VARCHAR2(4000);
v_update VARCHAR2(4000);
v_cols VARCHAR2(1000);
v_columns VARCHAR2(2000)DEFAULT '';
v_columns_new VARCHAR2(2000) DEFAULT '';
v_columns_old VARCHAR2(2000) DEFAULT '';
v_tab1 VARCHAR2(30) DEFAULT ' ';
v_tab2 VARCHAR2(30) DEFAULT ' ';
v_tab3 VARCHAR2(30) DEFAULT ' ';
v_schema varchar2(30) default 'schema'; --
v_user_id VARCHAR2(30); -- not used in this program
v_host VARCHAR2(30);
BEGIN
/* The audit tables are created with table name prefixed with AUD and columns ACTION AND ISSUE DATE, ISSUE_USER,ISSUE_HOST added.*/
FOR rec IN
(SELECT tname FROM tab WHERE tname LIKE 'AUD%'
LOOP
v_columns := '';
v_columns_new := '';
v_columns_old := '';
v_output1 := 'CREATE OR REPLACE TRIGGER '
||v_schema||'.' ||rec.tname ||'_TRIG ' || chr(13)
||' AFTER ' || chr(13)
||' INSERT OR '|| chr(13)
||' UPDATE OR '|| chr(13)
||' DELETE ON '|| chr(13)
||v_schema||'.'
|| substr(rec.tname,5)
|| chr(13)
||' FOR EACH ROW '
||chr(13)
||' BEGIN ' || chr(13);
FOR colrec IN
(SELECT column_name
FROM user_tab_cols
WHERE table_name = rec.tname
AND column_name NOT IN ('ACTION','ISSUE_TIMESTAMP','ISSUE_USER','ISSUE_HOST')
LOOP
v_columns := v_columns||v_tab3||colrec.column_name ||','||chr(13);
v_columns_new := v_columns_new ||v_tab3||':new.'||colrec.column_name ||','||chr(13) ;
v_columns_old := v_columns_old ||v_tab3||':old.'||colrec.column_name ||','||chr(13) ;
END LOOP;
v_columns := SUBSTR(v_columns ,1,LENGTH(v_columns ) -2);
v_columns_new := SUBSTR(v_columns_new,1,LENGTH(v_columns_new)-2);
v_columns_old := SUBSTR(v_columns_old,1,LENGTH(v_columns_old)-2);
v_insert := v_tab1||'IF INSERTING THEN ' || chr(13)
||v_tab2||'INSERT INTO '||v_schema||'.' ||rec.tname|| chr(13)
|| v_tab2||' ( '||chr(13)
|| v_tab3||'ACTION,'||chr(13)
|| v_tab3||'ISSUE_TIMESTAMP,' ||chr(13)
|| v_tab3||'ISSUE_USER,'||chr(13)
|| v_tab3||'ISSUE_HOST,'||chr(13)
||v_columns ||chr(13) ||v_tab2||')' || chr(13)
||v_tab2||' VALUES ' ||chr(13)
||v_tab2||' (' ||chr(13)
||v_tab3||'''I'''||','||chr(13)
||v_tab3||'SYSTIMESTAMP'||','||chr(13)
||v_tab3||'USER'||','||chr(13)
||v_tab3||'sys_context('||'''USERENV'''||','||'''HOST'''||'),'||chr(13)
||v_columns_new ||v_tab2||');' ||chr(13)
||v_tab1||'END IF;'
|| CHR(13);
v_delete := v_tab1||'IF DELETING THEN ' || chr(13)
||v_tab2||'INSERT INTO '||v_schema||'.' ||rec.tname|| chr(13)
||v_tab2|| '( '||chr(13)
|| v_tab3||'ACTION,'||chr(13)
|| v_tab3||'ISSUE_TIMESTAMP,' ||chr(13)
|| v_tab3||'ISSUE_USER,'||chr(13)
|| v_tab3||'ISSUE_HOST,'||chr(13)
||v_columns ||chr(13)
||v_tab2||')' || chr(13)
||v_tab2||' VALUES ' ||chr(13)
||v_tab2||' (' ||chr(13)
||v_tab3||'''D'''||','||chr(13)
||v_tab3||'SYSTIMESTAMP'||','||chr(13)
||v_tab3||'USER'||','||chr(13)
||v_tab3||'sys_context('||'''USERENV'''||','||'''HOST'''||'),'||chr(13)
||v_columns_old
||v_tab2||');' ||chr(13)
||v_tab1
||'END IF;' ||CHR(13);
v_update := v_tab1||'IF UPDATING THEN ' || chr(13)
||v_tab2||'INSERT INTO '||v_schema||'.' ||rec.tname|| chr(13)
||v_tab2|| '( '||chr(13)
||v_tab3|| 'ACTION,'||chr(13)
||v_tab3|| 'ISSUE_TIMESTAMP,' ||chr(13)
||v_tab3||'ISSUE_USER,'||chr(13)
||v_tab3||'ISSUE_HOST,'||chr(13)
||v_columns ||chr(13)
||v_tab2||')' || chr(13)
||v_tab2||' VALUES ' ||chr(13)
||v_tab2||' (' ||chr(13)
||v_tab3||'''U'''||','||chr(13)
||v_tab3||'SYSTIMESTAMP'||','||chr(13)
||v_tab3||'USER'||','||chr(13)
||v_tab3||'sys_context('||'''USERENV'''||','||'''HOST'''||'),'||chr(13)
||v_columns_new
||v_tab2||');' ||chr(13)
||v_tab1||'END IF;'|| chr(13) ;
-- v_output1 := v_output1 || v_insert || v_update|| v_delete ||CHR(13)||'END;';
--EXECUTE IMMEDIATE v_output1;
-- v_output1 := v_output1 || chr(13)||'/';
dbms_output.put_line(v_output1); generate script
dbms_output.put_line(v_output1);
dbms_output.put_line(v_insert);
dbms_output.put_line(v_update);
dbms_output.put_line(v_delete);
dbms_output.put_line('END;');
dbms_output.put_line('/');
END LOOP;
END;
show errors;
Edited by: ranjus on Oct 17, 2012 3:33 PM

Thank you so much for your suggestions.
I went through the link you suggested.
I wondered why DBA requested me to create triggers when such easy option was available to them. And I found the reason why triggers for audit.
When Triggers Are Necessary
Avoiding False Positives. Audit trails are generated through autonomous transactions from the original transactions. Hence they are committed even if the original transactions are rolled back.
Here is a simple example to illustrate the point. Assume that we have set up auditing for UPDATEs on table CLASS. A user issues a statement to update a data value from 20 to 10 and then rolls it back as shown below.
update class set size = 10 where class_id = 123;
rollback
Now the value of the column SIZE will be 20, not 10, as if the user never did anything. However, the audit trail will capture the change, even if it's rolled back. This may be undesirable in some cases, especially if there are lots of rollbacks by users. In such a case, you may have to use the trigger to capture only committed changes. If there were a trigger on the table CLASS to insert records into the user defined audit trail, upon rollback the audit trails would have been rolled back too
Capturing Before-change Values. Oracle-provided audit trails do not show the values before and after the change. For instance, the above change will create an audit record that shows the statement and the SCN number at the change, but not the value before the change (20). The value can be obtained from the SCN number using flashback query, but it depends on the information being available in the undo segments. If the information is not captured within the limit specified by the undo_retention period, the prior values can never be retrieved. Using a trigger guarantees that the values are captured without dependence on the undo_retention period, and may prove useful at times. Under these two circumstances you may decide to continue using triggers to record the audit trails at a granular detail.

Similar Messages

  • Can any 1 tell me how to create budget journal and how to enter journal ?

    hi all+
    Can any one tell me how to create budget journal and how to enter journal against that budget ?+
    Regards+
    Yasir+

    http://download.oracle.com/docs/cd/A60725_05/html/comnls/us/gl/budjrnl.htm#index-Budget-journals-Entering--0
    Enter budget journals to maintain an audit trail for your budget balances. You can use budget rules to calculate budget journal amounts automatically.
    When you post budget journals, the journal amounts update existing budget balances. You can review and change your budget journals before posting them.
    Attention: When you use budget rules in Journal Mode, General Ledger calculates the appropriate debit or credit needed to achieve the balance you enter for the account type.
    Prerequisites
    1. Define a budget
    2.Open one or more years for your budget
    3.Define a budget organization
    4. Assign the "Entered" budget entry type to the accounts for which you want to enter budget journals
    To enter budget journals for a single account:
    1. Navigate to the Enter Budget Journals window.
    2. Specify the Budget Organization for the account to which you want to budget. If the budget organization is password-protected, you must enter the password before you can enter budget journals.
    3. Enter the Budget you want to update. You cannot use a budget that is frozen.
    4. Enter the range of Accounting Periods to which you want to budget.
    5. Enter the Currency of the budget amounts you are entering. The accounts must be assigned to the budget organization for this currency.
    6. Choose Journal Mode from the region poplist to enter budget amounts in a journal format.
    You can also use Single Row Mode or Worksheet Mode to enter budget journal amounts. However, you can only generate budget journals from these entry modes when you use the Enter Budget Journals window.
    Additional Information: When you use Journal Mode, Balance Type is a display-only field. It displays Budget when you are entering budget journals. In the Enter Journals window, this field displays Actual when you are entering actual journals.
    7. Enter or query the Account to which you want to budget. You can also switch to Worksheet Mode to easily query accounts, then return to Journal Mode to enter budget journals.
    8. Enter a Debit or Credit amount for each period. Do not enter journal amounts if you want to use budget rules to calculate and distribute budget amounts.
    9. Choose Create Journals to create a budget journal batch. If you are using budgetary control, you specify a funds action when you create the batch.

  • How to create po through batch run

    Hi,
    how to create po through batch run (through background job)
    Prashanth

    Hi Prashanth ,
    Run the below program as a background job.
    Make sure that inforecord exists for all and automatic PO are ticked in Vendor and Material Master.
    Program : RM06BB30
    Create a suitable variant.
    Job Creation : Tcode : SM36
    Regards
    Ramesh Ch

  • How to create transparent image at run-time?

    How to create transparent image at run-time? I mean I want to create a (new) transparent image1, then show other (loaded) transparent image2 to image1, then show image1 to a DC. The problem is - image1 has non-transparent background...

    i'm not sure, but you can set the alpha value to 0 in all pixels which are 'in' the background..
    greetz
    chris

  • Is it possible to create a thread and run it in background in nokia ??

    Is it possible to create a thread and run it in background in nokia series 40 mobile phones using j2me ??

    Probably a good question for ForumNokia. If you mean start up a thread and run in the background while your MIDlet UI does other things, then sure, why not. If you mean that you want to exit your MIDlet, but leave a thread running in the background, then I don't believe you can do this on series 40. S60 is another story since it supports multiple tasks.

  • How to create a partner and header record using CRM_ORDER_MAINTAIN?

    Hi any one knows how to create a partner and header record using the function module CRM_ORDER_MAINTAIN??
    I tried to  create a record, but i only managed to create a header record and the partner record is not reflected in the transaction.  Why is that so? is there any indicator that i need to include?
    Thanks..
    Jen

    Hi Jen!
    I use this FM and it works perfectly.
    Use this to create a partner:
      gs_partner-ref_handle    = '0000000001'.
      gs_partner-ref_kind      = 'A'.
      gs_partner-ref_partner_handle = '0001'.
      gs_partner-partner_fct   = '00000001'.
      gs_partner-partner_no    = NO_PARTNER. "number of the partner, bu_partner
      gs_partner-display_type  = 'BP'.
      gs_partner-no_type       = 'BP'.
      gs_partner-kind_of_entry = 'C'.
    *  ls_partner_l-ref_handle    = '1'.
      gs_partner-ref_guid      = '00000000000000000000000000000000'.
      APPEND gs_partner  TO gT_partner .
      ls_input_field-ref_kind  = 'A'.
      ls_input_field-logical_key   = '0001'.
      ls_input_field-objectname  = 'PARTNER'.
      ls_input_field-ref_handle  = '0000000001'.
      ls_input_field_names-fieldname = 'DISPLAY_TYPE'.
      INSERT ls_input_field_names INTO TABLE ls_input_field-field_names.
      ls_input_field_names-fieldname = 'KIND_OF_ENTRY'.
      INSERT ls_input_field_names INTO TABLE ls_input_field-field_names.
      ls_input_field_names-fieldname = 'NO_TYPE'.
      INSERT ls_input_field_names INTO TABLE ls_input_field-field_names.
      ls_input_field_names-fieldname = 'PARTNER_FCT'.
      INSERT ls_input_field_names INTO TABLE ls_input_field-field_names.
      ls_input_field_names-fieldname = 'PARTNER_NO'.
      INSERT ls_input_field_names INTO TABLE ls_input_field-field_names.
      INSERT ls_input_field  INTO TABLE  gt_input_fields.
      clear ls_input_field-field_names[].
      CALL FUNCTION 'CRM_ORDER_MAINTAIN'
      EXPORTING
    *    it_schedlin_i   = gt_schedlin_i_com
        it_partner      = gt_partner
    *    it_sales        = gt_sales
    *      it_orgman       = gt_orgman
    *      it_appointment  = gt_appointment
    *      it_ordprp_i     = gt_ordprp_i
    *   it_product_i    = gt_product_i
    *      it_activity_i   = gt_activity_i
    *      it_pridoc       = gt_pridoc_com
      CHANGING
        ct_orderadm_h   = gt_orderadm_h
    *   ct_orderadm_i   = gt_orderadm_i
        ct_input_fields = gt_input_fields.
    *      ct_doc_flow     = gt_doc_flow
    *      cv_log_handle   = gv_log_handle.
    Hope it helps u,
    Regards,
    Mon.

  • How To Create A Technical And Business Systems For Web AS ABAP ???

    Hi Experts,
    How To Create A Technical And Business Systems For Web AS ABAP ???
    Please Let me Know All the Step-By-Step Process to Create ????
    Points Will be Given
    Regards
    Khanna

    Hi Sumit,
    When U Told the thing that first time to execute the RZ70 and All i Did this in XI System
    So I got An Entry for the Technical System for XI System.
    Now i Deleted that and Executed RZ70 in R/3.
    When I Executed RZ70, I got this Error.
    <b> "RFC Call failed: Error Opening an RFC Connection "</b>.
    Now I am Unable to see Any Technical System  Under Web As ABAP. It's Showing Empty Now.
    Please Let me know
    Regards
    Khanna

  • How to create the ICONS and SYMBOLS

    how to create the ICONS and SYMBOLS
    Title was edited by:
            Alvaro Tejada Galindo

    HI,
    Check this sample code....Not mine...
    REPORT SHOW_ICONS.
    TABLES: ICON.
    INCLUDE <ICON>.
    FIELD-SYMBOLS: <F>.
    SELECT * FROM ICON.
       ASSIGN (ICON-NAME) TO <F>.
       WRITE:   /(5) <F>, 20 '@',21 ICON-ID+1(2),23 '@',ICON-OLENG,
                ICON-BUTTON,ICON-STATUS,ICON-MESSAGE,ICON-FUNCTION,
                ICON-NAME.
    ENDSELECT.
    Also Try this,
    INCLUDE <symbol>.
    INCLUDE <icon>.
    Write:/ 'Phone Symbol:', SYM_PHONE AS SYMBOL.
    SKIP.
    WRITE: / 'Alarm Icon: ', icon_alarm AS ICON.
    For more information check out the following link it will help you.
    http://help.sap.com/saphelp_nw04/helpdata/en/9f/db9e4a35c111d1829f0000e829fbfe/content.htm
    Regards,
    Padmam.

  • How to create a package and add a file?

    Hi all,
    I am new to Java and very much much confussed with how to create a package and then include some files any help will be very thankfull.
    Thanks for your help
    kka.

    Steps for creating a package in java are as follows:
    Choose a base directory for your classes. For example, you might choose c:\java\packages. Type the following command:
    set CLASSPATH=%CLASSPATH%;c:\java\packages
    Create subdirectories for each chapter or section, if you don't already have them.
    For each of the classes in the subdirectories, add the following line to the very top of each file:
    package directory-name;
    where directory-name is the name of the subdirectory the class file is located in.
    In other directories you may have class files that need to access one of the classes in another directory (package). To do this, write one of the following at the top of the class that needs the other class:
    import subdir.*;
    or
    import subdir.classname;
    Use the class by name in the new class file.
    Note that you can create sub-packages by creating subdirectories of the original subdirectories, and inserting package statements at the top of the java files in those directories.
    Hope this helps!

  • How to create new user and How can i assign end user roles

    Hi,
    I am new to SAP, please explain how to create end users and their roles
    Thanks
    ravi

    Hi,
    Roles are decided by IT managers. Suppose if Persons who are working in shopfloor or production side
    give authorization to Production order create , change and Confirm like that etc
    1. In role maintenance (transaction PFCG), choose the Authorizations tab page.
    2. To change the authorization data for the transactions assigned to the role, choose Change Authorization Data or Expert Mode for Profile Generation. Otherwise, a dialog box appears in expert mode (see Regenerating an Authorization Profile After Changes).
    Please take telp from Basis person also refer this link,
    http://help.sap.com/saphelp_46c/helpdata/EN/52/6714a9439b11d1896f0000e8322d00/frameset.htm
    Thanks

  • How to create portal user and integrate with external appl login

    How to create portal user and integrate the user with external application for single sign-on ?
    I want to access my external application thru portal user ..?
    Shyam

    Hi Jithin,
    The link that you've shared talks about a different scenario.
    In my case, I want to pass the portal user id when the user clicks on the Help Link present in the header area.
    I am trying to pass it along with the Help Link Url property of a masthead iview but it is not getting passed to the target Url.
    I would like to know if it is possible to pass the Portal User Id in this way or not.
    Though if we create a appintegrator iview and pass the user id <User.UserID> along with the target Url, it reaches there.
    Thanks & Regards,
    Anurag

  • How we create custom infotype and how to configure with its subtypes.

    hai abap-hr gurus,
    how to create custom infotype and how to configure with its subtypes. when i am creating infotypes i am not getting how to configure subtypes.
    plz help me for this with an example code.
    thanks..
    kiran kumar

    Hi Kiran,
        Please fallow the below steps to create the custom infotype. If you have any quires let me know.
    For Creation of Infotype first Go to Transaction PM01, Enter the custom Infotype number which you want to create, it should be a 4 digit number and have to start with 9xxx.
    then select the `Employee Infotype' radio button
    after that select the `PS Structure Infotype'
    then click on Create… A separate table maintenance window appears
    then Create a PS structure with all the fields you want on the infotype
    Save and Activate the PS structure
    now Go back to the initial screen of PM01
    Click on `All' push button. It takes a few moments
    Click on `Technical Characteristics’. Infotype list screen appears
    Click on `Change'(pencil) button
    Now select your Infotype and click on `Detail' (magnifying glass) button
    Give `T591A' as subtype table & also Give `T591S' as subtype txt tab
    Give your subtype field as subtype field & Save and come back to PM01 initial screen
    Click on `Infotype Characteristics' … Infotype list screen appears
    Click on `Change' (pencil) button & on New Entries
    and then Enter your Infotype number and short text Here we have to set different Infotype Characteristics as per the requirement. (Better open another session with some standard Infotype's infotype characteristics screen and use as the reference
    to fill yours). Now save ur entries
    Now the Infotype is created and ready to use.
    If you want to change the layout of the Infotype as per your
    requirement…
    In the PM01 initial screen…Select `Screen' radio button and give
    2000 as the screen name, then click on edit.
    In the next screen.. Select `Layout Editor' and click `Change'.
    Screen default layout appears…here you can design/modify the
    screen..change the attributes of the fields..etc.
    Save and activate. (Don't forget to `Activate at every level)
    Regards,
    Ramakrishna kotha.

  • How to create process chains,and how to use process like and or xor

    Hi,
    How to create process chains,and how to use process like and or xor.
    can any one please give me a example in each.
    Thanks,
    cheta.

    Hi Cheta,
    Here is step by step procedure to create process chains
    Process chain is nothing but executing a process ..(or) loading the data any process we can do in background.. that means.. automatically we can execute our process based on Time or any event..
    Creating Process Chains
    Prerequisites
    If you want to include a load process in the process chain, you need to have already created an InfoPackage.
    You cannot load flat file data from a client workstation in the background. For this reason, you have stored your data on an application server.
    Creating Process Chains
    You have the option of creating a process chain in the process chain maintenance screen directly or by using a maintenance dialog for a process:
    Creating a Process Chain Directly in the Process Chain Maintenance Screen
    You are in the BW Administrator Workbench.
    1. Click on the Process Chain Maintenance icon in the AWB toolbar.
    The Process Chain Selection dialog window appears.
    2. Choose Create.
    3. Enter the technical name and a description of the chain, and confirm your entry.
    The Add Start Process dialog window appears.
    4. Create a variant for a start process.
    1. a. On the Maintain Start Process screen, choose whether you want to schedule the chain directly or whether you want to start it using a metachain.
    2. b. If you choose to schedule the chain directly, enter the start date value for the chain under Change Selections and save your entries.
    The Maintain Start Process screen appears again.
    3. c. Save your entries, return to the previous screen and confirm your entries in the Add Start Process dialog window.
    You are taken to the Plan View of the process chain maintenance screen.
    In the left-hand area of the screen, a navigation area is displayed. In the right-hand area of the screen, the process chain is displayed.
    5. Use the drag-and-drop function to add the relevant processes into your process chain.
    You use the Process Types function to select the processes. This sorts the process types according to different categories. You can also call up InfoPackages and processes for the data target from the separate InfoSources and Data Targets navigation trees.
    Hope this helps
    Regards
    Karthik

  • New user created. How to create Central Person and Business partner

    Hi,
    I got a new users created in SRM 5.0 .how to create Central person and business partner.
    Without CP & BP we cannot work. please guide.
    Regards
    G.Ganesh Kumar

    HI GANESH
    As you aware , assign the user to organisation structure via users_gen , the system will create a BP and Central Person , Position and user for you.
    br
    muthu

  • How to create a form and show it as a modal window in VB6?

    oform.modal  -> modal is read-only property
    help,please.

    Hi Santiago!
    HTH: How to create a form and show it as a modal window?

Maybe you are looking for