update table variable with join

Answered update table variable with join

  • Monday, August 06, 2012 4:00 PM
     
     

    Hi

    I am using sql server 2000 and I want to update a table variable with join statement. Below is the code am using


    update @test                
    set col =
    (
    select max(anothercol) from realtable a (nolock)               
         where a.id=@test.id  
       ,b.name = @test.name
    )
    where col = 'x'

    And I am getting an error something like 'must declare variable'. Please could anybody re-write and let me know as soon as possible.


    Thanks,
    Rakesh.


    Please mark as helpful and propose as answer if you find this as correct!!! Thanks, Rakesh.

All Replies

  • Monday, August 06, 2012 4:15 PM
    Moderator
     
     Answered Has Code

    Try:

    update T              
    set col = 
    (
    select max(anothercol) from realtable a               
         where a.id=T.id  AND
        a.name = T.name
    )
    FROM @Test T
    where col = 'x'


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    • Proposed As Answer by SQL Novice 01 Monday, August 06, 2012 4:16 PM
    • Unproposed As Answer by Iam_Rakesh Monday, August 06, 2012 4:21 PM
    • Marked As Answer by Iam_Rakesh Monday, August 06, 2012 4:26 PM
    •