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