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

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!
Eduardo FuerteDeveloper and AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lcohanDatabase AnalystCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eduardo FuerteDeveloper and AnalystAuthor Commented:
Thank you for reply.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.