Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Oracle query

Options
  • 23-03-2016 5:03pm
    #1
    Registered Users Posts: 1,830 ✭✭✭


    Quick question. Can the update field and the condition field be the same in an update statement?

    I need the query to return the number of records newly updated, .i.e exclude the records that previously had the 'mytextvalue' value.

    Here's what I'm trying to do but it doesn't work.

    UPDATE mytable
    SET mycolumn = 'mytextvalue'
    WHERE mycolumn <> 'mytextvalue'


Comments

  • Registered Users Posts: 7,500 ✭✭✭BrokenArrows


    If you want to only update fields which are not already the new value then you just compare the field you are modifying. Not sure what your NEW_FLAG column is.
    UPDATE mytable
    SET mycolumn = 'mytextvalue'
    WHERE mycolumn  <> 'mytextvalue'
    


  • Moderators, Technology & Internet Moderators Posts: 1,334 Mod ✭✭✭✭croo


    From here
    CREATE TABLE employees_temp AS SELECT * FROM employees;
    BEGIN
    UPDATE employees_temp SET salary = salary * 1.05 WHERE salary < 5000;
    DBMS_OUTPUT.PUT_LINE('Updated ' || SQL%ROWCOUNT || ' salaries.');
    END;
    /


  • Registered Users Posts: 1,830 ✭✭✭CountingCrows


    If you want to only update fields which are not already the new value then you just compare the field you are modifying. Not sure what your NEW_FLAG column is.
    UPDATE mytable
    SET mycolumn = 'mytextvalue'
    WHERE mycolumn  <> 'mytextvalue'
    

    Sorry, my SQL should be as you posted, I've edit the OP. The statement above doesn't work, hence my confusion.


  • Registered Users Posts: 10,494 ✭✭✭✭28064212


    Sorry, my SQL should be as you posted, I've edit the OP. The statement above doesn't work, hence my confusion.
    What do you mean by it "doesn't work"? Do you get an error, or does it just not update any rows?

    If you do:
    SELECT * FROM mytable WHERE mycolumn  <> 'mytextvalue'
    
    do you get results?

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users Posts: 1,830 ✭✭✭CountingCrows


    28064212 wrote: »
    If you do:
    SELECT * FROM mytable WHERE mycolumn  <> 'mytextvalue'
    
    do you get results?

    I get 0 rows returned. I think I've found my problem, NULL records aern't been included. I thought <> 'mytextvalue' would include NULLS but it seems I need to specify the IS NULL as a separate condition.


  • Advertisement
  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    Try:
    SELECT * 
    FROM mytable 
    WHERE mycolumn  <> 'mytextvalue'
    OR NVL(mycolumn, 0) = 0
    


  • Registered Users Posts: 11,977 ✭✭✭✭Giblet


    I get 0 rows returned. I think I've found my problem, NULL records aern't been included. I thought <> 'mytextvalue' would include NULLS but it seems I need to specify the IS NULL as a separate condition.

    If you need to use equality, turn off ansi nulls, otherwise is null or is not null are required. I would recommend using ansi nulls only though. It's clearer and non ansi nulls are in danger of being removed.


Advertisement