SQL to find out changes

Hi,
I have a table with 50 columns, lets call it Table_A . There is a identical table called Table_B. The data in Table_B gets flushed every night and data from Table_A is moved to Table_B. A new set of data is inserted into Table_A. Now I need to find out which field value is changed and what is changed . A minus command can give the changed records, but how to get the what was prior value for each changed field . Here is what I am looking for
WITH TABLE_A AS  (  SELECT 1 ID, 'JOHN' NAME,  'SALES' DIV FROM DUAL
UNION
        SELECT 2 ID, 'MARRY' NAME,  'ACCOUNTS' DIV FROM DUAL
UNION
        SELECT 3 ID, 'KIM' NAME,  'SERVICE' DIV FROM DUAL),
TABLE_B AS  (  SELECT 1 ID, 'JOHN' NAME,  'SALES' DIV FROM DUAL
UNION
        SELECT 2 ID, 'MARRY' NAME,  'ACCOUNTS' DIV FROM DUAL
UNION
        SELECT 3 ID, 'KIM' NAME,  'SALES' DIV FROM DUAL) 
SELECT * FROM TABLE_A
MINUS
SELECT * FROM TABLE_B
what i want is something that will spit out this (only one record, because there is only one diff record found in the above query.)
ID,     OLD_ID,     NAME,   OLD_NAME ,  DIV ,       OLD_DIV
3           3       KIM       KIM       SERVICE     SALESThe above one is for sample purpose, The actual table has 50 fields, and except the ID field, all other field values might change.
All ideas and solutions are appreciated.
Thanks in advance.

>
I understand your frustration. Here is the scenario. There are set of 5 tables. 1 master and 4 child tables ( and an identical set of 5 tables called "prior" tables, to store the flushed data from the "current" 5 tables). Every night data comes in XML files, the data from the 5 "prior" tables get flushed out . The data from the 5"current" table set moves to 5 "prior" tables. Then XML data gets loaded into the 5 "current"tables. Now once XML gets loaded into the original table ,I have to find out the diffs in those 5 tables(between current and prior) and generate result sets listing only the columns that have changed value. So what is current value and what was the prior value(from the prior table) side by side .
>
I'm not frustrated. It's just that any suggestions we make are solely based on the information provided. Sometimes posters start focusing on the solution they want to use (which is ok for their viewpoint) and don't explain the actual problem they are trying to solve. That often limits our ability to understand the options.
Now that you posted the above there is another option you could use that might better deal with some of the issues people have mentioned.
That solution is to compare the XML versions of the data. You haven't mentioned HOW you load that XML data into relational tables (what tool and process) but there is one VERY USEFUL feature of data in XML format:
ALL data is already in TEXT format (excluding the obvious LOB possibilities, which you also haven't mentioned).
That means ALL datatypes can use simple string comparison to detect differences: numbers, dates, character data. The actual meaning doesn't matter. If even a single byte is different the value can be considered different.
Oracle uses XML and XSLT functionality as the base for their DBMS_METADATA_DIFF package. That package is used to compare schemas or databases to detect object (e.g. tables, views) differences and to automatically produce DDL for ALTER statements that can modify an object to have the same structure as the objects being compared.
I suggest that you consider doing your comparisons on the XML versions of the data. Each night you would compare the new XML file data against the previous night's XML data. You can use XSLT to do the comparison and also to produce an XML file that contains the differences. Then you can use that 'difference' file as the source of your process to ultimately display the data.
An additional advantage of doing the XML comparisons is that you can do them asynchronously as soon as the new file is available. And the, if you don't really need the 'change' info in the database you never need to load it. Just transform the change XML into an Excel file or flat file format for users to load into Excel for review.
Do you license the partitioning option? If so then another suggestion would be to stop doing your nightly 'flush and move'. Recreate your 'prior' tables as partitioned tables that have just ONE range partition; a MAXVALUE partition that covers ALL data. Then each night you would:
1. TRUNCATE the prior table
2. Do an EXCHANGE partition between the 'current' table and the associated 'prior' table. Very fast, metadata only - no data actually gets moved.
3. Load the 'current' table with new data.
The above process avoid any actual data movement between the 'current' and 'prior' tables.
Not picking on you but you also haven't said how that new daily 'current' data is being used. If the only reason you are loading it into a table is to compare it to yesterday's data you could just do that with XML like I mentioned above.
Or you could load the XML itself into a table (e.g. XMLTYPE) and uses Oracle's built-in XDB fuctionality and do the XML comparison 'inside' the database. Then load a standard heap table from the XML table when you need to.
Maybe the above helps explain why the more info we have about the actual end-to-end problem you have the more options we can suggest for dealing with it. You are the only one that can determine if an option is viable for your particular use case and constraints.
Updated to add links to Oracle's XML functionality
See the XMLDiff function that you can use IN the database if you load the XML files themselves. From the SQL Language doc
http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions241.htm#SQLRF20025
>
Purpose
The XMLDiff function is the SQL interface for the XmlDiff C API. This function compares two XML documents and captures the differences in XML conforming to an Xdiff schema. The diff document is returned as an XMLType document.
For the first two arguments, specify the names of two XMLType documents.
For the integer, specify a number representing the hashLevel for a C function XmlDiff. If you do not want hashing, set this argument to 0 or omit it entirely. If you do not want hashing, but you want to specify flags, then you must set this argument to 0.
For string, specify the flags that control the behavior of the function. These flags are specified by one or more names separated by semicolon. The names are the same as the names of constants for XmlDiff function.
See Also:
Oracle XML Developer's Kit Programmer's Guide for more information on using this function, including examples, and Oracle Database XML C API Reference for information on the XML APIs for C
Examples
The following example compares two XML documents and returns the difference as an XMLType document:
>
See Oracle' own XML Developer's Kit for out-of-the-box functionality for 'diffing' XML documents:
http://docs.oracle.com/cd/E11882_01/appdev.112/e23582/adx_c_diff.htm
>
21 Determining XML Differences Using CThe Oracle XDK includes components that help you to determine the differences between the contents of two XML documents and then to apply the differences (patch) to one of the XML documents.
This chapter contains these topics:
Overview of XMLDiff in C
Using XmlDiff
Using XmlPatch
Using XmlHash
Overview of XMLDiff in C
You can use Oracle XmlDiff to determine the differences between two similar XML documents. XmlDiff generates an Xdiff instance document that indicates the differences. The Xdiff instance document is an XML document that conforms to an XML schema, the Xdiff schema.
You can then use XmlPatch, which takes the Xdiff instance document and applies the changes to other documents. This can be used to apply the same changes to a large number of XML documents.
XmlDiff only supports the DOM API for input and output.
XmlPatch also supports the DOM for the input and patch documents.
XmlDiff and XmlPatch can be used through a C API or a command line tool, and they are exposed by two SQL functions.
An XmlHash C API is provided to compute the hash value of an XML tree or subtree. If hash values of two trees or subtrees are equal, the trees are identical to a very high probability.
>
Did you notice that part about 'XmlDiff generates an Xdiff instance document that indicates the differences'? That is EXACTLY what your initial question was asking about, albeit from a DB perspective.
But XML is XML, DIFF is DIFF, a date/number/text are all strings. So XML 'database data' works the same as XML 'test or complex hierarchy' data.
Here is a pretty good writeup of how to do XML diff. See the XMLDIFF2 section for the best Oracle sample:
http://ellebaek.wordpress.com/2011/02/01/comparing-xml-in-oracle/
>
XMLDIFF2
The algorithm in our extended XMLDIFF function that we’re going to call XMLDIFF2 is:
1.Prepare CLOB variable for constructing the difference XML.
2.Find minimum number of leading elements that form primary key values (unique lookup).
3.Look for primary key values in XML1 not present in XML2: These primary key values have been deleted.
4.Look for primary key values in XML2 not present in XML1: These primary key values have been inserted.
5.Diff all common primary key values, ie in both XML1 and XML2.
6.Return NULL if no differences found.

Similar Messages

  • SQl to find out the last  business day of a given week

    Can any one please let me know what will be the SQL to find out the last business day (monday-friday) of a week?
    Edited by: 863997 on Oct 6, 2011 10:40 AM

    Assuming for "this week" that you want "last week Friday," then here is the SQL:
    TIMESTAMPADD(SQL_TSI_DAY,-1,TIMESTAMPADD(SQL_TSI_DAY, DAYOFWEEK(CURRENT_DATE)*-1, CURRENT_DATE))
    If you want the "nth" business day of the month, click on this link here:
    Re: calculation of Business day in OBIEE

  • Sql to find out combination of true and false

    Hi all,
    Could any one provide the sql to find out combination of true and false . For eg: if I have 3 elements a , b c i would like to get below o/p
    a   b  c
    t t t
    t t f
    t f f
    f f f
    t f t
    f f t
    your reply is greatly appreated - thank you

    Do you mean something like this (all permutations) ?
    SQL> with t as (
      2    select 't' bool from dual union all select 'f' from dual)
      3  select a.bool a,b.bool b,c.bool c
      4  from t a,t b,t c
      5  /
    A B C
    t t t
    t t f
    t f t
    t f f
    f t t
    f t f
    f f t
    f f fIf not, then you already got correct answers regarding boolean type in sql.
    Best regards
    Maxim

  • How to find out changes to standard objects

    Hi,
    Is there any way to find out which all standard objects have been modified, either via a table or FM or any other way?
    Have tried SE95 but it also gives objects changed during last upgrade.
    Any help would be appreciated.
    Thanks in advance.
    Peter

    >
    durga kottapalli wrote:
    > Hi,
    > check this link
    > http://i-sap.blogspot.com/2007/11/change-documents.html
    >
    > it would help you
    > Thanks and regards
    > durga.K
    That has absolutely nothing to do with what OP is asking. So it wouldn't help him.

  • Sql to find out non-standard dba_data_file

    version 8i-10g
    few dbfile were created in $OH/dbs dir in few databases in tivoli tablespace (can be dropped and re-cretaed without issues) , i am working on this to fix that.
    what would be sql script to create report of all databases , having db files created in $OH/dbs location in tablespace like 'TIV%'
    may be need help adjust this to show db name ....
    SQL> select TABLESPACE_NAME, FILE_NAME
    2 from dba_data_files
    3 where TABLESPACE_NAME like 'TIV%'
    4 ;
    TABLESPACE_NAME
    FILE_NAME
    TIVOLIORTS
    /u01/app/oracle/product/8.1.6/dbs/tivoliorts.dbf

    It might be actually easier if you approach the problem the other way round - by finding out which database each of the files in the directory belong. This can be done by issuing a fuser command on the file to see which processes have the file open and then a ps command to see the database that has it opened. Something like:
    $ fuser swf_th_data01_01.dbf
    swf_th_data01_01.dbf:     9900o    6917o   28306o     846o   29413o   29411o   13633o   13615o   13600o   13176o   13146o   13134o   13120o   13100o   13090o
    $ ps -ef |grep 9900
      oracle  9900     1   0   Mar 01 ?           0:10 oracledoith7 (LOCAL=NO)Edited by: Lakmal Rajapakse on 05-Mar-2010 08:25

  • SQL to find out Objects used by

    Hi,
    How can I find out the list of objects which are using an object? e.g. Table temp_1 is used by procedure p_test11 and function F_test22... Is there any way to find out the list of these to objects with their type?

    These dictionary table you can refer to.
    SQL> select * from dictionary where table_name like '%DEPENDENCIES%'
      2  /
    TABLE_NAME           COMMENTS
    DBA_DEPENDENCIES     Dependencies to and from objects
    USER_DEPENDENCIES    Dependencies to and from a users objects
    ALL_DEPENDENCIES     Dependencies to and from objects accessible to the user
    ALL_REFRESH_DEPENDEN Description of the detail tables that materialized views depend on for
    CIES                 refresh
    DBA_APPLY_VALUE_DEPE Synonym for _DBA_APPLY_CONSTRAINT_COLUMNS
    NDENCIES
    DBA_APPLY_OBJECT_DEP Synonym for _DBA_APPLY_OBJECT_CONSTRAINTS
    ENDENCIES
    TABLE_NAME           COMMENTS
    6 rows selected.

  • How to find out changes in order in APO

    Hi,
    I have with me order number. I want to know what changes are done in APO in particular time period through which transactions.
    I mean I want to see changes particular order in particular time period.
    I tried with /SAPAPO/C3
    But in 24 hours period there are thousands of log it is not possible  search one by one.
    By anyway can I filter logs pertaining to one production order.
    Regards,
    Santosh

    Thanks both.
    I was searching for changes in APO through ECC only.
    Our problem is suddenly few production orders are loosing charcteristics(we are using CDP).
    We are working with SAP for last one month, but no luck.
    To findout reason we were copying active planning version twice a week and also monitoring for new production order with lost charcteristics.
    While comparing versions we found that one receipt element ( we are using multi item order)is added in production order which may be reason for loss of charcteristic.
    to our surprise  there is no change in production order in ECC during this period.
    I cannot think of APO changing firmed receipt element production order.
    NO CCR was done for this order during this period.
    As we know day and not exact time when this happened wanted to have logs based prodction  order.
    It seems it is not possible?
    Is there anyway?
    Regards,
    Santosh

  • Sql to find all Sysadmin logins in EBS

    Hi ,
    I am using this sql to find out all Sysadmin logins in my EBS environment.
    SELECT user_name "User Name",
    to_char(start_time, 'MM-DD-YYYY HH24:MI:SS') "Login Time",
    to_char(end_time, 'MM-DD-YYYY HH24:MI:SS') "Logout Time"
    FROM apps.fnd_logins a,
    apps.fnd_user b
    WHERE a.user_id = b.user_id
    AND b.user_name IN ('SYSADMIN', 'MOBDEV', 'MOBADM')
    AND TRUNC(start_time) > TRUNC(SYSDATE) - 1
    AND login_type = 'FORM'
    ORDER BY start_time;
    But, somehow I am unable to relate this logins to database sessions id. Is these any direct sql to find out all session info from gv$session / gv$process whenever there is a sysadmin login.
    EBS 11.5.10.2
    DB 11.2.0.2

    Hi
    HusseinNone of the query gives a correct output.
    For example , when I enter the user_name as SYSADMIN for the query , the v$session.action gives result as "FRM:GXGONG:ACCO CA IND Shipping "..this is the forms session info for "GXGONG" user who doesn't have the sysadmin responsibility.
    SELECT distinct(s.sid),
    s.serial#,
    p.spid,
    s.process,
    substr(to_char(s.logon_time,'mm-dd-yy hh24:mi:ss'),1,20) Logon_Time,
    s.action,
    s.module,
    fu.user_name,
    fip.ipaddress
    FROM
    V$SESSION s, V$PROCESS p,fnd_oam_forms_rti fip, fnd_logins fl, fnd_user fu
    WHERE s.paddr = p.addr
    AND s.process = fip.PID
    AND fl.pid = p.pid
    AND fl.user_id = fu.user_id
    AND s.username IS NOT NULL
    and fu.user_name IN ('SYSADMIN', 'MOBDEV', 'MOBADM')
    and s.action like 'FRM%'
    and s.logon_time > trunc(sysdate) -1
    1047     48328     28016     31638     03-25-13 13:04:55     FRM:JPJUSTIC:ACCO ITEM/INVENTORY     INVTVQOH     SYSADMIN     10.32.33.95:64961
    3772     63791     32428     30206     03-25-13 22:10:16     FRM:EXFINKLE:ACCO INVENTORY ADJU     WIPTXSFM     SYSADMIN     10.32.82.104:46034
    3425     48215     18143     14036     03-25-13 21:26:09     FRM:GXGONG:ACCO CA IND Shipping      WMSCTLBD     SYSADMIN     172.20.23.102:33548
    774     51097     21922     26155     03-25-13 21:12:15     FRM:JXLIM:ACCO PURCHASING APPLIC     POXPOVPO     SYSADMIN     10.43.2.16:47312
    1442     21606     20941     21354     03-25-13 07:58:41     FRM:MDJAMES:ACCO Shipping Execut     FNDRSRUN     SYSADMIN     10.32.33.127:6350
    5150     17379     16824     31376     03-25-13 16:21:49     FRM:GXROY:ACCO Receivables Colle     ARXCWMAI     SYSADMIN     172.25.16.120:60150
    5117     45721     16824     31376     03-25-13 16:09:17     FRM:GXROY:ACCO Receivables Colle     ARXCWMAI     SYSADMIN     172.25.16.120:60150
    3447     151     18143     14036     03-26-13 00:26:44     FRM:GXGONG:ACCO CA IND Shipping      FNDRSRUN     SYSADMIN     172.20.23.102:33548
    5147     4240     16824     31376     03-25-13 16:08:54     FRM:GXROY:ACCO Receivables Colle     ARXCUDCI     SYSADMIN     172.25.16.120:60150
    5107     62706     16824     31376     03-25-13 16:11:17     FRM:GXROY:ACCO Receivables Colle     ARXCUDCI     SYSADMIN     172.25.16.120:60150
    5168     4367     16824     31376     03-25-13 16:38:00     FRM:GXROY:ACCO Receivables Colle     ARXCUDCI     SYSADMIN     172.25.16.120:60150
    3450     59009     18143     14036     03-26-13 00:29:13     FRM:GXGONG:ACCO CA IND Shipping      INVMATWB     SYSADMIN     172.20.23.102:33548
    5137     45737     16824     31376     03-25-13 16:13:01     FRM:GXROY:ACCO Receivables Colle     OEXOETEL     SYSADMIN     172.25.16.120:60150
    3461     16761     18143     14036     03-25-13 21:25:39     FRM:GXGONG:ACCO CA IND Shipping      INVMATWB     SYSADMIN     172.20.23.102:33548
    434     26798     24756     17190     03-25-13 16:20:57     FRM:MXBAKER:Direct US CSR User     OEXOEORD     SYSADMIN     10.32.33.123:50117
    5142     31731     16824     31376     03-25-13 16:20:46     FRM:GXROY:ACCO Receivables Colle     ARXCWMAI     SYSADMIN     172.25.16.120:60150
    1716     11892     24609     27641     03-26-13 00:36:16     FRM:BAMALAGO:ACCO INVENTORY ADJU     WIPTXSFM     SYSADMIN     10.32.82.143:27123

  • How to find out Orphan Process

    Hi
    We are using Windows clients and connect to Oracle Database Server. For some reason if the Client machine is rebooted when an application is running, The Process still shown in the V$Session and it take hours to get released from the V$Session.
    Is there any sql to find out such Orphan Process?
    Thanks in advance
    S Muralidharan
    Procon Systems Pvt. Ltd.

    http://download-west.oracle.com/docs/cd/B10501_01/network.920/a96581/sqlnet.htm#437587
    Read the limitations and side effects before implementing.

  • Find out Large objects in Shared pool

    Hi,
    I am working on Oracle 10.2.0 and 11.2.0 databases.
    Could you please give the sql to find out the Large objcts in Shared pool.
    Thanks a lot

    Hi;
    I suggest please review:
    Shared Pool script
    Re: regarding shared pool
    Regard
    Helios

  • How to find out if SQL execution plan is changed proactively using job/grid

    Hello,
    Can you help me on How to find out if SQL execution plan is changed proactively using job/grid control?
    Thank you..
    -

    The answers so far are supposed to show ways how to see that a plan changed after the fact - that is not really proactive but that was the question.
    A way to see a plan change proactively would be to create a SQL Plan Baseline for the SQL statement with the 'good old' plan and then watch DBA_SQL_PLAN_BASELINES for new rows with that SQL_HANDLE which would indicate that a new execution plan was computed - although not yet used.
    Kind regards
    Uwe Hesse
    "Don't believe it, test it!"
    http://uhesse.com

  • How to find out PL/SQL function usage in Discoverer workbooks?

    We have to make changes to one PL/SQL function that has been registered in Administrator. Is it possible to find out in which reports the function is used? The function is called in calculations and it returns certain time information. We have to add one argument to the function and therefore have to change calculations where the function is called. We have too many workbooks to manually check them all.
    I am aware how to find out which folders and items are used in workbooks but can't find the same information about functions. We are using Discoverer 10.1.2.2
    Thanks in advance!

    Hi,
    If you calculation is in the EUL you can look at the item dependencies, but if your calculation is in the workbook there is no way other than using the workbook dump (d51wkdmp.exe) utility or opening the workbook and manually checking.
    You might want to consider overlaying the PL/SQL function definition so that there are 2 variants of the function. You can then add the extra parameter as an optional parameter in Discoverer. The correct variant will be called depending on how many parameters are used.
    Hope that helps,
    Rod West

  • Find out which program/FM changes a table..

    Hi guys,
    a table is changed and i don't know the program/FM/job which causes this. Of course i looked
    at the "where use list" but didn't find out. I know the exactly time (+/- 5 sec.) of change.
    Is there a possibility to find out via SQL Trace? Or how to do that?
    Thanks

    Hi,
    'DBTABLOG' is a standard table which is used to "Log Records of Table Changes."
    You can try by giving your table name , date & time as input and get Program Name, Transaction Code which has updated your table.
    For this in your table's technical settings "Log data changes" has to be checked.
    Regards
    Balaji R

  • SQL Server Software ISO, How to find out what is the Edition and Version of the SQL software before starting installation

    Hi All-
    Before starting installation, I would like to find out What EDITION and VERSION  is the SQL Server Software ISO.
    Please note this is not to find out what version is the installed sql server but before the installation: Is there a note or abrv. that describes what kind of Edition and version is the ISO?
    Detail explanation is appreciated.
    Thanks.
    belayzeleka

    @Balmukund - Thanks for the input - That one is a nice blog!
    But assuming you can`t change the Product Key value (you are not provided that number, as you know it gets populated by itself) can you find out
    what implies the corresponding edition? If there is a folder where this reference is store, could you provide the full file path where  this folder is located?
    The ideal solution would be a way to find out what the Edition
    is befor we start installation.
    Thanks!
    Also just to add what I came across:
    The SQL Server 2012 installation software comes with pretty much all edition in it, that kind of makes it difficult to easily identify what edition is the specific ISO, but when
    one downloads from MSDN one must downloads the
    right edition either Standard,
    Enterprise, etc so that when you install it, it installs whatever edition is downloaded.
    The best source would be whoever downloaded the software.
    Also, before going too far, you can find out what edition it is being installed right before the setup wizard finishes on the
    License Terms page (Microsoft Software License Terms Page) and if the edition is not the one you intended to install, you can cancel it there and everything will be rolled back- no hastle.
    belayzeleka

  • How can we find out data in an editable ALV grid has been changed or not?

    Hi Experts,
    How can we find out whether a data in an editable ALV grid has been changed or not.
    I am using the
    FM -> REUSE_ALV_GRID_DISPLAY_LVC
    for ALV display.
    I have to chekc whther data has been changed or not befor saving. if changed then only i want to
    SAVE
    . I cannot use the internal table comparison method for this purpose also i am not using OOP ALV.
    So kindly sugest me an alternative.
    Thanks and Regards,
    Shahana

    Hi,
    Thanks for your answer. I already saw this post.
    See this method.
    CALL METHOD reuse_alv_grid->check_changed_data
    IMPORTING
    e_valid = lv_check.
    This will update the internal table with the edited values. Then we can go for internal table comparison.
    But my scenario will not allow me for itab comparisons.I just want to know the ALV data has been changed or not.
    Regards,
    Shahana

Maybe you are looking for