Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Unanswered

Query CRM On Premise Database

Posted on by 395

Hi ,

Is there any easiest way to find out the records created by a user in CRM? I want to check all the records created by a user in one of the CRM on premise instance. I know we can use the linked entities with outer join but i. have lot of entities and need to find out for around 70 or 80 users so want to write an SQl query and search across the DB.

  • David Jennaway Profile Picture
    David Jennaway 14,063 on at
    RE: Query CRM On Premise Database

    You can query the metadata to find all entities that have the 'createdby' attribute, and use a SQL cursor to dynamically query each one. The following will give a list of all records and who created them, so you may want to filter it down before you run it

    declare cur cursor fast_forward for
    select e.ReportViewName, e.Name as EntityName, aPrimary.Name as PrimaryName
    
    from AttributeAsIfPublishedLogicalView a
    join EntityAsIfPublishedLogicalView e on a.EntityId = e.EntityId
    join AttributeAsIfPublishedLogicalView aPrimary on aPrimary.EntityId = e.EntityId
    where a.Name = 'CreatedBy' and e.ReportViewName is not null
    and aPrimary.DisplayMask & 256 = 256	-- Get Primary Attribute
    order by e.Name
    
    declare @sql nvarchar(2000), @ViewName sysname, @EntityName sysname, @PrimaryName sysname
    
    open cur
    fetch next from cur into @ViewName, @EntityName, @PrimaryName
    while @@FETCH_STATUS = 0
    begin
     set @sql = 'select '''   @EntityName   ''', '   @PrimaryName   ' , CreatedByName from '   @ViewName   ' order by CreatedByName'
     exec (@sql)
    fetch next from cur into @ViewName, @EntityName, @PrimaryName
    
    end
    
    close cur
    deallocate cur
    

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Query CRM On Premise Database

    Hello,

    You can use "Union" For that purpose. Here is an example:

    Select
        'account' recordType
        ,a.accountid recordId
        ,a.name recordName
    From FilteredAccount a Where a.createdby = 'Put user guid here'
    Union
    Select
        'contact' recordType
        ,c.contactid recordId
        ,c.fullname recordName
    From FilteredContact c Where c.createdby = 'Put user guid here'

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans