Search code examples
sqlsql-servert-sqlsql-server-2000

Update using ranges provided in another table


I have two table

**T1**      

object  Value   Calculated_Name 

AA       10 
BB       100    
CC       150    

**T2**  

R1  R2     Name

1   15      Z
16  130     w

I want to update Calculated_Name with name like this

**T1**      

object  Value   Calculated_Name 

AA       10        Z
BB       100       W
CC       150      Null

How I can do this? PS: I have to run the command on SQL2000 or higher


Solution

  • You seem to have a relationship for update like if the value is between the range specified in the second table. Ranges also look inclusive from data.

    update t
    set 
    Calculated_Name =Name
    from
    t1 t join
    t2 p on t.value between r1 and r2