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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Oracle query

  • 23-03-2016 4:03pm
    #1
    Registered Users, Registered Users 2 Posts: 1,832 ✭✭✭


    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, Registered Users 2 Posts: 7,501 ✭✭✭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,336 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, Registered Users 2 Posts: 1,832 ✭✭✭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, Registered Users 2 Posts: 10,906 ✭✭✭✭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, Registered Users 2 Posts: 1,832 ✭✭✭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, Registered Users 2 Posts: 1,987 ✭✭✭Ziycon


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


  • Registered Users, Registered Users 2 Posts: 11,989 ✭✭✭✭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