update table with next value

  • ronan.healy (10/5/2012)


    thanks very much for help will try that

    and also will try improve my [post in future

    again thanks

    No problem. Happy to help. The best thing you can do is to test your ddl and dml so you know it will work. 😀 Let me know if that doesn't work.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • hi

    out of interest is it possible to do it this way

    update ACRT set MIDIRB = min(IRBT2.IRBRATING)

    from ACRT,IRBT,IRBT2 where ACRT.MIDIRB = IRBT.IRBRATING

    and IRBT2.IRBRATING > IRBT.IRBRATING

    and if so what do i have to do to get this error out of it

    Msg 208, Level 16, State 1, Line 1

    Invalid object name 'IRBT2'.

    IRBT2 is a table in database

  • ronan.healy (10/9/2012)


    hi

    out of interest is it possible to do it this way

    update ACRT set MIDIRB = min(IRBT2.IRBRATING)

    from ACRT,IRBT,IRBT2 where ACRT.MIDIRB = IRBT.IRBRATING

    and IRBT2.IRBRATING > IRBT.IRBRATING

    and if so what do i have to do to get this error out of it

    Msg 208, Level 16, State 1, Line 1

    Invalid object name 'IRBT2'.

    IRBT2 is a table in database

    No - there's an aggregate operator but no GROUP BY clause.

    -- test this

    SELECT

    ACRT.*,

    x.MIDIRB

    FROM ACRT

    INNER JOIN IRBT

    ON IRBT.IRBRATING = ACRT.MIDIRB

    CROSS APPLY (

    SELECT MIDIRB = MIN(IRBT2.IRBRATING)

    FROM IRBT2

    WHERE IRBT2.IRBRATING > IRBT.IRBRATING

    ) x

    -- if it correctly displays the rows to be updated and the correct value to update to,

    -- then convert the SELECT to an UPDATE:

    UPDATE ACRT SET MIDIRB = x.MIDIRB

    FROM ACRT

    INNER JOIN IRBT

    ON IRBT.IRBRATING = ACRT.MIDIRB

    CROSS APPLY (

    SELECT MIDIRB = MIN(IRBT2.IRBRATING)

    FROM IRBT2

    WHERE IRBT2.IRBRATING > IRBT.IRBRATING

    ) x


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • thanks for response will try that

  • still getting this error

    Msg 208, Level 16, State 1, Line 1

    Invalid object name 'IRBT2'.

  • ronan.healy (10/9/2012)


    still getting this error

    Msg 208, Level 16, State 1, Line 1

    Invalid object name 'IRBT2'.

    Is that a table? In your original post you did not mention that table.

    And fwiw you should use the newer join syntax instead of the old style joins.

    update ACRT set MIDIRB = min(IRBT2.IRBRATING)

    from ACRT

    join IRBT on ACRT.MIDIRB = IRBT.IRBRATING

    join IRBT2 on IRBT2.IRBRATING > IRBT.IRBRATING

    This imho is easier to read and is less prone to coding error. With the comma separated list it is very easy to forget a join condition and you end up with a cross join.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 6 posts - 16 through 20 (of 20 total)

You must be logged in to reply to this topic. Login to reply