Search code examples
sqlsql-servert-sqltemp

sql update temp table from another table


1)#tblTemp

RD
------
20 Aug
 5 Aug
30 Aug
 4 Aug
20 Aug
18 Aug

2) tblMST

startDate
---------
22 Aug
 6 Aug
30 Aug
19 Aug

I want update table #tblTemp using tblMST and need to get output like following:

3) #tblTemp

RD
--------
22 Aug
 6 Aug
30 Aug
 6 Aug
22 Aug
19 Aug

Solution

  • Are you wanting the nearest date from the tblMST? If so, you want to do something like this:

    UPDATE #tblTemp SET RD = (SELECT top(1) startDate FROM tblMST WHERE startDate >= RD ORDER BY startDate)
    

    Again, I'm not sure if this is the logic you are looking for. Let me know if it is not. Thanks!