Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create XML schema from an existing database in SQL Server 2008

Is there a way to create an XML schema from an existing database in SQL Server 2008, SQL Server Management Studio?

I have a DB with ~50 tables. I'm looking to create a "nice" diagram showing the relationship between those tables. Using another application called SQL Designer (https://code.google.com/p/wwwsqldesigner/) will give me the "nice" looking picture, but I don't know how to create the XML schema required.

I did a search across the forum (and MS) and couldn't quite find my answer. I could find tools which created a database, but I'm looking at the reverse... I need a pretty picture which shows the database in diagrammatic form. I thought if I could get my db structure into XML then SQL Designer will do the rest for me.

Thanks for your assistance.

Nick

like image 373
Nickster Avatar asked Jul 08 '13 06:07

Nickster


People also ask

How can a XML file be created from a database?

Here we are going to create an XML file from Database. Make an SQL connection to the Database and execute the sql and store the data in a Datset. Call Dataset's WriteXml() method and pass the file name as argument. You have to pass necessary database connection information to connection string.


2 Answers

If you only need the xml schema of tables query them with this:

select top 0 * FROM daTable FOR XML AUTO,XMLSCHEMA

If you need the table names and columns in order to create a representation of your database and how tables are connected you can use something like this:

SELECT
s.name as '@Schema'
,t.name as '@Name'
,t.object_id as '@Id'
,(
    SELECT c.name as '@Name'
    ,c.column_id as '@Id'
    ,IIF(ic.object_id IS NOT NULL,1,0) as '@IsPrimaryKey'
    ,fkc.referenced_object_id as '@ColumnReferencesTableId'
    ,fkc.referenced_column_id as '@ColumnReferencesTableColumnId'
    FROM sys.columns as c
    LEFT OUTER JOIN sys.index_columns as ic
        ON c.object_id = ic.object_id
        AND c.column_id = ic.column_id
        AND ic.index_id = 1
    LEFT OUTER JOIN sys.foreign_key_columns as fkc
        ON c.object_id = fkc.parent_object_id
        AND c.column_id = fkc.parent_column_id
    WHERE c.object_id = t.object_id
    FOR XML PATH ('Column'),TYPE
)
FROM sys.schemas as s
INNER JOIN sys.tables as t
    ON s.schema_id = t.schema_id
FOR XML PATH('Table'),ROOT('Tables')

Let your application use the ColumnReferencesTableId and ColumnReferencesTableColumnId to get table relations. You could also further join back to columns and tables which are referenced if you prefer writing their names out but I thought their Ids would suffice.

like image 134
David Söderlund Avatar answered Sep 29 '22 10:09

David Söderlund


Combined with a cursor running through INFORMATION_SCHEMA or sysobjects, the following should help you:

SELECT * FROM [MyTable] FOR XML AUTO, XMLSCHEMA

I'm uncertain as to whether you can simply apply this to a whole database, or what postprocessing effort would be required to combine all the various table schemas, but it's something to work with.

like image 23
Zec Avatar answered Sep 29 '22 10:09

Zec