Insert if duplicate update..possible?

I want to do a trigger in oracle. In case a user tries to insert values in the table X and the value that he tries to put in column Y already exists I would like to actually update that row. is that possible? so a user tries the input, it checks to see if the value that he wants to put in column Y is already there, if it is, overwrite that row/entry, otherwise insert it. is it possible? 10x in adfance

Hi,
when you say "so a user tries the input, it checks to see if the value that he wants to put in column Y is already there"
Do you mean if the column is NOT NULL or you mean to say that the ROW you are inserting with respect to a PRIMARY KEY is present.
If possible could you please send us the TABLE structure.
thanks

Similar Messages

  • Insert delete or update entry in Custom table from KONV entry changed

    Hi All,
    I have custom table ZKONV with only few required columns and should have same number of records as KONV has at any point in time.
    KONV is a cluster table so its not readable from ORACLE level. So ZKONV is created. But I dont know how to keep these both tables in sync.
    I need to perform insert delete or update entry in Custom table if insert delete or update happens on cluster table KONV from any transactions.
    As KONV is a cluster table and does not have changed time stamp I am not able to know the number of records changed in perticulat time period.
    Thanks,

    Thanks for reply,
    There is a Outside SAP system which needs to read KONV data to feed into their system, but as KONV is cluster table they are not able to read it from ORACLE level.
    To solve this we are thinking to create a transparent Z-table and will fill it with KONV and catch Update, delete or Insert statement and do same on ZKONv.
    Is this possible some how? by some database event or something....

  • Insert - Ignore Duplicate Entry Error

    Hello
    I would like to use an insert statement which also contains a select statement in it and returns a set of records and gets inserted in to a particular table.
    But, when I tried to run the sql, it throws error as some of the records are duplicate entry insertions. Is it possible to modify the sql to ignore any duplicate entries and insert the new ones.
    Illustration:
    TABLE_A
    STUDENT_ID GROUP_ID
    101 200
    101 201
    103 200
    107 201
    and so on.
    Here, when I try to insert a set of records like
    101 200
    108 200
    101 300
    103 200
    103 201
    I would like the exact duplicate entries like (101,200), (103,200) to be ignored with out producing any error but insert the other records in to the TABLE_A table.
    Thr sql used is similar to this
    Insert into TABLE_A (student_id, group_id)
    select distinct person.studentid, groups.groupid from person, groups where
    Did any one know how to handle this?
    Thanks
    Shiv

    If I include 'NOT EXISTS' after insert and before
    select statement, it throws the error
    missing VALUES keyword
    The NOT EXISTS goes in the WHERE clause.
    Insert into Table_A (student_id, group_id)
    select distinct p.studentid, g.groupid
    from person p, group g
    where NOT EXISTS (select 1
                     from table_A a
                     where a.student_id = p.studentid
                     and a.group_id = g.groupid)or you could do
    Insert into Table_A (student_id, group_id)
    select distinct p.studentid, g.groupid
    from person p, group g
    MINUS
    select student_id, group_id
    from Table_A

  • A trigger that changes an insert into an update?

    This is probably a quite unusual question, but is it possible to create a trigger that changes an insert into an update?
    So, if someone tries to do something like this:
    INSERT INTO SOME_TABLE (column1, column2, column3) VALUES (value1, value2, value3);
    ...the trigger is able to change it into:
    UPDATE SOME_TABLE column1=value1, column2=value2, column3=value3 WHERE ID=1;
    Can it be done?

    Hi,
    You can do things like that in an INSTEAD OF INSERT trigger.
    See the PL/SQL manual for details:
    http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28370/create_trigger.htm#sthref2864
    INSTEAD of triggers only work on views. Of course, you can create a view as "SELECT * FROM some_table" just so you can use an INSTEAD OF trigger.

  • Instead of trigger example - INSERT works but UPDATE and DELETE does not?

    Below is a demostration script of what I am trying to troubleshoot. Tests are done on 10gR2;
    conn system/system
    drop table tt purge ;
    create table tt nologging as select * from all_users ;
    alter table tt add constraint pk_tt_user_id primary key (user_id) nologging ;
    analyze table tt compute statistics for table for all indexed columns ;
    conn hr/hr
    drop database link dblink ;
    create database link dblink
    connect to system identified by system
    using 'xe.turkcell' ;
    select * from global_name@dblink ;
    drop view v_tt ;
    create view v_tt as select username, user_id, created from tt@dblink order by 2 ;
    select count(*) from v_tt ;
    COUNT(*)
    13
    drop sequence seq_pk_tt_user_id ;
    create sequence seq_pk_tt_user_id
    minvalue 1000 maxvalue 99999
    increment by 1;
    create synonym tt for tt@dblink ;
    CREATE OR REPLACE PROCEDURE prc_update_tt(old_tt v_tt%ROWTYPE, new_tt v_tt%ROWTYPE) IS
    BEGIN
    IF old_tt.user_id != new_tt.user_id THEN
    RETURN; -- primary key
    END IF;
    IF old_tt.user_id IS NOT NULL AND new_tt.user_id IS NULL THEN
    DELETE FROM tt
    WHERE user_id = nvl(old_tt.user_id,
    -99);
    RETURN;
    END IF;
    IF (old_tt.username IS NULL AND new_tt.username IS NOT NULL) OR
    (old_tt.username IS NOT NULL AND new_tt.username != old_tt.username) THEN
    UPDATE tt
    SET username = new_tt.username
    WHERE user_id = nvl(old_tt.user_id,
    -99);
    END IF;
    IF (old_tt.created IS NULL AND new_tt.created IS NOT NULL) OR
    (old_tt.created IS NOT NULL AND new_tt.created != old_tt.created) THEN
    UPDATE tt
    SET created = new_tt.created
    WHERE user_id = nvl(old_tt.user_id,
    -99);
    END IF;
    END prc_update_tt;
    CREATE OR REPLACE PROCEDURE prc_insert_tt(old_tt v_tt%ROWTYPE, new_tt v_tt%ROWTYPE) IS
    new_tt_user_id NUMBER;
    BEGIN
    SELECT seq_pk_tt_user_id.NEXTVAL INTO new_tt_user_id FROM dual;
    INSERT INTO tt
    (username, user_id, created)
    VALUES
    (new_tt.username, new_tt_user_id, new_tt.created);
    END prc_insert_tt;
    CREATE OR REPLACE PROCEDURE prc_delete_tt(old_tt v_tt%ROWTYPE, new_tt v_tt%ROWTYPE) IS
    BEGIN
    DELETE FROM tt
    WHERE user_id = nvl(old_tt.user_id,
    -99);
    END prc_delete_tt;
    CREATE OR REPLACE TRIGGER trg_iof_v_tt
    INSTEAD OF UPDATE OR INSERT OR DELETE ON v_tt
    FOR EACH ROW
    DECLARE
    new_tt v_tt%ROWTYPE;
    old_tt v_tt%ROWTYPE;
    BEGIN
    dbms_output.put_line('INSTEAD OF TRIGGER fired');
    dbms_output.put_line(':NEW.user_id ' || :NEW.user_id);
    dbms_output.put_line(':OLD.user_id ' || :OLD.user_id);
    dbms_output.put_line(':NEW.username ' || :NEW.username);
    dbms_output.put_line(':OLD.username ' || :OLD.username);
    dbms_output.put_line(':NEW.created ' || :NEW.created);
    dbms_output.put_line(':OLD.created ' || :OLD.created);
    new_tt.user_id := :NEW.user_id;
    new_tt.username := :NEW.username;
    new_tt.created := :NEW.created;
    old_tt.user_id := :OLD.user_id;
    old_tt.username := :OLD.username;
    old_tt.created := :OLD.created;
    IF inserting THEN
    prc_insert_tt(old_tt,
    new_tt);
    ELSIF updating THEN
    prc_update_tt(old_tt,
    new_tt);
    ELSIF deleting THEN
    prc_delete_tt(old_tt,
    new_tt);
    END IF;
    END trg_iof_v_tt;
    set serveroutput on
    set null ^
    insert into v_tt values ('XXX', -1, sysdate) ;
    INSTEAD OF TRIGGER fired
    :NEW.user_id -1
    :OLD.user_id
    :NEW.username XXX
    :OLD.username
    :NEW.created 30/01/2007
    :OLD.created
    1 row created.
    commit ;
    select * from v_tt where username = 'XXX' ;
    USERNAME USER_ID CREATED
    XXX 1000 31/01/2007          <- seems to be no problem with insert part but
    update v_tt set username = 'YYY' where user_id = 1000 ;
    INSTEAD OF TRIGGER fired
    :NEW.user_id
    :OLD.user_id
    :NEW.username YYY
    :OLD.username
    :NEW.created
    :OLD.created
    1 row updated.
    commit ;
    select count(*) from v_tt where username = 'YYY' ;
    COUNT(*)
    0               <- here is my first problem with update part, Oracle said "1 row updated."
    delete from v_tt where user_id = 1000 ;
    INSTEAD OF TRIGGER fired
    :NEW.user_id
    :OLD.user_id
    :NEW.username
    :OLD.username
    :NEW.created
    :OLD.created
    1 row deleted.
    commit ;
    select count(*) from v_tt ;
    COUNT(*)
    14               <- here is my second problem with delete part, Oracle said "1 row deleted."
    Any comments will be welcomed, thank you.
    Message was edited by:
    TongucY
    changed "-1" values to "1000" in the where clause of delete and update statements.
    it was a copy/paste mistake, sorry for that.

    What table do you process in your procedures ? You don't use DBLINK to
    reference remote table in your procedures.
    Seems, you have table "TT" in "HR" schema too.
    Look:
    SQL> create table tt nologging as select * from all_users where rownum <=3;
    Table created.
    SQL> select * from tt;
    USERNAME                          USER_ID CREATED
    SYS                                     0 25-APR-06
    SYSTEM                                  5 25-APR-06
    OUTLN                                  11 25-APR-06
    SQL> conn scott/tiger
    Connected.
    SQL> create database link lk65 connect to ... identified by ... using 'nc65';
    Database link created.
    SQL> select * from tt@lk65;
    USERNAME                          USER_ID CREATED
    SYS                                     0 25-APR-06
    SYSTEM                                  5 25-APR-06
    OUTLN                                  11 25-APR-06
    SQL> create view v_tt as select username, user_id, created from tt@lk65 order by 2;
    View created.
    SQL> select * from v_tt;
    USERNAME                          USER_ID CREATED
    SYS                                     0 25-APR-06
    SYSTEM                                  5 25-APR-06
    OUTLN                                  11 25-APR-06
    SQL> create sequence seq_pk_tt_user_id
      2  minvalue 1000 maxvalue 99999
      3  increment by 1;
    Sequence created.
    SQL> CREATE OR REPLACE PROCEDURE prc_insert_tt(old_tt v_tt%ROWTYPE, new_tt v_tt%ROWTYPE) IS
      2  new_tt_user_id NUMBER;
      3  BEGIN
      4  SELECT seq_pk_tt_user_id.NEXTVAL INTO new_tt_user_id FROM dual;
      5  INSERT INTO tt
      6  (username, user_id, created)
      7  VALUES
      8  (new_tt.username, new_tt_user_id, new_tt.created);
      9  END prc_insert_tt;
    10  /
    Warning: Procedure created with compilation errors.
    SQL> show error
    Errors for PROCEDURE PRC_INSERT_TT:
    LINE/COL ERROR
    5/1      PL/SQL: SQL Statement ignored
    5/13     PL/SQL: ORA-00942: table or view does not exist
    SQL> edit
    Wrote file afiedt.buf
      1  CREATE OR REPLACE PROCEDURE prc_insert_tt(old_tt v_tt%ROWTYPE, new_tt v_tt%ROWTYPE) IS
      2  new_tt_user_id NUMBER;
      3  BEGIN
      4  SELECT seq_pk_tt_user_id.NEXTVAL INTO new_tt_user_id FROM dual;
      5  INSERT INTO tt@lk65
      6  (username, user_id, created)
      7  VALUES
      8  (new_tt.username, new_tt_user_id, new_tt.created);
      9* END prc_insert_tt;
    SQL> /
    Procedure created.Rgds.

  • Insert,  Delete and Update options in Table control

    Experts,
    I have writen code for Insert,  Delete and Update options in Table control. They are not working properly...
    can any one send the code for the above please...
    Thanks in advance..

    Hi,
    Following steps will help you.
    1.TOP-INCLUDE
    DATA: ITAB1 LIKE KNA1 OCCURS 0 WITH HEADER LINE.
    DATA: ITAB2 LIKE KNA1 OCCURS 0 WITH HEADER LINE.
    DATA: WA LIKE KNA1.
    DATA: ANT TYPE I,CUR TYPE I.
    DATA: OK_CODE TYPE SY-UCOMM.
    CONTROLS: TABCTRL TYPE TABLEVIEW USING SCREEN 100.
    IN FLOWLOGIC
    PROCESS BEFORE OUTPUT.
    LOOP AT ITAB1 CURSOR CUR WITH CONTROL TABCTRL.
    ENDLOOP.
    PROCESS AFTER INPUT.
    MODULE CLEAR_DATA.
    LOOP AT ITAB1.
    MODULE MOVE_DATA.
    ENDLOOP.
    ADD “OK_CODE” IN ELEMENT LIST. CLICK ON LAYOUT AND  ADD TABLE CONTROL(name it as TABCTRL) AND PUSHBUTTONS AS FOLLOWS.
    SELECT THE FIELDS FROM PROGRAM. SAVE CHECK AND ACTIVATE.
    CLICK ON FLOWLOGIC EDITOR FROM APPLICATION TOOL BAR.
    DOUBLE CLICK ON MODULE “CLEAR_DATA”.
    write the in this module as below.
    CLEAR ITAB2. REFRESH ITAB2.
    DOUBLE CLICK ON MODULE “MOVE_DATA”.
    write the code in this module as below.
    APPEND ITAB1 TO ITAB2.
    ACTIVATE PAI AND WRITE THE CODE AS BELOW.
    CASE OK_CODE.
    WHEN 'FETCH'.
    SELECT * FROM KNA1 INTO TABLE ITAB1 UP TO 20 ROWS.
    TABCTRL-LINES = SY-DBCNT.
    WHEN 'ADD'.
    GET CURSOR LINE CNT.
    CNT = TABCTRL-TOP_LINE + CNT - 1.
    CLEAR WA.
    INSERT WA INTO ITAB1 INDEX CNT.
    WHEN 'MODIFY'.
    GET CURSOR LINE CNT.
    READ TABLE ITAB2 INDEX CNT.
    LOOP AT ITAB2.
    MODIFY KNA1 FROM ITAB2.
    ENDLOOP.
    SELECT * FROM KNA1 INTO TABLE ITAB1.
    WHEN 'EXIT'.
    LEAVE PROGRAM.
    ENDCASE.
    SAVE,CHECK AND ACTIVATE ALL.
    CREATE TCODE AND EXECUTE.
    contact if u hv any issues regarding this code.
    reward points,if it is useful.
    Thanks,
    Chandu.

  • Will insert (ignore duplicates) have a better performance than merge?

    Will insert (ignore duplicates) have a better performance than merge (insert if not duplicate)?

    Ok. Here is exactly what is happenning -
    We had a table with no unique index on it. We used 'insert all' statement to insert record.
    But later when we found duplicates in there we started removing them manually.
    Now, to resolve the issue we added unique index and added exception handling to ignore DUP_VAL_ON_INDEX exception.
    But with this all records being inserted by 'INSERT ALL' statement gets ignored even if only one record is duplicate.
    Hence we have finally replaced 'insert all' with merge statement. Which inserts only if a corresponding record is not found (match based on column in unique index) in the table.
    But I am wondering how much performance will get impacted.

  • Reimaging Computers and SCCM sending duplicate updates

    We image computers with Ghost and use the same computer name they originally had on the network once they are imaged.
    The intent of the latest image is to be fully updated to this point before it is pushed (Via Ghost) to the computers.
    We fully updated the image and pushed it out to a few test PC's and we are using computer names that are already on the Domain and in SCCM.
    The problem is SCCM thinks that our imaged computers need updates.
    I believe these updates are ones that the computers didnt get before they were imaged, as like I said the image should have all the updates.
    We want to assure that the images are updated and SCCM isnt pushing duplicates to the boxes.
    My guess is that SCCM believes it is talking to the old system vs the new one with the updates.
    Does anyone have any insight on how to fix this.
    Thank you in advance.
    MILADMIN

    The problem is SCCM thinks that our imaged computers need updates.
    This is really a question for the Configuration Manager Software Updates forum..
    but why do YOU think that a NEWLY IMAGED system would not need updates?
    I believe these updates are ones that the computers didnt get before they were imaged
    I'm intensely inclined to concur. Unless you made that image since Tuesday, August 13th, it's *impossible* that the image would be fully patched! Ergo any system built from that image is, by definition, NOT fully patched and WILL need more patches.
    We want to assure that the images are updated and SCCM isnt pushing duplicates to the boxes.
    Whether the images are updated or not has absolutely nothing to do with Software Updates.
    As for duplicate patches.. for almost 20 years Windows Update and it's associated enterprise products, all based on the Windows Update Agent, have successfully patched systems without sending duplicate updates to a client -- not ONCE! Why would you be concerned
    that an architecture that's existed for over a dozen years might suddenly screw your environment up when it hasn't done so in the previous dozen years? :-)
    My guess is that SCCM believes it is talking to the old system vs the new one with the updates.
    I think you're overthinking the situation. Your guess is totally inaccurate. A *NEW* system has a *NEW* identifier (GUID), and it's impossible to confuse a newly imaged system with a previously existing system.
    The *OLD* system, at worse, is orphaned in Configuration Manager and needs to be deleted.
    Does anyone have any insight on how to fix this.
    So your assumption is that it's broken???
    Lawrence Garvin, M.S., MCSA, MCITP:EA, MCDBA
    SolarWinds Head Geek
    Microsoft MVP - Software Packaging, Deployment & Servicing (2005-2014)
    My MVP Profile: http://mvp.microsoft.com/en-us/mvp/Lawrence%20R%20Garvin-32101
    http://www.solarwinds.com/gotmicrosoft
    The views expressed on this post are mine and do not necessarily reflect the views of SolarWinds.

  • Insert only the updated Fields

    I have a log table based on the master table. The master table have 50 fields. Any update in the master data has to be logged in a new table. So the master table will have only the last updated data.
    Any change in the master table to be inserted in to a logged table. I need to insert only the updated fileds not all the fields.
    How to write insert statement in forms 6i for inserting to a logged table where only the changed fields.
    INSERT TO EMASTER_LOGTABLE
    (ECODE,
    ENAME,
    EDEPT,
    ETRADE
    VALUES
    (:ECODE,
    :ENAME,
    :EDEPT,
    :ETRADE
    Row will be inserted in to the EMASTER_LOGTABLE with the updated field only not all the field except primary.

    Hi!
    Whats about a new idea?
    Create a new table:
    create table EMASTER_HISTORY (
    EMP_CODE        number(6),  --> i don't know yours
    CHANGED_COLUMN  varchar2(30) not null,
    CHANGED_USER    varchar2(30) default user not null,
    CHANGE_TIME     date default sysdate not null,
    OLD_VALUE       varchar2(4000),
    NEW_VALUE       varchar2(4000) )
    storage ( your storage );In your form create a pre-update-trigger on your block like:
    declare
    l_item varchar2(30) := get_block_property ( 'your_block', first_item );
    l_data_old varchar2(4000);
    l_data_new varchar2(4000);
    begin
    loop
      if
       get_item_property ( 'your_block.' || l_item, item_type ) in ( 'BUTTON', 'IMAGE' )
      then
        null;
      elsif
        get_item_property ( 'your_block.' || l_item, database_value ) != name_in ( 'your_block.' || l_item ) OR
         ( get_item_property ( 'your_block.' || l_item, database_value ) is null AND name_in ( 'your_block.' || l_item ) is not null ) OR
         ( get_item_property ( 'your_block.' || l_item, database_value ) is not null AND name_in ( 'your_block.' || l_item ) is null )
      then
        l_data_old := get_item_property ( 'your_block.' || l_item, database_value );
        l_data_new := name_in ( 'your_block.' || l_item );
        insert into test.emaster_history ( emp_code, changed_column, old_value, new_value )
        values ( :your_block.emp_code, l_item, l_data_old, l_data_new );
      end if;
      exit when l_item = get_block_property ( 'your_block', last_item );
      l_item := get_item_property ( 'your_block.' || l_item, nextitem );
    end loop;
    exception
    when others then message ( l_item || ': ' || nvl ( error_text, dbms_error_text ) );
    end;So, for every updated item in your form you will have a record in EMASTER_HISTORY with timestamp
    and you're able to read the history of data in every column in your emp_master table.
    Addition:
    The only disadvantage is, non database items may will be logged too.
    This because i don't know a "clean" item property to find out, if a item is a database item.
    The item property column_name is not required and could by null alltough the item is a database item.
    Regards

  • Insert Mode or Update Mode

    Hello,
    In my form I have a component list. He calls another form. Calling this form is in insert mode or update mode. How can I do this?
    In the insert mode is showing the error:
    ORA-01403: no data found Error Unable to fetch row.
    How can I do that if the record does not exist, it change to insert mode?
    Thank you all.
    Marcelo

    If you built the form using the create form wizard, you should not need to tell it whether you are expecting to insert or update - we do that for you.
    If you built your own, you will need to add a condition to your fetch so that it only fires if you primary key column was passed in (that is what typically signifies an update). You will need the same on the processing to identify whether to run your insert or update procedure (ditto for your buttons, you don't want to display Delete when a user is inserting).
    Even if you did build your own, you might want to create a form using a wizard and inspect what gets created for you so that you can see how it works and then replicate on your page.
    -- Sharon

  • Do we have a shortcut to insert a duplicate record except PK ?

    Do we have a shortcut to insert a duplicate record except PK ?
    Thanks.

    Do you want to insert another row with all of the columns the same but with a different PK? If so then:
    INSERT INTO t (pk, col1, col2, col3)
    SELECT new_value_for_pk, col1, col2, col3
    FROM t
    WHERE pk = <value>What you use for new_value_for_pk will depend on how the PK is generated.
    John

  • TS3681 I went into recovery mode - and keep receiving error 4005 - tried different usb, computers, did all updates possible nothing helps, my iphone screen shows apple logo and constant waiting circle

    I went into recovery mode - and keep receiving error 4005 - tried different usb, computers, did all updates possible nothing helps, my iphone screen shows apple logo and constant waiting circle

    Same here. I tried to update my iPhone 5S and iPad 4 to 7.1 but iTunes wiped out them. This thing already happened for iOS 7.0.6 update. What happened to Apple? I am really curious whether Apple is aware of this problem.

  • Duplicate records getting inserted instead of update.

    I have any entity which has multiple childern. All have one to many relation with the parent. When I update the no of childerns and merge using EntityManager. It is updating the parent and insert all the childern records, which is creating duplicates. What could be the possible error.

    Where did you read the parent object and children from originally (was it serialized?), and how are you merging it? How have you defined your id in your child objects? What version of TopLink are you using?
    Perhaps include some sample code. It most likely has something to do with how you are merging the objects, if you just read the parent and access the children, are they inserted?
    -- James : http://www.eclipselink.org

  • SQL MERGE trying to duplicate update/insert. The insert/update is not ideal, but the sql merge hangs.

    I am trying to duplicate this update/insert with a sql merge:
    -UPDATE [etag].Energy_Processed
    SET Start_Datetime = estg.Start_Datetime,
    --End_Datetime = estg.End_Datetime,
    --Schedule_MW = estg.Schedule_MW,
    --Active_MW = estg.Active_MW,
    Load_Dt = GETUTCDATE() 
    FROM  [etag].Energy_Stg estg, [etag].[Energy_Processed] ep
    WHERE estg.Tag_Name IN
    SELECT DISTINCT(tproc.Tag_Name) from [etag].[Energy_Processed] eproc
    INNER JOIN [etag].[Tag_Processed] tproc ON tproc.Id = eproc.Tag_Id
    INNER JOIN [etag].[Energy_Stg] estg1 ON estg1.Tag_Name = tproc.Tag_Name
               AND ep.Active_MW = estg.Active_MW
               AND ep.End_Datetime = estg.End_Datetime
              AND ep.Schedule_MW = estg.Schedule_MW 
    INSERT into [etag].[Energy_Processed] (Tag_Id, Start_Datetime, End_Datetime, Schedule_MW, Active_MW, Load_Dt)
    SELECT tproc.Id, estg.Start_Datetime, estg.End_Datetime, Schedule_MW, Active_MW, GETUTCDATE()  FROM [etag].[Energy_Stg] estg
    INNER JOIN [etag].[Tag_Processed] tproc ON tproc.Tag_Name = estg.Tag_Name
    WHERE estg.Id BETWEEN @minEId AND @maxEId AND
    estg.Tag_Name NOT IN (
    SELECT DISTINCT (tproc.Tag_Name) from [etag].[Energy_Processed] eproc
    INNER JOIN [etag].[Tag_Processed] tproc ON tproc.Id = eproc.Tag_Id
    INNER JOIN [etag].[Energy_Stg] estg1 ON estg1.Tag_Name = tproc.Tag_Name
    SQL MERGE:
    MERGE [Forecast_Data_Repository].[etag].[Energy_Archive] enArch
    USING 
    (SELECT ep.Tag_Id, ep.Start_Datetime, ep.End_Datetime, ep.Schedule_MW, ep.Active_MW, ep.Load_Dt 
    FROM  [etag].[Energy_Stg] estg, [etag].[Energy_Processed] ep
    WHERE estg.Tag_Name IN
    SELECT DISTINCT(tproc.Tag_Name) from [etag].[Energy_Processed] eproc
    INNER JOIN [etag].[Tag_Processed] tproc ON tproc.Id = eproc.Tag_Id
    INNER JOIN [etag].[Energy_Stg] estg1 ON estg1.Tag_Name = tproc.Tag_Name
    ) AS sourceEP
    ON EXISTS(SELECT sourceEP.Tag_Id)
    WHEN MATCHED THEN
    UPDATE
    SET Tag_Id = sourceEP.Tag_Id,
    Start_DateTime = sourceEP.Start_Datetime,
    End_Datetime = sourceEP.End_Datetime,
    Schedule_MW = sourceEP.Schedule_MW,
    Load_dt = GETUTCDATE()
    WHEN NOT MATCHED BY TARGET THEN
    INSERT (Tag_Id, Start_Datetime, End_Datetime, Schedule_MW, Active_MW, Load_dt)
    VALUES (sourceEP.Tag_Id, sourceEP.Start_Datetime, sourceEP.End_Datetime, sourceEP.Schedule_MW,
    sourceEP.Active_MW, GETUTCDATE())
    OUTPUT
    $action,
    INSERTED.Tag_Id
    --UPDATED.Tag_Name
    INTO @MergeOutput;
    Greg Hanson

    This bit
        ON EXISTS(SELECT sourceEP.Tag_Id)
    Looks wrong. Sould be something like:
       ON sourceEP.Tag_Id = enArch.Tag_Id
    And don't update the matching columns in the UPDATE part, as they already match.
    David
    David http://blogs.msdn.com/b/dbrowne/

  • ALV, event DATA_CHANGED, avoid insert of duplicate key

    Hello,
    I would like to implement edit functionality of an internal table with ALV based on class CL_GUI_ALV_GRID. Some fields of internal table are key fields and the combination of them should be unique. The user has a possibility to add a new record (all fields in new record are editable) or change values in an existing record (only non-key fields are editable). So far so good, it works.
    I'm doing a validation of entered data in the event DATA_CHANGED. In this event I'm checking if the new record is a duplicate combination of existing key fields. If yes, then all fields of the new record are reported as error with ADD_PROTOCOL_ENTRY method. This removes all entries related to this row from MT_GOOD_CELLS table and this is ok for me.
    My problem: after validation ALV inserts such empty row in my internal table and next time, when the user makes a correction, this record is an update and not a new record (there is no entry in MT_INSERTED_ROWS).
    Question: Is there a possibility to avoid ALV inserting a new record when all its cells are bad?
    Regards,
    Annie

    Hi,
    As far as i have understood your problem,you can try the following to avoid the insertion of an empty row:
    When you must be creating an empty row,just set a flag say flag_create = 'X'.
    Then when you are performing the validation then if there is an error then check for this flag that is :
    if flag_create = 'X'.
    then delete from the internal table based on sy-tabix.
    endif.
    This way the new row that got appended with wrong values will get deleted and the code will not consider this as an updated row.
    Kindly <<deleted> if useful.:)
    Follow the rules of engagement
    Edited by: Vijay Babu Dudla on Jan 9, 2012 7:07 AM

Maybe you are looking for

  • Calling a method on ear deployment.

    Hi, Our application needs to do some pre-processing when the application ear file is first deployed. Is it possible to execute custom code when a particular ear file is deployed to application server? I looked around and found few ways to do it, none

  • I cant sync my ipad and my computer what is the problem

    I have an ipad and am trying to sync my firefox to my ipad. It doesnt give me specific instructions on how to do this. Can you help me?

  • Why can't my ATV connect to my library when it used to work??

    I have tried everything.  It previously worked, but now after trying to connect to my library it says it's unable to connect.  It keeps telling me to make sure Home Sharing is turned on.  I have tried resetting ATV.  I have tried restoring ATV.  I ha

  • DNS request behind content switch: troubleshooting

    Hi, sometimes we experience problems resolving a dns name behind a content switch. The problem solves himself after 5 minutes or something like that. We see the dns request on the dns servers subnet. It looks like the is a problem when the packet ret

  • Distribute TestStand Application

    Hi All, I have just started to work on TestStand. My work includes various Test Sequences to be controlled and monitored from an User Interface. I had worked on CVI before. Can we make a User Interface similar to CVI in TestStand so that say, if i cl