Update of several rows of the same table using trigger

Hello

I have the following table

---------------------------------------------------------------------------------------------------------

CREATE TABLE ATT_PERMISSIONS
(
DATE OF PRMONTHYR,
EMPIDNUMBER (10)DEFAULT VALUE 0,
FROMDATETIMEDATE,
TODMTFDATETIMEDATE,
NUMBER OF PERMMINS (3),

SSLINASTRETCH NUMBER (3).

PERMATTINELIGIBLENUMBER (1)DEFAULT VALUE 0
);

------------------------------------------------------------------------------------------------------

I want to update the column PERMATTINELIGIBLE in the same table when each line is inserted.

I created following trigger for that

CREATE OR REPLACE TRIGGER SSL_AFTER_UPDATE AFTER UPDATE ON ATT_PERMISSIONS

FOR EACH LINE

BEGIN

Update ATT_PERMISSIONS set PERMATTINELIGIBLE =

(

SELECT THE CHECK BOX

WHEN PERMMINS < = 60

or

(

PERMMINS < = SSLINASTRETCH

AND

COUNT (CASE WHEN PERMMINS = 0 THEN NULL OTHERWISE 1 END)

COURSES (PARTITION BY empid, prmonthyr) < = 1

)

THEN 1

0 OTHERWISE

END

FROM ATT_PERMISSIONS where prmonthyr =:new.prmonthyr, empid =:new.empid

) where prmonthyr =:new.prmonthyr, empid =:new.empid;

END;

---------------------------------------------------------------------------------------------------

That is to say.

When a row is inserted the PERMATTINELIGIBLE of all records for this employee for the month is recalculated.

There would be only two or three records for an employee for a month.

PERMMINS is the number of minutes to use as short leave.

If it is less than 60 is eligible for the bonus to attendance for this employee.

If this isn't normally the date is not eligible for the attendance for this employee premium.

but an exception is that if a short leaves of DISP employee allowed a month (IE. ( SSLINASTRETCH ) in a straight line, IE. one day, that day is eligible for the bonus of presence.

That is to say. Why the number of short leaves in a month is taken. If it's one and done minutes use is lower to SSLINASTRETCH, then the day is eligible for the attendance bonus.

But when I try to create the trigger I get error, PL/SQL: ORA-00907: missing a right parenthesis.

Help, please

I use oracle 10g

I want to update the column PERMATTINELIGIBLE in the same table, when each row is inserted.

No - you do NOT want to do in a trigger.

You cannot query the same table that the trigger is activated on. -C' is a MUTANT and you will get an exception.

When a row is inserted is recalculated to the PERMATTINELIGIBLE of all the records for that employee for that month.

There would be only two or three records for an employee for a month.

PERMMINS is the number of minutes to use as short leave.

If it is less than 60 is eligible for the bonus to attendance for this employee.

If is not normally the day is not eligible for the premium of attendance for this employee.

but an exception is that if a short leaves of DISP employee allowed a month (IE. SSLINASTRETCH) in a straight line, IE. one day, that day is eligible for the bonus of presence.

That is to say. Why the number of short leaves in a month is taken. If it's one and done minutes use is less than SSLINASTRETCH, then the day is eligible for the bonus of presence.

But when I try to create the trigger I get error, PL/SQL: ORA-00907: missing a right parenthesis.

You can not do in a trigger. Even if correct you this error of syntax, you will get the exception that I mentioned above.

Oracle is a multi-user system. Other users can modify the same table that you use.

So even if you could try to interview "all the records for this employee" some of them may be locked if other users access them. Also, an insert statement could try to insert two rows for an employee and the trigger could not have seen the other lines still.

If you want to work with data SETS (all records for an employee), you must write the PL/SQL code that LOCKS all lines updated to prevent others to interfere.

Then you make your operations but NOT in a trigger. Perform inserts and then use an UPDATE query to set this value.

Then VALIDATE to unlock the lines.

Tags: Database

Similar Questions

  • Compare two rows in the same table

    Hi all

    I need to compare two rows in the same table, I don't know hoe to do it in pl/sql. Please help me on this.

    example:

    price of ro TR
    xya0001 AMA.7 12
    xya0003 ama6 14
    xya0004 AMA.7 16

    in table b is a unique value for each line, I need to compare the price column and see if the first value is less than or greater than the following value and, if there is more to put the corresponding value of br to a variable, and if it is less, put the corresponding value of the br to another variable. I don't know a method to do this, as I'm new to pl/sql. Please help me in this
    for data in(select tr,br,price, lag(price) over ( order by tr) newcol
    from yourtable)
    loop
    if nvl(data.newcol,0) > data.price then
    variable1:=data.br;
    else
    varable2:=data.br;
    end if;
    end loop;
    
  • Update lines with the info from the other rows in the same Table.

    I'm trying to update the lines with the information of the same table. The table is loaded with information from a report that runs and there must be a new entry every month, but I would like to bring some of the info from the last month. This statement below works but updates all rows in the new load table and in my test case, I only did a few game only like 5 files need to get updates. It is an example of what I'm trying to do. If I add this (C2. COL_INVC_ID = C1. COL_INVC_ID) until the last "* where *" statement get an invalid identifier for 'C2 '. COL_INVC_ID ". So what I'm doing wrong here? How can I update only the lines where also in recent months run?

    Thanks in advance for any help!

    ------------
    Update OpenIssues OI1
    Together (OI1. NUM, OI1. Status, OI1. Code, OI1. LastModifiedDate) =
    (Select ios2. NUM, ios2. Status, ios2. Code, ios2. LastModifiedDate
    Of OpenIssues ios2
    Where OI2.num = OI1.num and ios2. TableLoadDate = TO_DATE (January 31, 2012 00:00:00 ',' ' the HH24: MI: SS DD/MM/YYYY)
    )
    Where and OI1. TableLoadDate = TO_DATE (February 29, 2012 00:00:00 ',' ' the HH24: MI: SS DD/MM/YYYY)
    ------------


    SQLMe

    As Frank suggested merger is much effective here, go...

      create table  temp
    as(
    select 1 eno, 1 amt , sysdate load_date from dual union all
    select 1 eno, 2 amt , add_months(sysdate,1) load_date from dual union all
    select 2 eno, 1 amt , sysdate load_date from dual union all
    select 2 eno, 2 amt , add_months(sysdate,1) load_date from dual
    );
    
    merge into temp t
    using (select eno, amt, load_date
             from temp
            where trunc(Load_Date) = TO_DATE('05/30/2012', 'MM/DD/YYYY')) s
    on ((s.eno = t.eno) and trunc(t.Load_Date) = TO_DATE('06/30/2012', 'MM/DD/YYYY'))
    when matched then
      update set t.amt = s.amt;
    commit;
    
    select * from temp;
    
  • Possible to insert more than once in the same table using multi table insert?

    I used multi-table insert before insert a source separate from the tables without problem.

    However, when I want to insert into the same table two or more distinct lines, I encountered a problem because when I call nextval on the sequence since this is the same sequence # will try to use the same number for two inserts, which will raise a violation of PK constraint on the 2nd insert.

    A way around this problem?

    Another option is to create the sequence with increases of 2 or however many tables, you are targeting.
    When you then specify the. NEXTVAL, you must subtract the relative number of it, apart from the first table that you insert in to.

    Something like that;

    drop table t1;
    
    create table t1 (a number, b number);
    
    drop table t2;
    
    create table t2 (a number, b number);
    
    drop sequence test_seq
    /
    
    create sequence test_seq increment by 2 minvalue 0
    /
    
    insert all
    when 1 = 1 then
      into t1 (a, b) values (test_seq.nextval, 1)
    when 1 = 1 then
      into t2 (a, b) values (test_seq.nextval  - 1, 1)
    select 1, 1 from dual
    /
    
  • Need a script to select the update the row to another row in the same table

    I have a table with the columns and values
    KeyID       EffectiveDate     InactiveDate
    1              01/01/2013
    1              01/02/2013
    1              01/03/2013
    2              12/31/2012
    2              01/02/2013
    2              01/03/2013
    I need to update the InactiveDate of a line with the EffectiveDate of a line with the EffectiveDate newer than the update line and the even KeyID.
    KeyID       EffectiveDate     InactiveDate
    1              01/01/2013      01/02/2013
    1              01/02/2013      01/03/2013
    1              01/03/2013
    2              12/31/2012      01/02/2013
    2              01/02/2013      01/03/2013
    2              01/03/2013
    I want to have a select script that shows
    KeyID       EffectiveDate        InactiveDate    New_InactiveDate
    1              01/01/2013                              01/02/2013
    1              01/02/2013                              01/03/2013
    1              01/03/2013
    2              12/31/2012                              01/02/2013
    2              01/02/2013                              01/03/2013
    2              01/03/2013
    Thanks for any help.

    Published by: 881656 on January 3, 2013 17:57

    Published by: 881656 on January 3, 2013 18:15

    Published by: 881656 on January 3, 2013 18:20

    Hello

    881656 wrote:
    I have a table with the columns and values

    KeyID       EffectiveDate     InactiveDate
    1              01/01/2013
    1              01/02/2013
    1              01/03/2013
    2              12/31/2012
    2              01/02/2013
    2              01/03/2013
    
    I need to update the InactiveDate of a row with the EffectiveDate of a row with the newer EffectiveDate than the row to be updated and with the same KeyID.
    
    KeyID       EffectiveDate     InactiveDate
    1              01/01/2013      01/02/2013
    1              01/02/2013      01/03/2013
    1              01/03/2013
    2              12/31/2012      01/02/2013
    2              01/02/2013      01/03/2013
    2              01/03/2013
    

    This sounds like a job for the analytical function of LEAD:

    MERGE INTO table_x     dst
    USING (
         SELECT  keyid
         ,     effectivedate
         ,     LEAD (effectivedate) OVER ( PARTITION BY  keyid
                                           ORDER BY         activedate
                                         )            AS inactivedate
          )               src
    WHEN MATCHED THEN UPDATE
    ON    (    src.id          = dst.id
          AND  src.effectivedate     = dst.effectivedate
          )
    SET     dst.inactivedate     = src.inactivedate
    ;
    
    I want to have a select script which shows
    
    KeyID       EffectiveDate        InactiveDate    New_InactiveDate
    1              01/01/2013                              01/02/2013
    1              01/02/2013                              01/03/2013
    1              01/03/2013
    2              12/31/2012                              01/02/2013
    2              01/02/2013                              01/03/2013
    2              01/03/2013
    

    Once that you have done the above MERGER, it is simply:

    SELECT    *
    FROM      table_x
    ORDER BY  keyid
    ,         effectivedate
    ;
    

    If you do not have sthe FUSION, then see the query in the USING clause of the MERGER.

    I hope that answers your question.
    If not, post a small example data (CREATE TABLE and only relevant columns, INSERT statements), and the results you want from this data.
    In the case of a DML (UPDATE), for example, the sample data should show what looks like the tables before the DML, and the results will be the content of the table changed after the DML.
    Explain, using specific examples, how you get these results from these data.
    Always say what version of Oracle you are using (for example, 11.2.0.2.0).
    See the FAQ forum {message identifier: = 9360002}

  • Copy the data rows in the same table

    Here is the structure of the table, I work I would like all the value with ID = 1 in new lines. The ID must be 2 with the same data.

    I am using following query to accomplish, but get the error.

    Insert into hsp_column_detail_item (DETAIL_ID, VALUE, POSITION, GENERATION, OPERATOR, LABEL)

    Select 2, POSITION, GENERATION, OPERATOR, VALUE, LABEL

    Of

    hsp_column_detail_item

    where

    detail_id = 1

    ORA-02291: integrity constraint (PLANSAMP. FK_HSP_DETAIL_ID) violated - key parent not found

    Columns

    Detail_ID1THEREINTEGER NUMBERFrequency
    VALUE2THEREFLOATNone
    POSITION3THEREINTEGER NUMBERNone
    GENERATION4THEREINTEGER NUMBERNone
    OPERATOR5THEREINTEGER NUMBERNone
    LABEL6THEREVARCHAR2 (1500 bytes)None

    Sample data:

    Label Detail_ID value generation operator Position

    1500001ABC

    1300101def

    1200201EFG

    Index

    Name of the indexColumn name

    Col_Detail_ItemDetail_ID

    Col_Detail_ItemPosition

    866532 wrote:

    Where detail_id in

    (select detail_id in the hsp_column_detail where)

    dim1 = (select object_id from hsp_object where the object_name = 'Real' and object_type = 31) and

    Dim5 = (select object_id from hsp_object where the object_name = 'Work' and object_type = 35) and

    dim7 = (select object_id from hsp_object where the object_name = 'FY13' and object_type = 38)

    );

    ORA-00001: unique constraint (PLANSAMP. Voilated PK_HSP_COL_DETAIL)

    To not violate the unique constraint, two requirements:

    -The detail_id 5 you insert would not already exist in the table

    -Since you are forcing this detail_id, your subquery must return only one row: so you can write ' detail where id = (select...) "and:

    -either the dim1 / dim5 / dim7 filters are quite selective

    - or to make any decision on which detail_id to reproduce: max (detail_id) or min (detail_id) or any other way yo select the right / arbitrary

  • Compare several records in the same table

    Hello, I m writing a bat file so I can export information that I question.

    I have an automatic integration which checks from time to time if there is no new client´s and generates a trigger.
    If an error levonorgestrol I the process is repeated until the client is located in the database.
    I can't create tables or change the structure.


    My problem is that I need to compare the record more updated with the CLIENT_ID field
    Let me give you an example:


    Table A

    ERROR_MESSAGE CREATEDUSERID CLIENT_ID
    0 success 01/01/2009
    1 error 01/01/2009
    2 success 01/01/2009
    1 success 01/02/2009
    3 success 01/02/2009
    4 error 01/02/2009

    I need to compare "1" client created on 01/01/2009 this error gived with new records to see if there's success and my
    query should only return the client_ID '4' and export with sqlplus

    Could you please help me?

    Hello

    It works very well with my data.
    What data do you use? After a few lines of sample data (CREATE TABLE AS executable... or INSERT... statements are best) that give you wrong results.

    Make sure that everything is spelled correctly.
    For example, roughly halfway through the query, you say

    and io.status = 'Erro' 
    

    and at the end you say

    having max(decode(io.status, 'Error', 1, 2))  ...
    

    That is the same ("Error" or "Error") in both places?

  • Windows Update offers several times that the same installed upgrading

    original title: update Windows using Vista

    Windows Update keeps saying I need to install the same update: a security update for Microsoft Visual C++ 2005 Service Pack 1 Redistributable Package (KB2538242) I installed it successfully, but he continues to return again and again to the top.  Any ideas?  Thank you

    Launch a collateral request for assistance free Windows Update:

    https://support.Microsoft.com/OAS/default.aspx?Gprid=6527&St=1&wfxredirect=1&SD=GN>

    Visit the Microsoft Solution Center and antivirus security for resources and tools to keep your PC safe and healthy. If you have problems with the installation of the update itself, visit the Microsoft Update Support for resources and tools to keep your PC updated with the latest updates.

    If you /clear/ State only one specific updated because of problems and don't ask questions, you /he/ receive assistance free of charge for the question.

    North American customers can also obtain instant access to unlimited free support by email or unlimited individual chat by visiting the following Microsoft Web site:

    >

    For enterprise customers, support for security updates is available through your usual support contacts.

    How to hide an update in Windows Vista

    1 open Windows Update by clicking on the "Start" icon and then clicking on '' programs. '' Choose "Windows Update" in the list of programs.

    2 click on "View available updates" to move to the screen that allows you to select the updates available for your computer.

    3. Select an update of the list that you want to hide. Right-click on it to bring up a menu, then click on "Hide update". If user account control is enabled, a confirmation dialog box appears on the screen informing you that "Windows needs your permission to continue."

    4. click on 'Continue' to confirm your action. If there are additional updates that you want to hide, steps 4 and 5 for each of them.

    5 search for updates and then click on "View available updates" to ensure that the updates have been correctly hidden

    UTC/GMT is 03:38 Sunday, October 16, 2011

  • With one of several rows with the same value for the same ID

    Hello

    I have this request:

    SELECT

    Count (case when EXISTS ())

    SELECT *.

    FROM business_log bl, su topic

    WHERE su.ID_SUBJECT = s.ID_SUBJECT

    and bl.id_subject = su.id_subject

    AND bl.value = 'resolved '.

    )

    then 1 end another null) num_solved

    OF THE subject

    It is possible that a subject is more than once 'resolved' in the table BUSINESS_LOG

    I want to count only one line solved for a topic.

    I need to use only under RESERVE the table in the main query due to other counts.

    Thank you very much.

    Concerning

    Milos

    Post edited by: 2796614

    in the examples before we talked abount 'Resolved' instead of 'resolved' as in your last example... to let you know, how it is stored.

    I had advice is not to use SEPARATE.

    So what... the evidence that any other question is faster than anything goes business_log, filter lines and can count distinct keys...

    separate within a group function is not the same thing as do select distinct...

    HTH

  • COMPARING TWO ROWS IN THE SAME TABLE AND DELETE THEM USING DELETION BLOCK

    Hi all
    I have a problem. I have an Employee table

    employee

    Emp_name Emp_id Emp_status Emp_date

    John 1 P 11-27-2010

    Mike 2 26/11/2010 S

    Simon P 3 22 / 11 / 2010

    Dennis C 4 25/11/2010...


    I compare the rows in the table and their employee_status if 'P', should I remove the... Here, I should delete lines 1 and 3 as their Emp_status is 'P '. I only use BULK DELETE... I tried to use the join query internal and deletion, but it does not work... Can someone help me...

    Ooops, it should be:

    and e2.emp_status in ('P', 'C')

    not

    and e2.emp_status in ('P', 'A')

    Here it goes:

    SQL> select  *
      2    from  employees
      3  /
    
    E EMP_NAME      EMP_KEY          X
    - ---------- ---------- ----------
    C John              123          1
    P Mike              123          2
    P Paul              123          6
    C Harry             124          5
    A Gass              125          7
    C Gass              125          7
    
    6 rows selected.
    
    delete  employees e1
      where 2 = (
                 select  count(distinct e2.emp_status)
                      from  employees e2
                      where e2.emp_key = e1.emp_key
                        and e2.emp_status in ('P','C')
                   )
    /
    
    3 rows deleted.
    
    SQL>
    

    SY.

  • Insert several rows in the data table table

    Hi all

    I have a requirement as to insert multiple rows in the table for this table data only (I need to replicate the data).
    In this table, the primary key is a composite primary key with all the keys.primary foreign key and the foreign key to the Date. I need to change that date to the insertion.


    INSERT
    IN myschema. Fact_page_performance
    (
    time_sk,
    Batch_ID,
    delta_msec,
    delta_user_msec,
    error_code_sk,
    content_errs,
    element_count,
    page_bytes,
    Available,.
    date_sk

    )
    VALUES
    (SELECT time_sk,
    Batch_ID,
    delta_msec,
    delta_user_msec,
    error_code_sk,
    content_errs,
    element_count,
    page_bytes,
    Available
    OF myschema. FACT_PAGE_PERFORMANCE_BACKUP
    WHERE date_sk = 20090509, 20090510
    );

    But it gives error like missing expression.
    Could someone please help me.

    Thanks and greetings
    Swetha.

    You can have either the VALUES or not SELECT the two

    INSERT
    INTO myschema.Fact_page_performance
    (
    time_sk ,
    batch_id ,
    delta_msec ,
    delta_user_msec,
    error_code_sk ,
    content_errs ,
    element_count ,
    page_bytes ,
    Available ,
    date_sk
    
    )
    SELECT time_sk ,
    batch_id ,
    delta_msec ,
    delta_user_msec,
    error_code_sk ,
    content_errs ,
    element_count ,
    page_bytes ,
    Available
    FROM myschema.FACT_PAGE_PERFORMANCE_BACKUP
    WHERE date_sk=20090509,20090510;
    
  • How to update columns with the value of other lines in the same table

    Hello

    I use Oracle 11.2, I'd use SQL statements to update a column based on values in other rows in the same table. Here are the details:

    create table TB_test (number 4 myId, crtTs date, date of MDPU);

    insert into tb_test (1, to_date ('20110101', 'YYYYMMDD'), null);
    insert into tb_test (1, to_date ('20110201', 'YYYYMMDD'), null);
    insert into tb_test (1, to_date ('20110301', 'YYYYMMDD'), null);
    insert into tb_test (2, to_date ('20110901', 'YYYYMMDD'), null);
    insert into tb_test (2, to_date ('20110902', 'YYYYMMDD'), null);

    After you run the SQL code, I would like to have the following result:

    1, 20110101, 20110201
    1, 20110201, 20110301
    1, 20110301, null
    2, 20110901, 20110902
    2, 20110902, null

    Thanks for your suggestion.

    I guess you need this, otherwise please explain logic correctly:

    SQL> merge into tb_test t
      2  using (
      3    select rowid as rid
      4         , lead(crtts) over(partition by myid order by crtts) as updts
      5    from tb_test
      6  ) v
      7  on (t.rowid = v.rid)
      8  when matched then update
      9   set t.updts = v.updts
     10  ;
    
    5 rows merged.
    
    SQL> select * from tb_test order by 1,2;
    
          MYID CRTTS     UPDTS
    ---------- --------- ---------
             1 01-JAN-11 01-FEB-11
             1 01-FEB-11 01-MAR-11
             1 01-MAR-11
             2 01-SEP-11 02-SEP-11
             2 02-SEP-11
    
  • Several updates on the same Table

    Hello

    I need to update the same Table (T1) but to filter for different conditions as below.
    and I have a set of filter conditions 50 and update the same table.

    Is it possible to get in the single update statement?

    BEGIN
    UPDATE T1
    SET PTA_CLM_CNT = v_PTA_CLM_CNT__77,
    PTB_CLM_CNT = v_PTB_CLM_CNT__77
    WHERE run_dt = v_in_parm_dt
    AND state_code = '77';
    EXCEPTION
    WHEN NO_DATA_FOUND THEN
    null;
    WHILE OTHERS THEN
    v_error_cd: = SQLCODE;
    v_error_msg: = SUBSTR (SQLERRM, 1, 200);
    v_curr_record: = 'sum_bene_state_updt_77 ' | v_in_parm_dt;
    create_error_log ('_sum_cnts', v_error_cd, v_error_msg, v_curr_record);
    END;

    BEGIN
    UPDATE T1
    SET PTA_CLM_CNT_ = v_PTA_CLM_CNT__78,
    PTB_CLM_CNT_ = v_PTB_CLM_CNT__78
    WHERE run_dt = v_in_parm_dt
    AND state_code = '78';
    EXCEPTION
    WHEN NO_DATA_FOUND THEN
    null;
    WHILE OTHERS THEN
    v_error_cd: = SQLCODE;
    v_error_msg: = SUBSTR (SQLERRM, 1, 200);
    v_curr_record: = 'sum_bene_state_updt_78 ' | v_in_parm_dt;
    create_error_log ('sum_cnts', v_error_cd, v_error_msg, v_curr_record);
    END;

    Thanks in advance

    May be trying to use DECODE

    UPDATE T1
       SET PTA_CLM_CNT = DECODE(state_code,77,v_PTA_CLM_CNT_77,78,v_PTA_CLM_CNT_78)
           PTB_CLM_CNT = DECODE(state_code,77,v_PTB_CLM_CNT_77,78,v_PTB_CLM_CNT_78)
     WHERE run_dt = v_in_parm_dt;
    
  • update to column values (false) in a copy of the same table with the correct values

    Database is 10gr 2 - had a situation last night where someone changed inadvertently values of column on a couple of hundred thousand records with an incorrect value first thing in the morning and never let me know later in the day. My undo retention was not large enough to create a copy of the table as it was 7 hours comes back with a "insert in table_2 select * from table_1 to timestamp...» "query, so I restored the backup previous nights to another machine and it picked up at 07:00 (just before the hour, he made the change), created a dblink since the production database and created a copy of the table of the restored database.

    My first thought was to simply update the table of production with the correct values of the correct copy, using something like this:


    Update mnt.workorders
    Set approvalstat = (select b.approvalstat
    mnt.workorders a, mnt.workorders_copy b
    where a.workordersoi = b.workordersoi)
    where exists (select *)
    mnt.workorders a, mnt.workorders_copy b
    where a.workordersoi = b.workordersoi)

    It wasn't the exact syntax, but you get the idea, I wanted to put the incorrect values in x columns in the tables of production with the correct values of the copy of the table of the restored backup. Anyway, it was (or seem to) works, but I look at the process through OEM it was estimated 100 + hours with full table scans, so I killed him. I found myself just inserting (copy) the lines added to the production since the table copy by doing a select statement of the production table where < col_with_datestamp > is > = 07:00, truncate the table of production, then re insert the rows from now to correct the copy.

    Do a post-mortem today, I replay the scenario on the copy that I restored, trying to figure out a cleaner, a quicker way to do it, if the need arise again. I went and randomly changed some values in a column number (called "comappstat") in a copy of the table of production, and then thought that I would try the following resets the values of the correct table:

    Update (select a.comappstat, b.comappstat
    mnt.workorders a, mnt.workorders_copy b
    where a.workordersoi = b.workordersoi - this is a PK column
    and a.comappstat! = b.comappstat)
    Set b.comappstat = a.comappstat

    Although I thought that the syntax is correct, I get an "ORA-00904: 'A'. '. ' COMAPPSTAT': invalid identifier ' to run this, I was trying to guess where the syntax was wrong here, then thought that perhaps having the subquery returns a single line would be cleaner and faster anyway, so I gave up on that and instead tried this:

    Update mnt.workorders_copy
    Set comappstat = (select distinct)
    a.comappstat
    mnt.workorders a, mnt.workorders_copy b
    where a.workordersoi = b.workordersoi
    and a.comappstat! = b.comappstat)
    where a.comappstat! = b.comappstat
    and a.workordersoi = b.workordersoi

    The subquery executed on its own returns a single value 9, which is the correct value of the column in the table of the prod, and I want to replace the incorrect a '12' (I've updated the copy to change the value of the column comappstat to 12 everywhere where it was 9) However when I run the query again I get this error :

    ERROR on line 8:
    ORA-00904: "B". "" WORKORDERSOI ": invalid identifier

    First of all, I don't see why the update statement does not work (it's probably obvious, but I'm not)

    Secondly, it is the best approach for updating a column (or columns) that are incorrect, with the columns in the same table which are correct, or is there a better way?

    I would sooner update the table rather than delete or truncate then re insert, as it was a trigger for insert/update I had to disable it on the notice re and truncate the table unusable a demand so I was re insert.

    Thank you

    Hello

    First of all, after post 79, you need to know how to format your code.

    Your last request reads as follows:

    UPDATE
      mnt.workorders_copy
    SET
      comappstat =
      (
        SELECT DISTINCT
          a.comappstat
        FROM
          mnt.workorders a
        , mnt.workorders_copy b
        WHERE
          a.workordersoi    = b.workordersoi
          AND a.comappstat != b.comappstat
      )
    WHERE
      a.comappstat      != b.comappstat
      AND a.workordersoi = b.workordersoi
    

    This will not work for several reasons:
    The sub query allows you to define a and b and outside the breakets you can't refer to a or b.
    There is no link between the mnt.workorders_copy and the the update and the request of void.

    If you do this you should have something like this:

    UPDATE
      mnt.workorders     A      -- THIS IS THE TABLE YOU WANT TO UPDATE
    SET
      A.comappstat =
      (
        SELECT
          B.comappstat
        FROM
          mnt.workorders_copy B   -- THIS IS THE TABLE WITH THE CORRECT (OLD) VALUES
        WHERE
          a.workordersoi    = b.workordersoi      -- THIS MUST BE THE KEY
          AND a.comappstat != b.comappstat
      )
    WHERE
      EXISTS
      (
        SELECT
          B.comappstat
        FROM
          mnt.workorders_copy B
        WHERE
          a.workordersoi    = b.workordersoi      -- THIS MUST BE THE KEY
          AND a.comappstat != b.comappstat
      )
    

    Speed is not so good that you run the query to sub for each row in mnt.workorders
    Note it is condition in where. You need other wise, you will update the unchanged to null values.

    I wouold do it like this:

    UPDATE
      (
        SELECT
          A.workordersoi
          ,A.comappstat
          ,B.comappstat           comappstat_OLD
    
        FROM
          mnt.workorders        A      -- THIS IS THE TABLE YOU WANT TO UPDATE
          ,mnt.workorders_copy  B      -- THIS IS THE TABLE WITH THE CORRECT (OLD) VALUES
    
        WHERE
          a.workordersoi    = b.workordersoi      -- THIS MUST BE THE KEY
          AND a.comappstat != b.comappstat
      ) C
    
    SET
      C.comappstat = comappstat_OLD
    ;
    

    This way you can test the subquery first and know exectly what will be updated.
    This was not a sub query that is executed for each line preformance should be better.

    Kind regards

    Peter

  • Why Windows asks me to install the same updates several times. Often several times the same updates and often several times in the same day?

    I install security updates and important. Sometimes in the same day, I wonder to install the same updates. When I check the history I see that they are already installed. This can happen several times in the same day.

    Troubleshooting Windows Update or Microsoft Update when you are repeatedly offered an update

    UTC/GMT is 00:34 Sunday, March 11, 2012

Maybe you are looking for