Skip to content
Advertisement

Correct database after backup

I need some help with a query. Actually we have some wrongs DATA in our database.

For some technical reason I restore the backup in another schema.

So actually in my Schema1.DB1.TBL1 I got the backed up DATA and in my Schema2.DB2.TBL2 I have the same table but with some wrongs DATA.

Could you please tell me an SQL query which can help me to restore my data?

UPDATE Schema1.DB1.TBL1 
SET 
     Schema1.DB1.TBL1.column1= Schema2.DB2.TBL2.column1 , 
     Schema1.DB1.TBL1.column2= Schema2.DB2.TBL.column2 , 
     Schema1.DB1.TBL1.column3= Schema2.DB2.TBL.column3 , 
     Schema1.DB1.TBL1.column4= Schema2.DB2.TBL.column4 ,
     .....
     Schema1.DB1.TBL1.column100= Schema2.DB2.TBL.column100 
FROM Schema1.DB1.TBL1
JOIN Schema2.DB2.TBL on (Schema1.DB1.TBL1.id= Schema2.DB2.TBL.id)

The problem is I have to many columns. So I’m asking you if there is an easier way?

Advertisement

Answer

I know the answers are multiples there is some way to do it :

/*way 1*/
delete from schema1.db1.tbl
select * into schema1.db1.tbl from schema2.db2.tb2

--or do the same with a truncate
/*way 2*/
delete from schema1.db1.tbl
UPDATE Schema1.DB1.TBL1 
SET 
     Schema1.DB1.TBL1.column1= Schema2.DB2.TBL2.column1 , 
     Schema1.DB1.TBL1.column2= Schema2.DB2.TBL.column2 , 
     Schema1.DB1.TBL1.column3= Schema2.DB2.TBL.column3 , 
     Schema1.DB1.TBL1.column4= Schema2.DB2.TBL.column4 ,
     .....
     Schema1.DB1.TBL1.column100= Schema2.DB2.TBL.column100 
FROM Schema1.DB1.TBL1
JOIN Schema2.DB2.TBL on (Schema1.DB1.TBL1.id= Schema2.DB2.TBL.id)

thanks for your answers as I said, I didn’t know if there is a “right way” to do that.

And definitly I’m sure there are other ways to do that. but it if could help someone.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement