Could you check if is this a valid way to replace data from a restore database to the current database by using MSSQLServer?

Eduardo Fuerte
Eduardo Fuerte used Ask the Experts™
on
Hi Experts

Could you check if is this a valid way to replace data from a restore database to the current database by using MSSQLServer?

I need to replace a range of lines that is incorrect on the current database by using the restore database, I'm planning to do that:
-- -------------------------------------------------------------------------------------
-- Deleting the lines from target database
DELETE FROM INFOSAUDE.dbo.assistido a WHERE WHERE cpf ='';

-- Insert correct lines from RESTORE database

INSERT INTO INFOSAUDE.dbo.assistido a
  SELECT a2.id_assistido FROM INFOSAUDE_REST1302.dbo.assistido a2  WITH(NOLOCK) WHERE cpf ='';
-- -------------------------------------------------------------------------------------

Open in new window


Could you check?

Thanks in advance!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Database Analyst
Commented:
That should be fine and you could drop the alias names as you're dealing with single object in both cases like:

-- -------------------------------------------------------------------------------------
-- Deleting the lines from target database
DELETE FROM INFOSAUDE.dbo.assistido WHERE WHERE cpf ='';

-- Insert correct lines from RESTORE database
INSERT INTO INFOSAUDE.dbo.assistido 
  SELECT * FROM INFOSAUDE_REST1302.dbo.assistido WITH(NOLOCK) WHERE cpf ='';
-- -------------------------------------------------------------------------------------

Open in new window


few considerations though - if the INFOSAUDE.dbo.assistido is accessed heavily and there are many rows to be deleted I would do that in small batches and use ROWLOCK hint on the delete statement.  If the INFOSAUDE.dbo.assistido is not accessed when you perform this data correction it should be all fine.
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Thank you for reply.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial