How can I get any database structure in xml using a single SQL query?

please, any authority knows how I can get my database structure as xml

I have a database: exdb
I have a table: extbl1
I have a column: excol1
I have a column: excol2
I have a column: excol3
& amp ; there is a column: excol4
.... etc.

how can i get the following structure

<DataBase DataBaseName="exdb">

 <Table TableName="extbl1">
  <Column ColumnName="excol1"/>
  <Column ColumnName="excol2"/>
  <Column ColumnName="excol3"/>
  <Column ColumnName="excol4"/>
 </Table>

 <Table TableName="extb2">
  <Column ColumnName="excol1"/>
  <Column ColumnName="excol2"/>
  <Column ColumnName="excol3"/>
  <Column ColumnName="excol4"/>
 </Table>

</DataBase>
+4
source share
1 answer

Using for xmland subqueries is pretty simple:

select 
'exdb' [@DataBaseName],

(select t.table_name [@TableName],

 (select c.COLUMN_NAME [@ColumnName] 
    from INFORMATION_SCHEMA.COLUMNS c
    where c.TABLE_NAME = t.TABLE_NAME
  for xml path('Column'), type)

 from INFORMATION_SCHEMA.TABLES t
for xml path('Table'), type)

for xml path('DataBase')
+1
source

Source: https://habr.com/ru/post/1529308/


All Articles