Results 1 to 5 of 5

Thread: UPDATE specific records

  1. #1
    Join Date
    Oct 2008
    Posts
    2

    UPDATE specific records

    hi, i am tryin to update 6 specific records out of 400 odd. Problem is my statement is updating all 400 records.

    UPDATE AM_X_422_2
    SET
    security_level =
    (
    SELECT
    MAX(security_level)
    FROM
    (
    SELECT
    code_name,
    LENGTH(code_name),
    security_level,
    mission_date
    FROM
    AM_X_422_2
    ORDER BY
    mission_date desc
    )
    WHERE
    rownum < 11
    )
    WHERE
    LENGTH(code_name) > 7
    ;


    I want to update the security level to the highest security level for missions which are within the 10 most recent missions and where length of the first word of the mission code_name exceeds 7 characters.

  2. #2
    Join Date
    Oct 2008
    Posts
    2
    statement has been changed to this:
    UPDATE AM_X_422_2
    SET
    security_level =
    (
    SELECT
    security
    FROM
    (
    SELECT
    code_name,
    LENGTH(code_name),
    MAX(security_level) security,
    mission_date
    FROM
    AM_X_422_2
    ORDER BY
    mission_date desc
    GROUP BY
    security_level
    )
    WHERE
    rownum < 11
    )
    WHERE
    LENGTH(code_name) > 7
    ;

    getting a ORA-00907 error. something to do with the group function???

  3. #3
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    You can't have aggregated and non aggregated columns in SELECT list. You are grouping by security_level but other columns are not grouped or aggregated.

  4. #4
    Join Date
    Oct 2008
    Posts
    2
    UPDATE AM_X_422_2 am
    SET
    am.security_level =
    (
    SELECT
    lev
    FROM
    (
    SELECT
    max(am.security_level) lev
    FROM
    (
    SELECT
    name,
    no,
    am.security_level
    FROM
    (
    SELECT
    am.code_name name,
    LENGTH(am.code_name) no,
    am.security_level,
    am.mission_date
    FROM
    AM_X_422_2 am
    ORDER BY
    am.mission_date desc
    )
    WHERE
    rownum < 11
    )
    WHERE
    LENGTH(name) > 7
    )over
    WHERE
    over.lev= am.security_level)

    changed the code which now brings up the ORA-01407 error, cannot update to null value. I did check the select statement which does return a max value

  5. #5
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    With MAX function Oracle returns null record if there are no records matching your condition. For example

    select max(dummy) from dual where dummy <>'X';

    returns one row with null value. Check if similar thing is happening in your code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •