Skip to content
Advertisement

SQL request from 2 different databases with same structure

I have 2 really similar databases, with exact same structure (one of them is a backup of the other so some values changed which is why they are similar but not the exact same).

So here is what I would like to do, taking value from database 2 and updating database 1 with it (so it will allow me later to backup some data from a certain user without having to do it all manually or to backup everyone)

UPDATE s 
SET 
    t1.column=t2.column 
FROM database1.table1 t1 
JOIN database2.table1 t2 
WHERE t1.table2='test' 
    AND t2.table2='test'

I tried something like this but it didn’t work, both database are in a same server and “next to each others”, also names are different, so I wanted to know if what I try to do is possible or not

Advertisement

Answer

If table’s name is Test, then try this query for Update (instead of key, place column primary key of table) :

 Update database1.dbo.test
 Set 
 database1.dbo.test.Column=t2.Column
    From
    (Select Column,Key from database2.dbo.test) t2
    Join database1.dbo.test t1
    On t2.Key=t1.Key

Or

UPDATE pereger.dbo.characters
   SET  
   pereger.dbo.characters.level=t2.level FROM
   (SELECT level,characterId FROM 
   peregercopy.dbo.characters) t2 
   JOIN pereger.dbo.characters t1 ON 
   t2.characterID=t1.characterId
   WHERE t2.characterId=5 
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement