比较跨多个数据库的存储过程 (SQL Server)
SQL Gurus——
我们的架构由多个客户数据库和一个公共代码库组成。当我们部署数据库更改时,必须针对每个数据库运行脚本。
由于部署问题,我们的存储过程有时会彼此不同步。我想创建一个脚本来返回这些不匹配的过程,以确保我们在部署后拥有数据库的同步副本。
是否可以通过让脚本查看两个数据库之间的所有过程来比较两个或多个数据库,并返回不匹配的情况?
其效果是:
DATABASE_1 | DATABASE_2 | MISMATCHED_PROCEDURE | DATABASE_1_MODIFY_DATE | DATABASE_2_MODIFY_DATE
Customer_1 | Customer_2 | sp_get_names | 1/1/2010 | 1/2/2010
Customer_1 | Customer_2 | sp_add_person | 1/5/2010 | 1/6/2010
作为奖励,是否可以通过将最新脚本应用到过时脚本来让脚本自动同步数据库?
非常感谢!
SQL Gurus --
Our architecture consists of multiple customer databases to a common codebase. When we deploy database changes, the scripts must be run agianst each database.
Because of deployment issues, there have come times when our stored procedures became out of sync with one another. I would like to create a script to return these mimatched procedures to ensure that we have sync'd copies of our databases after deployment.
Is it possible to compare two or more databases, by having a script look at all the procedures between two databases, and return the mismatches?
Something to the effect of:
DATABASE_1 | DATABASE_2 | MISMATCHED_PROCEDURE | DATABASE_1_MODIFY_DATE | DATABASE_2_MODIFY_DATE
Customer_1 | Customer_2 | sp_get_names | 1/1/2010 | 1/2/2010
Customer_1 | Customer_2 | sp_add_person | 1/5/2010 | 1/6/2010
As a bonus, would it be possible to have the script automatically sync the databases by applying the newest script to the out-of-date script?
Much Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
有很多工具可以做到这一点。最好的之一是 Red-Gate SQL Compare。另一个非常好的选择是使用 Visual Studio Database Professional 来管理数据库架构。除其他外,它将进行非常好的模式比较。
There are many tools to do this. One of the best is Red-Gate SQL Compare. Another very good alternative is to use Visual Studio Database Professional to manage your database schema. Among other things, it will do very nice schema compares.
您可以使用下面的脚本来识别哪些过程(以及稍加修改的其他对象)不同。
要同步数据库,您可能需要尝试 ApexSQL Diff。它类似于 Red Gate 的 SQL Compare。
You can identify which procedures (and other objects with slight modification) are different using the script below.
To synchronize databases you might want to try ApexSQL Diff. It’s similar to SQL Compare from Red Gate.
如果您没有适用于数据库架构师 (Data Dude) 的 SQL Compare 或 Visual Studio 团队系统...可以尝试一下...SQL 2005 及更高版本
If you don't have SQL Compare or Visual Studio team system for DB architects (Data Dude)...play around with this...SQL 2005 and up
使用以下内容:
Use the following:
Red Gate 的 Sql Compare 是完美的解决方案。
但是,如果您负担不起费用,那么有一个非常好的免费软件:Star Inix 的 Sql Compare http://www.starinix.com/sqlcompare02.htm
The Red Gate's Sql Compare is the perfect solution.
However, if you can't afford its cost there is a very nice software that is free: Star Inix's Sql Compare http://www.starinix.com/sqlcompare02.htm
简单的答案,但在所有过程上放置并创建脚本将非常简单且有效。
Simplistic answer but a drop and create script on all procedures would be very easy and effective.
是的,RedGate 的东西很棒,但这就是我为比较两个不同数据库中的存储过程所做的工作:
完毕!
Yes, the RedGate stuff is great, but that is what I have done to compare the stored procedures in two different databases:
Done!
这是切线相关的,但我写了一些提供两个存储过程的文本之间的百分比匹配统计信息的内容: http://www.sqlservercentral.com/scripts/T-SQL/65787/
This is tangentially related, but I wrote something that provides percentage matching statistic between the text of two stored procedures: http://www.sqlservercentral.com/scripts/T-SQL/65787/
如果要比较两个数据库中的所有存储过程以及另一个数据库中不存在的存储过程的输出名称,请使用以下命令。请注意,这不会检查存储过程的定义,仅检查其名称,但也有一种方法可以检查它。
If you want to compare all the stored procedures from both databases and output names of those that do not exist in the other, then use the following. Note this does not check the definition of the stored procedures only its name, but there is a way to check for that too.
没有足够的声誉来发表评论,所以这里有一个新的答案给你。我使用了 John Moore 提供的代码,但添加了
和 C1.colid = C2.colid
来比较跨越多个条目的过程。这是完整的代码:Not enough reputation to comment, so here's a new answer for you. I used the code provided by John Moore, but added
and C1.colid = C2.colid
to compare procedures that span multiple entries. Here's the full code:下面的Procedure可以找出两个不同数据库中Functions、Procedures、Triggers的差异。将数据库名称作为参数传递。
The following Procedure can find out the differences of Functions, Procedures, Triggers in two different databases. Pass the database names as parameters.