Book a Demo

Author Topic: Export Databases and tables to CSV  (Read 4092 times)

Typia

  • EA User
  • **
  • Posts: 88
  • Karma: +1/-0
    • View Profile
Export Databases and tables to CSV
« on: August 08, 2019, 06:45:50 pm »
Hi guys,
I would like to know if there is any way to export a database to a csv file giving tables fields and related attributes like field type, field length ..

Many thanks

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Export Databases and tables to CSV
« Reply #1 on: August 08, 2019, 07:40:23 pm »
Not that easy.
You would need to write an SQL query to get the info out, and not all of the info is that simple to find.

Here a snippet of VBScript code with the query that I use to get the data out. It gets more then only the tables and columns, it also get our logical data model and the mapping between LDM and TDM (Technical Data Model => Database)
If you use the query alone you can put it into an SQL search and from the search results export to CSV.
Or you can write a script and export it directly to Excel like I did

Code: [Select]
function getMappingDetails(sourcePackage, targetPackage)
'get the pakage tree id's
dim sourcePackageTreeIDs
sourcePackageTreeIDs = getPackageTreeIDString(sourcePackage)
dim targetPackageTreeIDs
targetPackageTreeIDs = getPackageTreeIDString(targetPackage)
dim sqlGetMappingDetails
sqlGetMappingDetails =  "select                                                                                                                                                             " & vbNewLine & _
"   ldm.[Domain], ldm.Entity as [Class], ldm.Attribute as Attribute, ldm.AttributeType as AttributeType                                                             " & vbNewLine & _
" , ldm.TimeSliced, ldm.Versioned, ldm.Mandatory, LEFT(ldm.facets,LEN(ldm.facets)-PATINDEX('%[^'+CHAR(13)+CHAR(10)+']%',REVERSE(ldm.facets))+1) as Facets           " & vbNewLine & _
" , ldm.Type as LDMType, ldm.CLASSGUID as [Guid]                                                                                                                    " & vbNewLine & _
" , tdm.[Database], tdm.Entity as [Table], tdm.Attribute as [Column]                                                                                                " & vbNewLine & _
" , tdm.Datatype, tdm.PK, tdm.NotNull                                                                                                                               " & vbNewLine & _
" , case when left(convert( varchar(max),tdm.[Default]), 1) = '''' then '''' else '' end + convert(varchar(max),tdm.[Default]) as [Default]                         " & vbNewLine & _
" , tdm.Type as TDMType, tdm.CLASSGUID as [Guid]                                                                                                                    " & vbNewLine & _
" , case when p.Package_ID is null then 'false' else 'true' END as EmptyMapping                                                                                     " & vbNewLine & _
" , tdm.Comment, tdm.MappingNotes                                                                                                                                   " & vbNewLine & _
" from (                                                                                                                                                            " & vbNewLine & _
" select o.ea_guid AS CLASSGUID, o.Object_Type AS CLASSTYPE, o.name as Entity, null as Attribute, 'Table' as Type, tv.Value as LDMGuid, p1.Name as [Database]       " & vbNewLine & _
" , CASE WHEN CHARINDEX('<description>',tv.NOTES) > 0 THEN                                                                                                          " & vbNewLine & _
"   SUBSTRING(tv.NOTES,CHARINDEX('<description>',tv.NOTES)+LEN('<description>'),                                                                                    " & vbNewLine & _
"              CHARINDEX('</description>',tv.NOTES)-CHARINDEX('<description>',tv.NOTES) - LEN('<description>')) END as Comment                                      " & vbNewLine & _
", null as Datatype, null as PK, null as NotNull, null as [Default] , tv.Notes as MappingNotes                                                                      " & vbNewLine & _
" from t_object o                                                                                                                                                   " & vbNewLine & _
" left join t_objectproperties tv on tv.Object_ID = o.Object_ID                                                                                                     " & vbNewLine & _
"                                                       and tv.Property in ('sourceElement','linkedAssociation', 'linkedAttribute')                                 " & vbNewLine & _
" inner join t_package p on p.Package_ID = o.Package_ID                                                                                                             " & vbNewLine & _
" left join t_package p1 on p1.Package_ID = p.Parent_ID                                                                                                             " & vbNewLine & _
" where 1 = 1                                                                                                                                                       " & vbNewLine & _
" and o.Object_Type = 'Class'                                                                                                                                       " & vbNewLine & _
" and o.Stereotype = 'table'                                                                                                                                        " & vbNewLine & _
" and o.Package_ID in (" & sourcePackageTreeIDs  & ")                                                                                                               " & vbNewLine & _
" union all                                                                                                                                                         " & vbNewLine & _
" select a.ea_guid AS CLASSGUID, 'Attribute' AS CLASSTYPE, o.name as Owner, a.Name as Attribute, 'Column' as Type, tv.VALUE as LDMGuid, p1.Name as [Database]       " & vbNewLine & _
" , CASE WHEN CHARINDEX('<description>',tv.NOTES) > 0 THEN                                                                                                          " & vbNewLine & _
"   SUBSTRING(tv.NOTES,CHARINDEX('<description>',tv.NOTES)+LEN('<description>'),                                                                                    " & vbNewLine & _
"              CHARINDEX('</description>',tv.NOTES)-CHARINDEX('<description>',tv.NOTES) - LEN('<description>')) END as Comment                                      " & vbNewLine & _
", a.type  + CASE WHEN dt.Size = 1 THEN '(' + cast(a.Length as varchar) + ')'                                                                                       " & vbNewLine & _
"WHEN dt.Size = 2 THEN '(' + cast(a.Precision as varchar) + ', ' + cast(a.Scale as varchar)  + ')'                                                                  " & vbNewLine & _
" ELSE '' END as Datatype                                                                                                                                           " & vbNewLine & _
",a.IsOrdered as PK, a.AllowDuplicates as NotNull, a.[Default]  , tv.Notes as MappingNotes                                                                          " & vbNewLine & _
" from t_attribute a                                                                                                                                                " & vbNewLine & _
" inner join t_object o on o.Object_ID = a.Object_ID                                                                                                                " & vbNewLine & _
"left join t_datatypes dt on dt.ProductName = o.GenType                                                                                                             " & vbNewLine & _
"                                         and dt.DataType = a.Type                                                                                                  " & vbNewLine & _
"                                         and dt.Type = 'DDL'                                                                                                       " & vbNewLine & _
" left join t_attributetag tv on tv.ElementID = a.ID                                                                                                                " & vbNewLine & _
"                                                       and tv.Property in ('sourceElement','linkedAssociation', 'linkedAttribute')                                 " & vbNewLine & _
" inner join t_package p on p.Package_ID = o.Package_ID                                                                                                             " & vbNewLine & _
" left join t_package p1 on p1.Package_ID = p.Parent_ID                                                                                                             " & vbNewLine & _
" where 1 = 1                                                                                                                                                       " & vbNewLine & _
" and o.Object_Type = 'Class'                                                                                                                                       " & vbNewLine & _
" and o.Stereotype = 'table'                                                                                                                                        " & vbNewLine & _
" and o.Package_ID in (" & sourcePackageTreeIDs  & ")                                                                                                               " & vbNewLine & _
" )tdm                                                                                                                                                              " & vbNewLine & _
" full outer join (                                                                                                                                                 " & vbNewLine & _
" select  o.ea_guid AS CLASSGUID, o.Object_Type AS CLASSTYPE, o.name as Entity, null as Attribute, o.Object_Type as Type, o.ea_guid as LDMGuid, p1.Name as [Domain] " & vbNewLine & _
" , tc.Value as TimeSliced, vs.Value as Versioned                                                                                                                   " & vbNewLine & _
" ,null as Mandatory, null as AttributeType, null as facets                                                                                                         " & vbNewLine & _
" from t_object o                                                                                                                                                   " & vbNewLine & _
" inner join t_package p on p.Package_ID = o.Package_ID                                                                                                             " & vbNewLine & _
" left join t_package p1 on p1.Package_ID = p.Parent_ID                                                                                                             " & vbNewLine & _
" left join t_objectProperties tc on tc.Object_ID = o.Object_ID                                                                                                     " & vbNewLine & _
" and tc.Property = 'Timesliced'                                                                                                  " & vbNewLine & _
" left join t_objectProperties vs on vs.Object_ID = o.Object_ID                                                                                                     " & vbNewLine & _
" and vs.Property = 'Versioned'                                                                 " & vbNewLine & _
" where 1 = 1                                                                                                                                                       " & vbNewLine & _
" and o.Object_Type = 'Class'                                                                                                                                       " & vbNewLine & _
" and o.Package_ID in (" & targetPackageTreeIDs  & ")                                                                                                               " & vbNewLine & _
" union all                                                                                                                                                         " & vbNewLine & _
" select a.ea_guid AS CLASSGUID, 'Attribute' AS CLASSTYPE, o.name as Owner, a.Name as Attribute, 'Attribute' as Type, a.ea_guid as LDMGuid, p1.Name as [Domain]     " & vbNewLine & _
" , tc.Value as TimeSliced, vs.Value as Versioned                                                                                                                   " & vbNewLine & _
" , a.lowerbound as Mandatory, isnull(cl.Name, a.Type) as AttributeType                                                                                             " & vbNewLine & _
",  case when ftd.Property is not null then ftd.Property + ':' + ftd.Value + char(13) + char(10) else '' end                                                        " & vbNewLine & _
"+ case when ffd.Property is not null then ffd.Property + ':' + ffd.Value + char(13) + char(10) else '' end                                                         " & vbNewLine & _
"+ case when fl.Property is not null then fl.Property + ':' + fl.Value + char(13) + char(10) else '' end                                                            " & vbNewLine & _
"+ case when fmie.Property is not null then fmie.Property + ':' + fmie.Value + char(13) + char(10) else '' end                                                      " & vbNewLine & _
"+ case when fme.Property is not null then fme.Property + ':' + fme.Value + char(13) + char(10) else '' end                                                         " & vbNewLine & _
"+ case when fmi.Property is not null then fmii.Property + ':' + fmii.Value + char(13) + char(10) else '' end                                                       " & vbNewLine & _
"+ case when fmi.Property is not null then fmi.Property + ':' + fmi.Value + char(13) + char(10) else '' end                                                         " & vbNewLine & _
"+ case when fmil.Property is not null then fmil.Property + ':' + fmil.Value + char(13) + char(10) else '' end                                                      " & vbNewLine & _
"+ case when fml.Property is not null then fml.Property + ':' + fml.Value + char(13) + char(10) else '' end                                                         " & vbNewLine & _
"+ case when fp.Property is not null then fp.Property + ':' + fp.Value + char(13) + char(10) else '' end                                                            " & vbNewLine & _
"as facets                                                                                                                                                          " & vbNewLine & _
" from t_attribute a                                                                                                                                                " & vbNewLine
(part 1 of the code)

Geert



Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Export Databases and tables to CSV
« Reply #2 on: August 08, 2019, 07:41:17 pm »
part 2 of the code (stupid 20000 character limi :-X)
Code: [Select]
& _
" inner join t_object o on o.Object_ID = a.Object_ID                                                                                                                " & vbNewLine & _
" inner join t_package p on p.Package_ID = o.Package_ID                                                                                                             " & vbNewLine & _
" left join t_package p1 on p1.Package_ID = p.Parent_ID                                                                                                             " & vbNewLine & _
"  left join t_attributetag tc on tc.ElementID = a.ID                                                                                                               " & vbNewLine & _
" and tc.Property = 'Timesliced'                                                                                                  " & vbNewLine & _
" left join t_attributetag vs on vs.ElementID = a.ID                                                                                                                " & vbNewLine & _
" and vs.Property = 'Versioned'                                                                                                 " & vbNewLine & _
" left join t_object cl on cl.Object_ID = a.Classifier                                                                                                              " & vbNewLine & _
" left join t_objectproperties ftd on ftd.Object_ID = cl.Object_ID                                                                                                  " & vbNewLine & _
" and ftd.Property = 'totalDigits'                                                                                                    " & vbNewLine & _
"left join t_objectproperties ffd on ffd.Object_ID = cl.Object_ID                                                                                                   " & vbNewLine & _
" and ffd.Property = 'fractionDigits'                                                                                                 " & vbNewLine & _
"left join t_objectproperties fl on fl.Object_ID = cl.Object_ID                                                                                                     " & vbNewLine & _
" and fl.Property = 'length'                                                                                                          " & vbNewLine & _
"left join t_objectproperties fmie on fmie.Object_ID = cl.Object_ID                                                                                                 " & vbNewLine & _
" and fmie.Property = 'minExclusive'                                                                                                  " & vbNewLine & _
"left join t_objectproperties fme on fme.Object_ID = cl.Object_ID                                                                                                   " & vbNewLine & _
" and fme.Property = 'maxExclusive'                                                                                                   " & vbNewLine & _
"left join t_objectproperties fmii on fmii.Object_ID = cl.Object_ID                                                                                                 " & vbNewLine & _
" and fmii.Property = 'minInclusive'                                                                                                  " & vbNewLine & _
"left join t_objectproperties fmi on fmi.Object_ID = cl.Object_ID                                                                                                   " & vbNewLine & _
" and fmi.Property = 'maxInclusive'                                                                                                   " & vbNewLine & _
"left join t_objectproperties fmil on fmil.Object_ID = cl.Object_ID                                                                                                 " & vbNewLine & _
" and fmil.Property = 'minLength'                                                                                                     " & vbNewLine & _
"left join t_objectproperties fml on fml.Object_ID = cl.Object_ID                                                                                                   " & vbNewLine & _
" and fml.Property = 'maxLength'                                                                                                      " & vbNewLine & _
"left join t_objectproperties fp on fp.Object_ID = cl.Object_ID                                                                                                     " & vbNewLine & _
" and fp.Property = 'pattern'                                                                                                         " & vbNewLine & _
" where 1 = 1                                                                                                                                                       " & vbNewLine & _
" and o.Object_Type = 'Class'                                                                                                                                       " & vbNewLine & _
" and o.Package_ID in (" & targetPackageTreeIDs  & ")                                                                                                               " & vbNewLine & _
" )ldm on ldm.LDMGuid = tdm.LDMGuid                                                                                                                                 " & vbNewLine & _
" left join t_package p on p.ea_guid = tdm.LDMGuid                                                                                                                  " & vbNewLine & _
" order by isnull(ldm.Entity, 'ZZ'), ldm.Attribute, tdm.Entity, tdm.Attribute                                                                                       "
    dim queryResults
'debug
' Dim debugFile
' set debugFile = new TextFile
' debugFile.Contents = sqlGetMappingDetails
' debugFile.FullPath = "h:\temp\mappingQuery.txt"
' debugFile.Save
set queryResults = getArrayListFromQuery(sqlGetMappingDetails)
'return
set getMappingDetails = queryResults
end function

Geert

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Export Databases and tables to CSV
« Reply #3 on: August 08, 2019, 08:07:04 pm »
Stupid indeed. However, you could have saved 10k chars in spaces xD

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Export Databases and tables to CSV
« Reply #4 on: August 08, 2019, 08:56:25 pm »
Stupid indeed. However, you could have saved 10k chars in spaces xD

q.
Sure, but I just wanted to copy the code from the source as it, without messing with the formatting.
(in EA's script editor it looks a lot better)

Geert