我的应用程序的数据库包含我的应用程序运行所必需的表(用户不可编辑)。例如,有一个报告表,其中包含我的 SSRS 报告列表。
除了自动增量和 GUID 字段,我的报告表中的数据应该在所有数据库中匹配。
为了使现有的客户端数据库与从头创建的数据库保持同步,有一个数据库更新程序应用程序可以运行脚本来更新现有的客户端群。
有单元测试可确保报告在两种类型的数据库上正确运行。但是,除了开发人员的眼睛,没有系统检查来确保这些行中的行和值在表之间匹配。这很容易出现人为错误。
为了解决这个问题,我计划在单元测试报告中添加一个小报告,该报告将为以下内容的开发提供信息:
- “更新”数据库中存在的“从零开始”数据库中缺失的记录
- “Made From Scratch”数据库中存在的“Updated”数据库中缺失的记录
- 表之间不匹配的字段
到目前为止,我有一个查询来报告所有涉及的表的上述信息。
示例查询如下所示:
--Take the fields I want to compare from TableToCompare in MadeFromScratch and put them in @First_Table_Var
--NOTE: MyFirstField should match in both tables in order to compare the values between rows
DECLARE @First_Table_Var table(
MyFirstField Varchar(255),
MySecondField VarChar(255),
MyThirdField Varchar(255),
);
INSERT INTO @First_Table_Var
SELECT
r.MyFirstField,
r.MySecondField,
l.MyThirdField
FROM
MadeFromScratch.dbo.TableToCompare r
INNER JOIN MadeFromScratch.dbo.LookUpTable l ON r.ForeignKeyID = l.PrimaryKeyID
--Take the fields I want to compare from TableToCompare in UpdatdDatabase and put them in @Second_Table_Var
DECLARE @Second_Table_Var table(
MyFirstField Varchar(255),
MySecondField VarChar(255),
MyThirdField Varchar(255),
);
INSERT INTO @Second_Table_Var
SELECT
r.MyFirstField,
r.MySecondField,
l.MyThirdField
FROM
UpdatdDatabase.dbo.TableToCompare r
INNER JOIN UpdatdDatabase.dbo.LookUpTable l ON r.ForeignKeyID = l.PrimaryKeyID
--**********************
-- CREATE OUTPUT
--**********************
--List Rows that exist in @Second_Table but not @First_Table
--(e.g. these rows need to be added to the table in MadeFromScratch)
SELECT
Problem = '1 MISSING ROW IN A MADE-FROM-SCRATCH DATABASE',
hur.MyFirstField,
hur.MySecondField,
hur.MyThirdField
FROM
@Second_Table_Var hur
WHERE
NOT EXISTS
(SELECT
*
FROM
@First_Table_Var hu
WHERE
hu.MyFirstField = hur.MyFirstField
)
UNION
--List Rows that exist in @First_Table but not @Second_Table
--(e.g. these rows need to be added to the table in UpdatdDatabase)
SELECT
Problem = '2 MISSING IN UPDATE DATABASE',
hur.MyFirstField,
hur.MySecondField,
hur.MyThirdField
FROM
@First_Table_Var hur
WHERE
NOT EXISTS
(SELECT
*
FROM
@Second_Table_Var hu
WHERE
hu.MySecondField = hur.MySecondField
)
UNION
--Compare fields among the tables where MyFirstField matches, but
SELECT
Problem = '3 MISMATCHED FIELD',
h.MyFirstField,
MySecondField = CASE WHEN h.MySecondField = hu.MySecondField THEN '' ELSE 'Created Value: ' + h.MySecondField + ' Updated Value: ' + hu.MySecondField END,
MyThirdField = CASE WHEN h.MyThirdField = hu.MyThirdField THEN '' ELSE 'Created Value: ' + CAST(h.MyThirdField AS VARCHAR(4)) + ' Updated Value: ' + CAST(hu.MyThirdField AS VARCHAR(4)) END,
FROM
@First_Table_Var h
INNER JOIN @Second_Table_Var hu on h.MyFirstField = hu.MyFirstField
WHERE
NOT EXISTS
(SELECT
*
FROM
@Second_Table_Var hu
WHERE
hu.MyFirstField = h.MyFirstField and
hu.MySecondField = h.MySecondField and
hu.MyThirdField = h.MyThirdField and
)
ORDER BY Problem
我编写代码来解析结果不会有任何问题,但由于以下原因,这种方法感觉过时了:
- 需要编写几个查询(本质上做同样的事情)
- 此过程的维护可能会很麻烦
我希望能够编写一些东西,其中要比较的表和字段列表由某种文件(XML?)维护。因此,无论是添加字段还是更改字段,用户所要做的就是更新此文件。
有没有办法使用 LINQ 和/或反射(或 .NET 4.0 中的任何功能),我可以比较两个数据库之间的表并像上面列出的那样维护它们?
欢迎提出想法。有一个例子的想法会很棒!:D