`
winzenghua
  • 浏览: 1370837 次
  • 性别: Icon_minigender_2
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

SQL Schema Comparison with Visual Studio 2010

阅读更多

SQL Schema Comparison with Visual Studio 2010

By: Arshad Ali -- 8/12/2010

Rating:<!-- Rating BEGIN -->

Problem
Often we need to compare databases across two different environments (or even sometimes in the same environment) to identify the schema object differences, so that one database can be brought in sync with another one. For example, we normally do development on the development box; then during the build preparation we compare the development database with the QA/UAT/Production database to identify differences and generate schema objects for synchronization or incremental deployment scripts. How we can compare schema objects between databases and generate synchronization or incremental deployment scripts?

Solution
Although there are many tools available for schema comparison (some free and some you need to purchase) I am going to discuss the Microsoft Visual Studio Database edition for schema comparison. Microsoft Visual Studio Database edition offers several features for database development, for example you can create an offline representation of a database for database development and version control, Database Unit Testing, Code Analysis, Schema Comparison, Data Comparison etc.

In this demonstration I am going to show you how Schema Comparison works on Visual Studio 2010 Ultimate edition although you can do the same with Visual Studio 2005/2008 Database edition too.

Open the Microsoft Visual Studio IDE (Integrated Development Studio) and you will see a "Data" menu in the menu bar depending on the Microsoft Visual Studio edition you have installed (for more details click here). Select Schema Compare under Data menu and then New Schema Comparison as shown below.

You will see a dialog box like this, here you need to specify your source schema and target schema. The source or target could be either a database project (offline representation of your database) or the database itself (for which you need to set up a connection to your SQL Server instance) or a *.dbschema file. You can even save your selection as *.scmp file to use later on.

The moment you click on the OK button, it will start doing the comparison. You will notice a new tool bar, some of the options of this new tool bar are: you can filter out the comparison result as you can see in the image below, you can synchronize your target by writing updates to it (have a look on the note below before doing this), you can export your synchronization/incremental update script to a file or to the editor.

The schema comparison result screen will look like the image shown below. On the top pane there are four columns; Status tells the type of change of each object i.e.. if the object is New, Missing, Equal or has a different definition. The next column shows the name of the object at the source. Third column allows you to specify the kind of action which you want to take during synchronization for example if you want to Skip updating target, update target, drop from target if the object is missing in source, create on target if the object does not exist on the target. And finally the fourth column shows the name of the object at the target.

The next pane shows the object definition of the selected object. It marks the changes in different colors, the meaning of the color coding is available on the bottom of this pane as you can notice in the below screenshot.

The bottom pane shows the target schema update script depending on the selection of "Update Action" of each object. If you are not able to see it or want to refresh, click on "Refresh Update Script" icon on the toolbar.

The Schema Comparison tool allows you to specify the different schema comparison options as well as specify which types of objects are to be compared as shown; make note here by default the Schema Comparison tool ignores extended properties, you can change this default behavior as well in the comparison options:

Note: As long as your source has new objects there is nothing to worried about, but if you have renamed your objects or columns at the source you might incur data loss while updating the target. For example in the above schema comparison result image you can see the EmployeeID column has been renamed to BusinessEntityID, so while updating the target it will drop the EmployeeID column and add the BusinessEntityID column which will have no data even though the EmployeeID column has data in it. So it is recommended to review your deployment scripts and take a backup of your target database before updating the target database. You can change this default behavior of data loss in the Schema Compare Options, as discussed above, and check the "Block schema updates if data loss might occur" under Script generation options and then the generated script will look like this if it would cause any data loss.


/*
The column [HumanResources].[Employee].[ContactID] is being dropped, data loss could occur.
The column [HumanResources].[Employee].[EmployeeID] is being dropped, data loss could occur.
The column [HumanResources].[Employee].[ManagerID] is being dropped, data loss could occur.
The column [HumanResources].[Employee].[Title] is being dropped, data loss could occur.
The column [HumanResources].[Employee].[BusinessEntityID] on table [HumanResources].[Employee] must be added, but the column has no default value and does not allow NULL values. If the table contains data, the ALTER script will not work. To avoid this issue, you must add a default value to the column or mark it as allowing NULL values.
The column [HumanResources].[Employee].[JobTitle] on table [HumanResources].[Employee] must be added, but the column has no default value and does not allow NULL values. If the table contains data, the ALTER script will not work. To avoid this issue, you must add a default value to the column or mark it as allowing NULL values.
*/
IF EXISTS (select top 1 1 from [HumanResources].[Employee]) RAISERROR ('Rows were detected. The schema update is terminating because data loss might occur.', 16, 127) WITH NOWAIT
GO

分享到:
评论

相关推荐

    Visual Studio.NET 2003 BT种子文件

    **Visual Studio .NET 2003:编程开发的强大工具** Visual Studio .NET 2003是由微软公司推出的集成开发环境(IDE),是Visual Studio系列的重要版本之一,专为.NET Framework 1.1设计。它标志着微软在.NET平台上的...

    Visual Studio 2005 Team Edition For Database Professional

    1. **数据库Schema管理**:通过Visual Studio项目的方式来控制数据库模式(schema),使得数据库结构的变化可以被更好地管理和追踪。 2. **单元测试**:支持对数据库内的存储过程、函数进行单元测试,并能够将这些测试...

    Visual Studio 中的各个后缀的意思

    Visual Studio 中的各个后缀的意思 Visual Studio 是一个功能强大的集成开发环境(IDE),它提供了许多功能来帮助开发者快速构建、测试和部署应用程序。其中,文件后缀是开发者需要了解的重要概念之一。本文将详细...

    21天学通Visual Studio.NET 2003-1

    《21天学通Visual Studio.NET 2003》是一本旨在帮助初学者快速掌握Microsoft的开发环境Visual Studio.NET 2003的教程。这本书涵盖了C#、VB(Visual Basic)和VC(Visual C++)等编程语言的基础知识,并结合实际案例...

    基于Visual Studio.net与SQL Server的工资管理系统的设计.pdf

    "基于Visual Studio.net与SQL Server的工资管理系统的设计" 本文详细介绍了基于Visual Studio.net与SQL Server的工资管理系统的设计。该系统的设计目标是建立一个高效、安全、易用的工资管理系统,能够帮助企业提高...

    Visual Studio 2005 简介

    10. **XML工具**:Visual Studio 2005提供了XML编辑器和验证功能,支持XML文档的创建和编辑,以及与XML相关的技术如XSLT和XML Schema。 11. **测试工具**:内置的单元测试和负载测试工具,帮助开发者进行代码质量...

    SQL DataBase schema & data Compare

    在实际应用中,有一些常用的SQL Schema和数据比较工具,如Redgate的SQL Compare和SQL Data Compare,以及Microsoft SQL Server Management Studio(SSMS)自带的比较功能。这些工具极大地简化了数据库管理和维护工作...

    LINQ and XML in Visual Studio 2008.zip

    《LINQ and XML in Visual Studio 2008》这个压缩包文件主要聚焦于微软在Visual Studio 2008中引入的重要技术——Language Integrated Query(语言集成查询,简称LINQ)以及XML的高级应用。在这个专题中,我们将深入...

    Visual Studio 2010 前端开发工具/扩展/插件推荐

    本文将推荐几款有助于改善Visual Studio 2010前端开发体验的工具。 首先,Web Standards Update for Visual Studio 2010 SP1是微软官方出品的一款扩展,旨在增强HTML5、CSS3和JavaScript的编辑支持。它提供了...

    store_schema.sql脚本

    《store_schema.sql脚本详解与应用》 在数据库管理和开发领域,`store_schema.sql`脚本扮演着至关重要的角色。这个脚本通常用于定义数据库的结构,包括表、视图、存储过程、触发器等对象,是构建和维护数据存储系统...

    Visual Studio 2005 Team Edition软件架构系列课程(2):开发面向服务的应用

    **Visual Studio 2005 Team Edition 软件架构系列课程** 在“Visual Studio 2005 Team Edition软件架构系列课程”中,我们深入探讨了如何利用Microsoft的这款强大的开发工具进行面向服务的应用程序设计。这个系列...

    [Visual.Studio..net.2003.简体中文1版].zip

    7. **XML和Web服务**:.NET框架支持XML标准,Visual Studio .NET 2003提供了XML文档编辑器和XML Schema Designer,方便开发者处理XML数据。此外,它还支持创建和使用Web服务,使分布式应用程序的构建变得简单。 8. ...

    sqlalchemy-sqlschema:SQL炼金术SQL模式

    with maintain_schema ( "my_schema" , session ): schema = session . execute ( "show search_path" ). scalar () assert schema == "my_schema" # a rollback still maintains the schema session . rollback...

    common_schema-2.2.sql

    mysql common_schema 完整sql。适合5.1版本以上,支持5.7以下的简单的json内容获取

    在Visual Studio中进行调试时查看SVG和其他HTML5内容

    Visual Studio提供了XML编辑器,支持XML Schema验证和格式化。在调试涉及XML的项目时,可以使用XML视图或数据绑定调试器来检查数据流和转换过程。 总之,在Visual Studio中查看和调试SVG和其他HTML5内容需要对MIME...

    store_schema.sql

    store_schema.sql脚本,oracle的store初始化脚本。

    init-schema.sql

    init-schema.sql init-schema.sql init-schema.sql init-schema.sql init-schema.sql

    oracle创建表,索引,表空间,触发器,schema用户,序列的Sql文

    5. **创建Schema用户**:Schema在Oracle中是逻辑上的数据库对象集合,通常与一个特定的数据库用户关联。使用`CREATE USER`创建用户,`GRANT`权限: ```sql CREATE USER MyUser IDENTIFIED BY password; GRANT ...

Global site tag (gtag.js) - Google Analytics