Solved

Comparing data in two tables

Posted on 2016-08-03
3
88 Views
Last Modified: 2016-08-04
I have two DB's.  To make things easy we will call them Source and Target.  I need to compare a table in each DB and if they don't match in Target then I need to either Delete/Update/Insert.  The fields I need to compare are the Employee, Percentage and AssignedType.  Here is how things break down.

AssignedType

  1. Matter Originator
  2. Paralegal
  3. Attorney
  4. Responsible

Each Client Case is called a 'Matter'.  Each 'Matter' can have multiple AssignedTypes and each type can have multiple Employees.  At anytime the Employee or Percentage can change for each AssignedType.  I need to compare the Source and Target and make sure they are the same.  Below is what I have so far.  I tried to use the MERGE technique but I am having trouble with a couple of spots.  I know that the Target DB can not be the remote DB so I run the code on the Target DB and call the Source DB.  
WITH ctePL AS
(
	SELECT m.Matters, mp.AssignedPercent, p.Initials, hbm.Matter_Uno, mp.AssignedType, hbmp.Empl_Uno,
	(SELECT TOP 1 ADRT FROM tbl_ADRAssignedType WHERE ProLaw = mp.AssignedType) AS adAssignedType
	FROM Source.matters m
	JOIN Source.MattersProfessionals mp on m.Matters= mp.Matters
	JOIN Source.Professionals p ON mp.Professionals = p.Professionals
	JOIN Target.HBM_Matter hbm ON hbm._ProLaw_FK = m.Matters
	JOIN Target.HBM_PERSNL hbmp ON hbmp.Initials = p.Initials
)

MERGE Target.TBM_CLMAT_PART AS Target
USING ctePL AS Source ON Target.Matter_Uno = Source.Matter_Uno AND Target.Part_Cat_Code = Source.AssignedType AND Target.Empl_Uno = Source.Empl_Uno

WHEN NOT MATCHED BY TARGET THEN
INSERT (Row_Uno, Rec_Type, Matter_Uno, Client_Uno, Part_Cat_Code, Eff_Date, To_Date, Empl_Uno, Percentage, Last_Modified)
VALUES ((SELECT LastKey + 1 FROM Target.CMS_UNIQUE_KEYS WHERE TBNAME = 'TBM_CLMAT_PART'), '1', Source.Matter_Uno, '0', 
		Source.adAssignedType, '2014-02-06 00:00:00.000', '2099-12-31 00:00:00.000', Source.Empl_Uno, 100, '2014-02-06 00:00:00.000')

WHEN MATCHED THEN
UPDATE SET Target.Percentage = Source.AssignedPercent

WHEN NOT MATCHED BY SOURCE AND Target.Matter_Uno = Source.Matter_Uno THEN
DELETE;

Open in new window


The problem with the INSERT section is that the ID or what we call 'Row_Uno' is not an auto increment.  Each time we INSERT then we need to get the ID from Target.CMS_UNIQUE_KEYS WHERE TBNAME = 'TBM_CLMAT_PART'.  Once we INSERT into the Target table we need to UPDATE Target.CMS_UNIQUE_KEYS.

The problem with the DELETE section is that it DELETES everything.  I need help with 'WHEN NOT MATCHED BY SOURCE '.  

Thanks in advance.
0
Comment
Question by:huerita37
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 42

Accepted Solution

by:
Eugene Z earned 500 total points
ID: 41741559
may be Merge is not the best statement for your case

check alternative ways
to compare tables data:
for example
NO EXISTS;
EXCEPT and INTERSECT (Transact-SQL)
https://msdn.microsoft.com/en-us/library/ms188055.aspx

LEFT RIGHT JOINS

---BTW: what is your SQL SERVER Version , edition Service PACK
is sql 2008 ( you may need to get the latest Service pack that should have some patches for MERGE bugs)
1
 

Author Comment

by:huerita37
ID: 41742763
I do have sql 2008.  I will look into your suggestions.

Thanks.
0
 

Author Closing Comment

by:huerita37
ID: 41742765
I won't be able to get back to test your solution but I didn't want to leave the question open.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Building JSON Results Table FROM DB 9 38
SQL Select Query help 1 38
SQL Server Compression Decision 5 48
Trouble installing msi file with msiexe.exe 2 20
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question