简体   繁体   中英

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

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.

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.

Try this (a variation of this answer ):

DECLARE @listStr VARCHAR(MAX)
SELECT @listStr = COALESCE(@listStr+',' ,'') + TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'

EXEC('
  DECLARE @schema xml
  SET @schema = (SELECT TOP 0 * FROM ' + 
     @listStr + 
     ' FOR XML AUTO, ELEMENTS, XMLSCHEMA(''DbSchema''))
  SELECT @schema
     ');

Replace the line:

,**IIF**(ic.object_id IS NOT NULL,1,0) as '@IsPrimaryKey'

With this:

,CASE WHEN ic.object_id IS NOT NULL THEN 1 ELSE 0 END AS '@IsPrimaryKey'
for MS SQL server versions not allowing the IIF function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM