Cross table update for Ms SQL T-SQL

Yesterday I make some mistake on accidentally update the data in the table. But lucky we have a daily backup of database in our server. So try to restore back the backup to a new database and the do a cross database and cross table update to recover back the latest possible data.

Try to google and MSN with some of my experience friend and found out this, just to upload to here in case may need it again in the future.Tongue out *hopefully will not use it again la...

Cross table update

UPDATE titles
        SET ytd_sales = t.ytd_sales + s.qty
        FROM titles t, sales s
        WHERE t.title_id = s.title_id
        AND s.ord_date = (SELECT MAX(sales.ord_date) FROM sales)

get from here

Cross database query

select *
from dbo.ThisTable t1
join ThatDatabase.dbo.ThatTable t2
on t1.KeyColumn = t2.KeyColumn

Post a Comment