On-Premise multi-organization SQL job

by Bill Owens 29. June 2011 22:33

I have a client with 20+ organizations on their CRM server and I just ran into an issue where we really needed to issue un update SQL command against all of the CRM’s currently installed on the server. In this case we needed to update the Case’s subjectid if it was not filled in and there is a kb article attached to the case. The KB Article has the subjectid we need to update into the case entity. I must also add this is CRM 4.0 On-Premise. I playing around I created the following script that reads the MSCRM_CONFIG DB for the organization databases and then dynamically executes an update to the databases

-- =============================================
-- Author:        Bill Owens    
-- Create date: 6/29/2011
-- Description:    Update all of the Case Subjects with the KB Article's Subject
-- =============================================
SET NOCOUNT ON;
DECLARE @DB as Varchar(100)
DECLARE @SQL AS nVarChar(4000)
DECLARE Org_Cursor CURSOR FOR
    SELECT     DatabaseName
    FROM         mscrm_config.dbo.Organization
    WHERE     (IsDeleted = 0)
OPEN Org_Cursor;
FETCH NEXT FROM Org_Cursor into @DB;
WHILE @@FETCH_STATUS = 0
   BEGIN
        SET @SQL = 'Select count(*) as cnt from ' + @DB +'.dbo.AccountBase'
        set @SQL = 'UPDATE IncidentBase SET SubjectId = KbArticleBase.SubjectId FROM '+@DB +'.dbo.IncidentBase INNER JOIN '+@DB +'.dbo.KbArticleBase ON IncidentBase.KbArticleId = KbArticleBase.KbArticleId WHERE('+@DB +'.dbo.IncidentBase.SubjectId IS NULL) AND (NOT ('+@DB +'.dbo.IncidentBase.KbArticleId IS NULL))'
        EXEC sp_executesql @SQL
      FETCH NEXT FROM Org_Cursor into @DB;
   END;
CLOSE Org_Cursor;
DEALLOCATE Org_Cursor;

. FYI, cross scripting needs to be turned on on the SQL server for this to work.

Tags: ,

CRM 4.0

Page List

About the author

I work for a consulting firm in Dublin Ohio called Affiliated Resource Group. For the last five years I have been spearheading our Microsoft Dynamics CRM practice. I have a deep appreciation for the Microsoft CRM platform and I am very excited about it. You might even describe me as a Microsoft CRM Advocate. I have many battle scars from my experience with the product and I’m constantly being asked questions about CRM and how-to-do something in it. Hence, this BLOG is to help disseminate that knowledge and information to everyone. As of last year I was posting links to many other blogs to help spread the knowledge, but now with the community.dynamics.com doing that for me, I will be following that practice unless a really juicy article catches my eye. Many people have asked where my post are for the first half of 2010, my company had me posting to another blog and maintain two was near impossible. I am now down to just this blog. So good luck and I hope that this blog may help in some way. If you have suggestions or questions, please email me them.

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012 BillOnCRM