简体   繁体   English

如何从 SQL Server 2008 中的现有数据库创建 XML 架构

[英]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?有没有办法从 SQL Server 2008、SQL Server Management Studio 中的现有数据库创建 XML 架构?

I have a DB with ~50 tables.我有一个大约有 50 个表的数据库。 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.使用另一个名为 SQL Designer ( https://code.google.com/p/wwwsqldesigner/ ) 的应用程序将为我提供“漂亮”的图片,但我不知道如何创建所需的 XML 模式。

I did a search across the forum (and MS) and couldn't quite find my answer.我在论坛(和 MS)上进行了搜索,但找不到我的答案。 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.我想如果我可以将我的数据库结构转换为 XML,那么 SQL Designer 将为我完成剩下的工作。

Thanks for your assistance.感谢您的帮助。

Nick缺口

If you only need the xml schema of tables query them with this:如果您只需要表的 xml 架构,请使用以下命令查询它们:

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.让您的应用程序使用 ColumnReferencesTableId 和 ColumnReferencesTableColumnId 来获取表关系。 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.如果您更喜欢写出它们的名称,您还可以进一步加入引用的列和表,但我认为它们的 ID 就足够了。

Combined with a cursor running through INFORMATION_SCHEMA or sysobjects, the following should help you:结合通过 INFORMATION_SCHEMA 或 sysobjects 运行的游标,以下内容应该对您有所帮助:

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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