2012-01-24 191 views
0

当前,我们的本地构建包括一个数据库构建&部署步骤,根据Sql Server 2008数据库项目中包含的数据库模型将数据库部署到开发人员的本地计算机。这适用于部署数据库模式,但无法部署任何已定义的数据库图。是否存在创建sysdiagrams表并支持存储过程的存储过程?

为了部署这些图表,我们现在必须包括sysdiagrams表的CREATE脚本和设计器所依赖的存储过程,以及作为部署后部分的图定义的数据插入脚本阶段。

是否有更好的方式来部署sysdiagrams表和存储过程?这些是在sql server management studio中创建的,当在对话框中单击yes时,询问我是否想要创建用于图表的支持对象 - 这是否会调用我可以挂接的内容?我无法找到相关的存储过程。

回答

1

有没有这样做的手段,你应该记住,SQL Server的版本之间的数据库图格式不同,因此SQL2008图不能直接插入到SQL 2000数据库中。该图存储为数据库中的二进制文件。

你可以继续做你现在正在做的事情或者在this link的工具上有一个lokk。

注:我没有连接到网站,所以不能担保,但它可能会给你一些想法。

+0

可以保证。伟大的工具。链接已损坏,请在[我的答案]中复制源代码(http://stackoverflow.com/a/22316032/224976) –

+0

此处更新了SQL2012的ConceptDevelopment代码:https://github.com/neutmute/ScriptDiagram – fiat

3

有一个我用来导入和导出SQL图表的工具。 2008年的版本是Craig Dunn的Tool_ScriptDiagram2008。看看http://www.conceptdevelopment.net/Database/ScriptDiagram2008/。我使用它将图表导出为可以保存在源代码控制中的文本格式,然后可以使用源代码控制中的这些脚本在任何机器上重新创建图表。

+0

由于链接关闭,我张贴我的来源[另一个答案](http://stackoverflow.com/a/22316032/224976) –

1

没有这样的程序。如果你想保持你的图表安全的地方 - 脚本表sysdiagrams和支持对象,如果有数据并保留生成的脚本。就这样。

0

我较小的修改,可以创建sysdiagrams表(original source)的阱中使用存储的过程:

/** 

<summary> 

    Script Sql Server 2008 diagrams (inspired by usp_ScriptDatabaseDiagrams for Sql Server 2000 by Clay Beatty, 
    and Tool_ScriptDiagram2005 by yours truly) 

</summary> 

<example> 

    USE [YourDatabaseName] 
    EXEC ScriptDiagram2008 'DiagramName' 

</example> 

<author>Craig Dunn</author> 

<remarks> 

    Helpful Articles 
    ---------------- 

    1) Upload/Download to Sql 2005 
     http://staceyw.spaces.live.com/blog/cns!F4A38E96E598161E!404.entry 

    2) MSDN: Using Large-Value Data Types 
     http://msdn2.microsoft.com/en-us/library/ms178158.aspx 

    3) "original" Script, Save, Export SQL 2000 Database Diagrams 
     http://www.thescripts.com/forum/thread81534.html 

    <![CDATA[ http://groups-beta.google.com/group/comp.databases.ms-sqlserver/browse_frm/thread/ca9a9229d06a56f9?dq=&hl=en&lr=&ie=UTF-8&oe=UTF-8&prev=/groups%3Fdq%3D%26num%3D25%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26group%3Dcomp.databases.ms-sqlserver%26start%3D25 ]]> 

    4) SQL2008 'undocumented' sys.fn_varbintohexstr 
     http://www.sqlservercentral.com/Forums/Topic664234-1496-1.aspx 

</remarks> 

<param name="name">Name of the diagram in the Sql Server database instance</param> 

*/ 

CREATE PROCEDURE [dbo].[ScriptDiagram2008] 
(
    @DiagramName VARCHAR(128) 
) 
AS 
BEGIN 
    DECLARE @diagram_id INT 
    DECLARE @index  INT 
    DECLARE @size  INT 
    DECLARE @chunk  INT 
    DECLARE @line  VARCHAR(max) 

    -- Set start index, and chunk 'constant' value 
    SET @index = 1 -- 
    SET @chunk = 32 -- values that work: 2, 6 
        -- values that fail: 15,16, 64 

    -- Get PK diagram_id using the diagram's name (which is what the user is familiar with) 
    SELECT 
     @diagram_id = diagram_id, 
     @size = DATALENGTH(definition) 
    FROM sysdiagrams 
    WHERE [name] = @DiagramName 

    IF @diagram_id IS NULL 
    BEGIN 
     PRINT '/**<error>' 
     PRINT 'Diagram name [' + @DiagramName + '] could not be found.' 
     PRINT '</error>*/' 
    END 
    ELSE -- Diagram exists 
    BEGIN 
     -- Now with the diagram_id, do all the work 
     PRINT '/**' 
     PRINT '<summary>' 
     PRINT 'Restore diagram ''' + @DiagramName + '''' 
     PRINT '</summary>' 
     PRINT '<remarks>' 
     PRINT 'Generated by ScriptDiagram2008' 
     PRINT 'Will attempt to create [sysdiagrams] table if it doesn''t already exist' 
     PRINT '</remarks>' 
     PRINT '<generated>' + LEFT(CONVERT(VARCHAR(23), GETDATE(), 121), 16) + '</generated>' 
     PRINT '*/' 
     PRINT 'PRINT ''=== ScriptDiagram2008 restore diagram [' + @DiagramName + '] ===''' 
     PRINT ' -- If the sysdiagrams table has not been created in this database, create it! 
       IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ''sysdiagrams'') 
       BEGIN 
        -- Create table script generated by Sql Server Management Studio 
        -- _Assume_ this is roughly equivalent to what Sql Server/Management Studio 
        -- creates the first time you add a diagram to a 2008 database 
        CREATE TABLE [dbo].[sysdiagrams](
         [name] [sysname] NOT NULL, 
         [principal_id] [int] NOT NULL, 
         [diagram_id] [int] IDENTITY(1,1) NOT NULL, 
         [version] [int] NULL, 
         [definition] [varbinary](max) NULL, 
        PRIMARY KEY CLUSTERED 
        (
         [diagram_id] ASC 
        )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) , 

        CONSTRAINT [UK_principal_name] UNIQUE NONCLUSTERED 
        (
         [principal_id] ASC, 
         [name] ASC 
        )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) 
        ) 

        EXEC sys.sp_addextendedproperty @name=N''microsoft_database_tools_support'', @value=1 , @level0type=N''SCHEMA'',@level0name=N''dbo'', @level1type=N''TABLE'',@level1name=N''sysdiagrams'' 
        PRINT ''[sysdiagrams] table was created as it did not already exist'' 
       END 

       -- Target table will now exist, if it didn''t before' 

     PRINT 'SET NOCOUNT ON -- Hide (1 row affected) messages' 
     PRINT 'DECLARE @newid INT' 
     PRINT '' 
     PRINT 'PRINT ''Create row for new diagram''' 

     -- Output the INSERT that _creates_ the diagram record, with a non-NULL [definition], 
     -- important because .WRITE *cannot* be called against a NULL value (in the WHILE loop) 
     -- so we insert 0x so that .WRITE has 'something' to append to... 

     PRINT 'BEGIN TRY' 
     PRINT ' PRINT ''Write diagram ' + @DiagramName + ' into new row (and get [diagram_id])''' 
     SELECT @line = 
       ' INSERT INTO sysdiagrams ([name], [principal_id], [version], [definition])' 
      + ' VALUES (''' + [name] + ''', '+ CAST (principal_id AS VARCHAR(100))+', '+CAST (version AS VARCHAR(100))+', 0x)' 

     FROM sysdiagrams WHERE diagram_id = @diagram_id 

     PRINT @line 
     PRINT ' SET @newid = SCOPE_IDENTITY()' 
     PRINT 'END TRY' 
     PRINT 'BEGIN CATCH' 
     PRINT ' PRINT ''XxXxX '' + Error_Message() + '' XxXxX''' 
     PRINT ' PRINT ''XxXxX END ScriptDiagram2008 - fix the error before running again XxXxX''' 
     PRINT ' RETURN' 
     PRINT 'END CATCH' 
     PRINT '' 
     PRINT 'PRINT ''Now add all the binary data...''' 
     PRINT 'BEGIN TRY' 

     WHILE @index < @size 
     BEGIN 
      -- Output as many UPDATE statements as required to append all the diagram binary 
      -- data, represented as hexadecimal strings 

      SELECT @line = 
       ' UPDATE sysdiagrams SET [definition] .Write (' 
       + ' ' + UPPER(sys.fn_varbintohexstr (SUBSTRING (definition, @index, @chunk))) 
       + ', null, 0) WHERE diagram_id = @newid -- index:' + CAST(@index AS VARCHAR(100)) 

      FROM sysdiagrams 
      WHERE diagram_id = @diagram_id 

      PRINT @line 
      SET @index = @index + @chunk 
     END 

     PRINT '' 
     PRINT ' PRINT ''=== Finished writing diagram id '' + CAST(@newid AS VARCHAR(100)) + '' ===''' 
     PRINT ' PRINT ''=== Refresh your Databases-[DbName]-Database Diagrams to see the new diagram ===''' 
     PRINT 'END TRY' 
     PRINT 'BEGIN CATCH' 
     PRINT ' -- If we got here, the [definition] updates didn''t complete, so delete the diagram row' 
     PRINT ' -- (and hope it doesn''t fail!)' 
     PRINT ' DELETE FROM sysdiagrams WHERE diagram_id = @newid' 
     PRINT ' PRINT ''XxXxX '' + Error_Message() + '' XxXxX''' 
     PRINT ' PRINT ''XxXxX END ScriptDiagram2008 - fix the error before running again XxXxX''' 
     PRINT ' RETURN' 
     PRINT 'END CATCH' 
    END 
END 

用法为所述DBO。 ScriptDiagram2008(@DiagramName)存储过程:

exec dbo.ScriptDiagram2008('My diagram') 

它会生成一个SQL脚本,您可以保存为图的“便携式”版本。执行该脚本将名为“我的图”的图插入到sysdiagrams表中。它甚至创建该sysdiagrams表,如果它不存在。