T-SQL - 是否有(免费)方法来比较两个表中的数据?

发布于 2024-09-05 05:49:26 字数 1344 浏览 3 评论 0原文

我有表a表b。 (SQL Server 2008)

两个表具有完全相同的架构。

出于此问题的目的,请考虑 table a = 我的本地开发表,table b = 实时表。

我需要创建一个 SQL 脚本(包含 UPDATE/DELETE/INSERT 语句),将表 b 更新为与表 a 相同。然后该脚本将被部署到实时数据库。

有什么免费工具可以做到这一点,或者更好的是我可以自己做这件事?

我想我可能需要对表中的所有字段进行某种类型的联接,然后基于此生成动态 SQL。

有人有什么想法吗?

编辑

好的,我想稍微澄清一下这个问题。

我需要同步的表是一个简单的查找表。数据非常简单明了。

TABLE A 可能如下所示: 这

IdFoo          Activity      IsFoo
1              Foo           1
2              Bar           0

TABLE B 可能如下所示的一个想法:

IdFoo          Activity      IsFoo
1              Foo           1
2              Bar           1

基本上,我想做的就是更新该 BIT TABLE B 中的 列 (IsFoo) 来匹配 TABLE A 中同一 IdFoo 的相应值。

请记住:

  • TABLE A 在我的本地计算机上
  • TABLE B 在实时服务器上

显然我有一个TABLE B 的(可靠)备份在我需要编写脚本的本地计算机上,然后在实时服务器上运行脚本。

该表还具有引用完整性(我没有显示的其他列)。这意味着我不能只删除 TABLE B 中的所有内容并从 TABLE A 中插入。

这个脚本将是一次性的。因此,无需执行链接服务器、复制等操作。感谢各位的回答。 =)

编辑:

好的 - 所以我已经接受了 Oleg 的回答(VS2010 数据比较)。快速、简单,而且很有魅力。

并不是说其他​​答案都是错误的。我感谢所有的答案!

I have table a and table b. (SQL Server 2008)

Both tables have the exact same schema.

For the purposes of this question, consider table a = my local dev table, table b = the live table.

I need to create a SQL script (containing UPDATE/DELETE/INSERT statements) that will update table b to be the same as table a. This script will then be deployed to the live database.

Any free tools out there that can do this, or better yet a way I can do it myself?

I'm thinking I probably need to do some type of a join on all the fields in the tables, then generate dynamic SQL based on that.

Anyone have any ideas?

EDIT

Okay, thought I'd clarify this question a little.

The table I need to synchronize is a simple look-up table. The data is very simple and straightforward.

Here's an idea of what TABLE A might look like:

IdFoo          Activity      IsFoo
1              Foo           1
2              Bar           0

Here's an idea of what TABLE B might look like:

IdFoo          Activity      IsFoo
1              Foo           1
2              Bar           1

Basically, all I want to do is update that BIT column (IsFoo) in TABLE B to match the corresponding value in TABLE A for the same IdFoo.

Keep in mind:

  • TABLE A is on my local machine
  • TABLE B is on the live server

Obviously I have a (reliable) backup of TABLE B on my local machine which i need to script against, then run the script on the live server.

The table also has referential integrity (other columns I didn't show). Which means I can't just delete everything in TABLE B and do an insert from TABLE A.

This script will be a once off. So no need to do things like linked server, replication, etc. Appreciate the answers though guys. =)

EDIT:

Ok - so I've gone with Oleg's answer (VS2010 Data Compare). Quick, easy, and works a charm.

Not to say the other answers are incorrect. I appreciate all the answers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

极度宠爱 2024-09-12 05:49:26

迟到的答案,但对线程访问者可能有用

除了其他提到的解决方案之外,我可以建议尝试 ApexSQL Data Diff。它可以比较和同步 SQL Server 数据库数据(实时数据库和备份中),并自动执行和安排数据迁移。它也适用于大型数据库,并且能够直接从 SSMS 执行比较。

您可以免费下载该工具并使用它。它提供功能齐全的免费试用版,并提供适用于 SQL Express 和 Azure SQL 数据库的社区版(试用期结束后)。

要了解有关该工具的更多信息,您可以访问http://www.apexsql.com/sql_tools_datadiff.aspx

Late answer but can be useful to the thread visitors

Besides other mentioned solutions, I can suggest trying ApexSQL Data Diff. It can compare and sync SQL Server database data (in live databases as well as backups), and automate and schedule data migrations. It works with huge databases also, and is able to perform comparisons directly from SSMS.

You can download this tool for free and play with it. It features a fully functional free trial, and offers a community edition (after the trial period is over) that works on SQL Express and Azure SQL Database.

To learn more about the tool, you may visit http://www.apexsql.com/sql_tools_datadiff.aspx

攒一口袋星星 2024-09-12 05:49:26

在非常简单的情况下,您将能够在本地 SQL Server 中定义一个新的链接服务器(请参阅 Microsoft SQL Server Management Studio 中的“服务器对象”/“链接服务器”)并使用 INNER JOINOUTER JOIN 找出 A 和 B 表之间的差异。

在真实且更复杂的情况下,您应该考虑目标数据库中存在的引用完整性、不同的外键和身份(自动增量)字段,因此更新脚本将更加复杂。因此,我建议您不要花时间创建开发人员和生产数据库的同步,而是使用标准工具。例如,我使用 Visual Studio Team Edition 2008(或数据库版本)或 Visual Studio 2010 Ultimate 版本的功能来比较两个数据库中的数据。它运作得很好。

In very simple cases you will be able to define in your local SQL Server a new Linked Server (see in Microsoft SQL Server Management Studio under "Server Objects" / "Linked Server") and use INNER JOIN and OUTER JOIN to find out differences between A and B tables.

In a real and more complex situation you should take into consideration Referential Integrity, different Foreign Keys, and Identity (auto incremental) fields existing in the destination database, so the update script will be more complex. So I'll recommend you don't spend your time in creating synchronization of your developer and production DB and use a standard tool instead. I use for example the features of Visual Studio Team Edition 2008 (or Database edition) or Visual Studio 2010 Ultimate edition to compare data in two databases. It works very well.

沫离伤花 2024-09-12 05:49:26

如果您只想同步表而不关心提前查看更改,则 MERGE 命令可以做到这一点。

MSDN - MERGE (Transact-SQL)

(免费)Microsoft SSDT 还内置了数据比较和同步功能,尽管它比 Redgate 数据比较等付费工具受到更多限制。

If you just want to synchronise the tables and don't care about reviewing the changes in advance the MERGE command can do this.

MSDN - MERGE (Transact-SQL)

The (free) Microsoft SSDT also has data compare and synchronization built in though it is more limited than paid tools such as Redgate data compare.

意犹 2024-09-12 05:49:26

RedGate 有 SQL Data Compare(尽管不是免费的),并且有也是 SMO内置功能。

最后,维基百科有一个相当全面的软件列表。

There is SQL Data Compare from RedGate (though not free) and there is also SMO and the built in functionallity.

Finally Wikipedia has quite a comprehensive list of software.

浅浅淡淡 2024-09-12 05:49:26

由于它是一次性的,您可以使用此查询来查找这两个表中不同的行:

(SELECT * FROM TABLE_A
 MINUS
 SELECT * FROM TABLE_B)

 UNION ALL

(SELECT * FROM TABLE_B
 MINUS
 SELECT * FROM TABLE_A)

MINUS 将逐个字段比较记录,然后它将丢弃第一个表中相同的记录记录在第二个表中。其工作原理如下:

  • 第一个 MINUS 获取 TABLE_A 中不在 TABLE_B 中的所有记录
  • 第二个 MINUS 获取所有记录从不在 TABLE_A 中的 TABLE_B
  • 联合获取两个表中其他表中没有匹配记录的所有记录。

现在您可以将这些记录插入到某个临时表中,然后进行插入/更新。

根据您的需要,您可以限制用于比较的字段列表。

请注意,您需要主键才能正常工作。

编辑:
哎呀。 SQL Server 不支持MINUS 运算符。去年半我在 ORACLE 工作,所以这是自动的。

您可以使用 EXCEPT 运算符代替。请参阅这篇文章:EXCEPT 和 INTERSECT ( Transact-SQL)

编辑 2:

Re scherand 的评论:
如果他确实无法从本地计算机连接到实时服务器,那么他可以简单地转储 TABLE_A 并将其加载到服务器上。无论哪种方式,目标都是更改实时服务器上的表数据。

Since it is one off, you can use this query to find rows that are different in this two tables:

(SELECT * FROM TABLE_A
 MINUS
 SELECT * FROM TABLE_B)

 UNION ALL

(SELECT * FROM TABLE_B
 MINUS
 SELECT * FROM TABLE_A)

MINUS will compare records field by field, then it will discard records from first table for which there is identical record in second table. This works like this:

  • First MINUS gets all records from TABLE_A that are not in TABLE_B
  • Second MINUS gets all records from TABLE_B that are not in TABLE_A
  • Union gets all records from both tables for which there is no matching record in other table.

Now you can insert those records in some temp table and then do inserts/updates.

Depending on your needs, you can restrict field list for comparing.

Note that you need primary key for this to work.

Edit:
Ooops. SQL Server does not support MINUS operator. I'm working with ORACLE last year and half, so this was automatic.

You can use EXCEPT operator instead. See this article: EXCEPT and INTERSECT (Transact-SQL)

Edit 2:

Re scherand's comment:
If he really cannot connect from local machine to live server, than he can simply dump TABLE_A and load it on server. One way or the other, goal is to change table data on live server.

凝望流年 2024-09-12 05:49:26

我遇到了与您相同的问题 - 搜索了一个比较两个 MS SQL 表中的数据的免费工具 - 但什么也没找到。然后我创建了一个简单的免费软件命令行实用程序。它比较两个表中的数据,并创建 INSERT/DELETE/UPDATE 语句以使目标表数据与源表数据相同。现在我用它进行数据比较,由于它是完全免费的,建议您在以下位置查看:Sourceforge。网-UltraDBC

I ran in the same problem as you - searched for a free tool that compares data from two MS SQL tables - and found nothing. Then I created a simple freeware command line utility. It compares data from two tables, and creates INSERT/DELETE/UPDATE statemenets to make destination table data the same as source. Now I am using it for data comparison, and as it is completely free, can recommend to check it out at: Sourceforge.net - UltraDBC

梦萦几度 2024-09-12 05:49:26

您可以使用数据脚本生成器来创建插入脚本,然后使用文件比较工具(例如 WinMerge)来比较文件以查找差异。有一篇关于在代码项目上生成数据脚本的文章:http://www.codeproject。 com/KB/database/sqlinsertupdategenerator.aspx

You could use a data script generator that creates script for the inserts, and then use a file comparison tools such as WinMerge to compare the files to find the differences. There's an article on generating the data scripts on code project: http://www.codeproject.com/KB/database/sqlinsertupdategenerator.aspx

记忆消瘦 2024-09-12 05:49:26

Tablediff 可以工作 - 它是免费的并且附带 SQL Server
http://msdn.microsoft.com/en-us/library/ms162843.aspx

Tablediff would work - it's free and comes with SQL Server
http://msdn.microsoft.com/en-us/library/ms162843.aspx

丢了幸福的猪 2024-09-12 05:49:26

您还可以尝试使用 SQL Server 2008 提供的导入和导出数据。这是将所有数据从任何地方复制到任何地方的相当直接的方法。我做了同样的事情并且工作完美。

You can also try using the import and export data provided by SQL Server 2008. Its is fairly straight way to copy all the data from anywhere to anywhere. I do the same thing and works perfectly.

_畞蕅 2024-09-12 05:49:26

您可以尝试我们的SQL Server 架构比较

这个工具不是免费的(它是共享软件) ),但您可以免费使用 30 天试用版,并且您还有机会获得该产品的免费许可证 - 请参阅我们的 免费许可条件页面

You might try our Schema Compare for SQL Server

This tool is not free (it's shareware), but you can use 30-days trial for free and also, you have an opportunity to get a free license for this product - please refer to our free license conditions page.

茶底世界 2024-09-12 05:49:26

如果您

  1. 将 devTableA 复制到 prod,
  2. 暂停对 prodTableB 的依赖关系
  3. ,将 prodTable B 重命名为 prodTableB1,
  4. 将 devTableA 重命名为 prodTableB
  5. 重新实现对新 prodTableB 的依赖关系,
  6. 测试新 prodTableB,然后删除已命名的旧 prodTableB

,会发生什么情况?

或者如果您必须更加尊重现有的 prodTableB 以及 dev 和 prod 中的身份列值匹配(即使这些值不连续)...

declare @iLast int, @x int, @y int
select @iLast = (select MAX(id) from prodTableB)
set @x = 1
while @x <= @iLast
begin
  select @y = (select COUNT(*) from prodTableB where id = @x)
  if @y = 1
    begin
      update prodTableB set isFoo = (select isFoo from devTableA where id=@x
    end
  @x=@x+1
end

what happens if you:

  1. copy devTableA onto prod,
  2. suspend dependencies on prodTableB
  3. rename prodTable B to prodTableB1,
  4. rename devTableA to prodTableB
  5. reimplement dependencies on new prodTableB
  6. test new prodTableB and then delete out-named old prodTableB

?

or if you have to be more respectful of the existing prodTableB and your identity column values in both dev and prod match (even if the values are not continuous)...

declare @iLast int, @x int, @y int
select @iLast = (select MAX(id) from prodTableB)
set @x = 1
while @x <= @iLast
begin
  select @y = (select COUNT(*) from prodTableB where id = @x)
  if @y = 1
    begin
      update prodTableB set isFoo = (select isFoo from devTableA where id=@x
    end
  @x=@x+1
end
你怎么这么可爱啊 2024-09-12 05:49:26

您还可以查看xSQL 数据比较。 SQL Express 版本是免费的,还有一个 Lite 版本可以满足小型数据库的需要。

一个很好的免费工具也是来自 SSDT 的数据比较。

免责声明:我隶属于 xSQL。

You can also have a look at xSQL Data Compare. The SQL Express version is free and there is also a Lite version that will do the trick for small databases.

A good free tool is also data compare from SSDT.

Disclaimer: I'm affiliated with xSQL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文