将数据库模式导出到XML文件中

时间:2016-08-11 08:08:38

标签: sql-server xml tsql sql-server-2012

我想将整个数据库架构导出到XML文件。那里需要的信息。

  1. 表格 - 列,数据类型,pk,fk,
  2. views - 返回列
  3. 函数和存储过程 - 参数,返回列和数据类型。
  4. 我在谷歌等地找不到任何东西......有人曾经面对类似的问题吗?

2 个答案:

答案 0 :(得分:1)

通过此查询,您可以获取表格和视图:

SELECT TABLE_NAME AS '@Name', CASE WHEN TABLE_TYPE = 'BASE TABLE' THEN 'Table' ELSE 'View' END AS '@Type',
(
    SELECT Column_Name as '@Name',
            DATA_TYPE as '@DataType',
            case data_type 
                when 'nvarchar' 
                then CHARACTER_MAXIMUM_LENGTH 
                when 'varchar'  
                then CHARACTER_MAXIMUM_LENGTH
                else null 
            end  as '@Length',
            IS_NULLABLE AS '@IsNullable',
            COLUMNPROPERTY(OBJECT_ID(TABLE_NAME), COLUMN_NAME, 'IsIdentity') AS '@IsIdentity',

            (SELECT tc.CONSTRAINT_TYPE FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE cu ON tc.CONSTRAINT_NAME = cu.CONSTRAINT_NAME
WHERE tc.TABLE_NAME = INFORMATION_SCHEMA.COLUMNS.TABLE_NAME AND cu.COLUMN_NAME = INFORMATION_SCHEMA.COLUMNS.Column_Name) AS '@Constraint'

    FROM INFORMATION_SCHEMA.COLUMNS 
    where INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = 
        INFORMATION_SCHEMA.TABLES.TABLE_NAME
    order by INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION
    For XML PATH ('Column'), type
)

FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='dbo'
ORDER BY TABLE_NAME ASC  
For XML PATH ('Table'),Root('Tables')

这适用于sps和fns。

 SELECT
      SPECIFIC_SCHEMA     AS '@ObjectSchema'
      ,ROUTINE_NAME       AS '@ObjectName'
      ,ROUTINE_TYPE       AS '@ObjectType'
      ,ROUTINE_DEFINITION AS '@TEXT'
FROM  INFORMATION_SCHEMA.ROUTINES 
WHERE   (ROUTINE_TYPE = 'function' OR ROUTINE_TYPE = 'procedure')
For XML PATH ('Object') , TYPE, ROOT('Objects') 
希望这会对你有所帮助。

答案 1 :(得分:0)

您可以在Visual Studio中使用实体数据建模

使用XML文本编辑器打开创建的.edmx文件,或者您喜欢或需要的ewer。 好的方面是你可以从.edxm文件(在Visual Studio中)生成数据库模型。