Script to clean up SyncEntry_<GUID> and SubscriptionStatistics_<GUID> tables in the SQL database

Published 07 May 09 11:10 AM | ukdynsupport 

SyncEntry_<GUID> and SubscriptionStatistics_<GUID> tables are created in the <CompanyName>_MSCRM SQL Database when using the CRM-outlook clients.  Generally you will have 2 of each table for each Online Client, and 3 of each table for each Offline Client.  However, these tables are created per user per machine, so in a citrix farm environment or if users have recently changed hardware, you may find that you have an extraordinary amount of these tables and would like to remove them from the database.

Below is a script that removes all SyncEntry_<GUID> and SubscriptionStatistics_<GUID> tables, along with corresponding records in other tables:

Declare     @SyncEnt    char(60),

      @sql        nchar(100),

      @sqlSync        nchar(100),

      @DN               char(50)

               

Declare User_cursor CURSOR for

               

select DomainName from SystemUserBase

               

                OPEN User_cursor

 

                FETCH NEXT FROM User_cursor INTO @DN

 

                WHILE @@Fetch_Status = 0

 

BEGIN

 

DECLARE CRMSync_cursor CURSOR FOR

select substring(SyncEntryTableName,11,42) as SyncEntryGUID from subscription where systemuserid in

      (select systemuserid from systemuserbase where domainname =@DN)

 

OPEN CRMSync_cursor

 

FETCH NEXT FROM CRMSync_cursor INTO @SyncEnt

 

WHILE @@Fetch_Status = 0

BEGIN

    SELECT @sql = 'DROP TABLE SubscriptionStatistics_' +(@SyncEnt)

                SELECT @sqlSync = 'DROP TABLE SyncEntry_' +(@SyncEnt)

 

                EXEC sp_executesql @sql

                EXEC sp_executesql @sqlSync

                FETCH NEXT FROM CRMSync_cursor INTO @SyncEnt

END

 

 

CLOSE CRMSync_cursor

DEALLOCATE CRMSync_cursor

 

delete from subscriptionclients where subscriptionid in

      (select subscriptionid from subscription where systemuserid in

      (select systemuserid from systemuserbase where domainname = @DN))

 

 

delete from Subscriptionsyncinfo where subscriptionid in

(select subscriptionid from subscription where systemuserid in

      (select systemuserid from systemuserbase  where domainname = @DN))

 

 

delete from subscription where systemuserid in

      (select systemuserid from systemuserbase where domainname = @DN)

 

                FETCH NEXT FROM User_cursor INTO @DN

END

 

CLOSE User_cursor

DEALLOCATE User_cursor

 

Best Regards,

Justin Thorp

CRM Senior Support Engineer

Filed under: ,

Comments

# Script to clean up SyncEntry_ and SubscriptionStatistics_ tables in the SQL database | Microsoft Share Point said on May 7, 2009 6:22 AM:

PingBack from http://microsoft-sharepoint.simplynetdev.com/script-to-clean-up-syncentry_-and-subscriptionstatistics_-tables-in-the-sql-database/

# check said on June 2, 2009 6:32 AM:

Hello Justin,

This is a very usefull query you've developed here and works very well indeed!  However, I've noticed that it doesn't remove sync tables in a imported organisation, where the tables were created prior to the import.  I assume that this is because there's no domain history in the CRM database?

# ukdynsupport said on June 4, 2009 12:28 PM:

Hi,

I've tried importing an organization and I wasn't able to reproduce this issue.

I think that the only reason the script wouldn't work is if you have a SystemUserId in the Subscription table that doesn't exist in the SystemUserBase table.  You can identify these records by running the following script:

select * from Subscription where SystemUserId not in (select SystemuserId from SystemUserBase)

If you do have some records returned by this statement, you can run through the following steps to clean the remaining tables (it's a slightly more manual process):

1.)  Using SQL Management Studio, create "Drop Table" scripts for all SyncEntry_<guid> tables:

A.)  Right click on your database and choose Tasks => Generate Scripts...

B.)  Choose Next, and select your <CompanyName>_MSCRM database and choose next

C.)  In the next window, change all options to false except "Script Drop" (this should be set to true).  Then choose next.

D.)  Mark "Tables" as the object type, then hit next.

E.)  Mark all SyncEntry_<guid> tables.  (It'll probably be easier to choose "Select All", then unmark all tables except the SyncEntry tables).  Hit Next.

F.)  Choose to script to a new query window, then hit finish.

G.)  Once this is done, you should have a script that will drop all current existing SyncEntry tables.

H.)  Execute this script against your <CompanyName>_MSCRM database

2.)  Repeat Step 1, except when you get to part E, mark all SubscriptionStatistics_<guid> tables.

3.)  Run the following SQL Script against the <CompanyName>_MSCRM database:

delete SubscriptionSyncInfo

GO

delete SubscriptionClients

GO

delete Subscription

GO

Thank you,

Justin

Anonymous comments are disabled

Search

This Blog

Syndication

Page view tracker