Sql-server – How i can create backup script from diagram in SQL Server

sql serversql-server-2008-r2sql-server-2012

I use SQL Server 2012 and SQL Server 2008 R2.

I create a script from all object (tables / trigger / stored procedure / function …) in my database.

I generated this script from SQL Server Management Studio.

I can recreate my database with this scrips on the other server.

But I miss all diagrams of my database after run my script for create another database.

Therefore, I need create backup script from all diagrams that exist in my database.

I need execute this script on the destination database for recreating all my diagrams.

I found this Link. but i need some thinks that create all script (Insert Command) automatically.

Best Answer

I write this query and this work.

USE DestinationDatabase

DELETE  sysDiagrams
WHERE   name IN ( SELECT    name
              FROM      SourceDatabase.dbo.sysDiagrams )

SET IDENTITY_INSERT sysDiagrams ON

INSERT  sysDiagrams
    ( name ,
      principal_id ,
      diagram_id ,
      version ,
      definition
    )
    SELECT  name ,
            principal_id ,
            diagram_id ,
            version ,
            definition
    FROM    SourceDatabase.dbo.sysDiagrams

SET IDENTITY_INSERT sysDiagrams OFF