INSERT data from one table INTO another DB

I can't test this so I need to make sure that what I will do is correct.  Here is my code:

UPDATE [aeeSQL].[Program_LIVE].[dbo].HBM_MATTER
 SET  [aeeSQL].[Program_LIVE].[dbo].HBM_MATTER._PROLAW_FK =Matters. Matters
 FROM Matters
 WHERE  [aeeSQL].[Program_LIVE].[dbo].HBM_MATTER.matter_code = Matters.MatterID
 AND  [aeeSQL].[Program_LIVE].[dbo].HBM_MATTER._PROLAW_FK IS NULL
 AND Matters.MatterID IS NOT NULL

Open in new window


This is what I am trying to do.

The  _PROLAW_FK  is NULL on some records in the  [aeeSQL].[Program_LIVE].[dbo].HBM_MATTER table.  If it is NULL then I want that field to be updated with the Matters field in the Matters table.  The Matters table is in a different DB.  This will be a one time run and I will be running it on the DB where Matters reside.  This is why I don't have reference to that table like I do for HBM_MATTER.

Is my code correct?
huerita37Asked:
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.

Vitor MontalvãoMSSQL Senior EngineerCommented:
Yes, it's correct but you don't need the last test for IS NOT NULL since it's included when you compare it with matter_code.
Since that's an one shot run I recommend you to run it in a transaction so you can rollback if the resuls aren't what you expect:
BEGIN TRAN -- This opens a transaction

-- Run the update
UPDATE [aeeSQL].[Program_LIVE].[dbo].HBM_MATTER
 SET  [aeeSQL].[Program_LIVE].[dbo].HBM_MATTER._PROLAW_FK =Matters. Matters
 FROM Matters
 WHERE  [aeeSQL].[Program_LIVE].[dbo].HBM_MATTER.matter_code = Matters.MatterID
 AND  [aeeSQL].[Program_LIVE].[dbo].HBM_MATTER._PROLAW_FK IS NULL

-- Verify the results
SELECT *
FROM  [aeeSQL].[Program_LIVE].[dbo].HBM_MATTER
WHERE HBM_MATTER._PROLAW_FK IS NULL

-- If data is correct uncomment and run the next line
-- COMMIT

-- otherwise uncomment and run the next line
-- ROLLBACK

Open in new window

0

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
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
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.