Deployment Solution

 View Only

Merge Duplicate Computer Records in Deployment Server 

Apr 09, 2009 12:44 PM

Occasionally, the Deployment Server console will show multiple computers with the same computer name. I have created a SQL Query that will merge these duplicate records. To use this SQL Query, open SQL Query Analyzer or Management Studio, create a new query, select your Deployment Server Database (eXpress) as the active database from the drop-down list of databases, copy and paste the following query text into the Query Window, and then execute the query. Everything following a “--” is a comment to help you understand what it does.

--Create a table variable to pair duplicate records for merging
DECLARE @ComputerIDs TABLE
(
  NewCompID int,
  OldCompID int
)
--Find all computers with more than one ID. The larger number is the younger computer record, the smaller number is the older computer record.
INSERT INTO @ComputerIDs (NewCompID, OldCompID)
  SELECT MAX(computer_id) AS NewCompID, MIN(computer_id) AS OldCompID
    FROM computer
    GROUP BY computer_name HAVING COUNT(1) = 2

 --Now update the younger computer record with all the data from the old computer record --Get the Group ID from the old computer record UPDATE N   SET group_id = O.group_id   FROM computer N     JOIN @ComputerIDs I ON N.computer_id = I.NewCompID     JOIN computer O ON I.OldCompID = O.computer_id


--Delete location from the new record DELETE FROM location WHERE computer_id IN (SELECT NewCompID FROM @ComputerIDs)


--Delete service processors from the new record DELETE FROM service_processor WHERE computer_id IN (SELECT NewCompID FROM @ComputerIDs)


--Move location from the old record to the new record UPDATE T SET computer_id = I.NewCompID   FROM location T     JOIN @ComputerIDs I ON T.computer_id = I.OldCompID


--Move service processors from the old record to the new record UPDATE T SET computer_id = I.NewCompID   FROM service_processor T     JOIN @ComputerIDs I ON T.computer_id = I.OldCompID


--Move config items from the old record to the new record UPDATE T SET target_computer_id = I.NewCompID   FROM config_item T     JOIN @ComputerIDs I ON T.target_computer_id = I.OldCompID


--Move event schedules from the old record to the new record UPDATE T SET computer_id = I.NewCompID   FROM event_schedule T     JOIN @ComputerIDs I ON T.computer_id = I.OldCompID


--Move history from the old record to the new record UPDATE T SET computer_id = I.NewCompID   FROM history T     JOIN @ComputerIDs I ON T.computer_id = I.OldCompID UPDATE T SET computer_id = I.NewCompID   FROM history_archive T     JOIN @ComputerIDs I ON T.computer_id = I.OldCompID


--Move status log from the old record to the new record UPDATE T SET computer_id = I.NewCompID   FROM status_log T     JOIN @ComputerIDs I ON T.computer_id = I.OldCompID UPDATE T SET computer_id = I.NewCompID   FROM status_log_archive T     JOIN @ComputerIDs I ON T.computer_id = I.OldCompID


--Delete old computer records DELETE FROM computer WHERE computer_id IN (SELECT OldCompID FROM @ComputerIDs)



        

Statistics
0 Favorited
0 Views
0 Files
0 Shares
0 Downloads

Tags and Keywords

Comments

Sep 25, 2012 02:05 PM

Great script and thanks to Maymne for posting a version I could just copy and paste.

Ran the script and all our duplicates are now gone. Can't thank you enough ! smiley

Mar 21, 2012 09:15 AM

This is gold, thanks so much!  We had several duplicate entries, probably because of VPN and rocket stick users (those portable USB internet sticks).  This script worked like a charm.  As mentioned at the beginning, be sure to select your eXpress database before running the script.

Mar 29, 2010 03:56 PM

Decided to post a working version where the parts where the code bled into the comments have been fixed already, so that people (like me) who are looking for this don't need to do the work again later... Not using Preformatting, because it's doing nasty things to the alignment and so on.

--Create a table variable to pair duplicate records for merging
DECLARE @ComputerIDs TABLE
(
  NewCompID int,
  OldCompID int
)
--Find all computers with more than one ID. The larger number is the younger computer record, the smaller number is the older computer record.
INSERT INTO @ComputerIDs (NewCompID, OldCompID)
  SELECT MAX(computer_id) AS NewCompID, MIN(computer_id) AS OldCompID
    FROM computer
    GROUP BY computer_name HAVING COUNT(1) = 2
 --Now update the younger computer record with all the data from the old computer record
--Get the Group ID from the old computer record
UPDATE N
  SET group_id = O.group_id
  FROM computer N
    JOIN @ComputerIDs I ON N.computer_id = I.NewCompID
    JOIN computer O ON I.OldCompID = O.computer_id

--Delete location from the new record
DELETE FROM location WHERE computer_id IN (SELECT NewCompID FROM @ComputerIDs)

--Delete service processors from the new record
DELETE FROM service_processor WHERE computer_id IN (SELECT NewCompID FROM @ComputerIDs)

--Move location from the old record to the new record
UPDATE T SET computer_id = I.NewCompID
  FROM location T
    JOIN @ComputerIDs I ON T.computer_id = I.OldCompID

--Move service processors from the old record to the new record
UPDATE T SET computer_id = I.NewCompID
  FROM service_processor T
    JOIN @ComputerIDs I ON T.computer_id = I.OldCompID

--Move config items from the old record to the new record
UPDATE T SET target_computer_id = I.NewCompID
  FROM config_item T
    JOIN @ComputerIDs I ON T.target_computer_id = I.OldCompID

--Move event schedules from the old record to the new record
UPDATE T SET computer_id = I.NewCompID
  FROM event_schedule T
    JOIN @ComputerIDs I ON T.computer_id = I.OldCompID

--Move history from the old record to the new record
UPDATE T SET computer_id = I.NewCompID
  FROM history T
    JOIN @ComputerIDs I ON T.computer_id = I.OldCompID
UPDATE T SET computer_id = I.NewCompID
  FROM history_archive T
    JOIN @ComputerIDs I ON T.computer_id = I.OldCompID

--Move status log from the old record to the new record
UPDATE T SET computer_id = I.NewCompID
  FROM status_log T
    JOIN @ComputerIDs I ON T.computer_id = I.OldCompID
UPDATE T SET computer_id = I.NewCompID
  FROM status_log_archive T
    JOIN @ComputerIDs I ON T.computer_id = I.OldCompID

--Delete old computer records
DELETE FROM computer WHERE computer_id IN (SELECT OldCompID FROM @ComputerIDs)

Apr 19, 2009 01:24 PM

Looks like it is working now.  Didn't notice that there was some code included on some of the lines that had the comments.  Had to do a little editing.  Thanks!

Apr 19, 2009 12:58 PM

I ran this, but I get the following error:

Msg 102, Level 15, State 1, Line 12
Incorrect syntax near '='.
Msg 156, Level 15, State 1, Line 41
Incorrect syntax near the keyword 'FROM'.


Any ideas?  We have several duplicate computers.......mostly because we do it by MAC Address, but then laptops that are used wired and wireless create cuplicates. 

May have to start using serial number as a lookup key.

Apr 10, 2009 02:41 PM

Wow thanks for the query.  I know we've been looking for something like this for quite awhile too.  With MAC address set as our primary key the VPN software we use can lead to multiple entries quite frequently.  This should be a huge help.

Apr 09, 2009 02:25 PM

For the longest time I have been looking for this script, but I had no luck. Thank you for writing it, it will be helpful if what I am doing now does not work.
The only thing that I could find to make it so I do not have dupicates was to go to Tools -> Options -> Global tab and put a check next to Synchronize display names with computer names.
The only time that I would think this would not work for some is if the computer name differs from the label it is given in DS.
Again, thank you very much for posting the SQL query. Cheers

Related Entries and Links

No Related Resource entered.