我有下面的代码

SELECT tA.FieldName As [Field Name],
       COALESCE(tO_A.[desc], tO_B.[desc], tO_C.Name, tA.OldVAlue) AS [Old Value],
       COALESCE(tN_A.[desc], tN_B.[desc], tN_C.Name, tA.NewValue) AS [New Value],
       U.UserName AS [User Name],
       CONVERT(varchar, tA.ChangeDate) AS [Change Date] 
  FROM D tA
       JOIN 
       [DRTS].[dbo].[User] U 
         ON tA.UserID = U.UserID
       LEFT JOIN 
       A tO_A 
         on tA.FieldName = 'AID' 
        AND tA.oldValue = CONVERT(VARCHAR, tO_A.ID)
       LEFT JOIN 
       A tN_A 
         on tA.FieldName = 'AID' 
        AND tA.newValue = CONVERT(VARCHAR, tN_A.ID)
       LEFT JOIN 
       B tO_B 
         on tA.FieldName = 'BID' 
        AND tA.oldValue = CONVERT(VARCHAR, tO_B.ID)
       LEFT JOIN 
       B tN_B 
         on tA.FieldName = 'BID' 
        AND tA.newValue = CONVERT(VARCHAR, tN_B.ID)
       LEFT JOIN 
       C tO_C 
         on tA.FieldName = 'CID' 
        AND tA.oldValue = tO_C.Name
       LEFT JOIN 
       C tN_C 
         on tA.FieldName = 'CID' 
        AND tA.newValue = tN_C.Name
 WHERE U.Fullname = @SearchTerm
ORDER BY tA.ChangeDate

当运行代码时,我在添加表c的两个连接后,将错误粘贴在标题中。我认为这可能与我使用SQL Server 2008的事实有关,并在2005年的机器上恢复了这个db的副本。


当前回答

我也有类似的要求;在这里为任何有类似情况的人记录我的方法……

场景

我有一个数据库从一个干净的安装与正确的排序规则。 我有另一个数据库,它有错误的排序。 我需要更新后者以使用在前者上定义的排序规则。

解决方案

使用SQL Server模式比较(来自SQL Server数据工具/ Visual Studio)来比较源(干净安装)和目标(无效排序规则的db)。

在我的例子中,我直接比较了两个db;虽然你可以通过一个项目,让你手动调整之间的部分…

Run Visual Studio Create a new SQL Server Data Project Click Tools, SQL Server, New Schema Comparison Select the source database Select the target database Click options (⚙) Under Object Types select only those types you're interested in (for me it was only Views and Tables) Under General select: Block on possible data loss Disable & reenable DDL triggers Ignore cryptographic provider file path Ignore File & Log File Path Ignore file size Ignore filegroup placement Ignore full text catalog file path Ignore keyword casing Ignore login SIDs Ignore quoted identifiers Ignore route lifetime Ignore semicolon between statements Ignore whitespace Script refresh module Script validation for new constraints Verify collation compatibility Verify deployment Click Compare Uncheck any objects flagged for deletion (NB: those may still have collation issues; but since they're not defined in our source/template db we don't know; either way, we don't want to lose things if we're only targeting collation changes). You can unchceck all at once by right clicking on the DELETE folder and selecting EXCLUDE. Likewise exclude for any CREATE objects (here since they don't exist in the target they can't have the wrong collation there; whether they should exist is a question for another topic). Click on each object under CHANGE to see the script for that object. Use the diff to ensure that we're only changing the collation (anything other differences manually detected you'll likely want to exclude / handle those objects manually). Click Update to push changes

这仍然需要一些手工工作(例如,检查你只影响排序)-但它为你处理依赖关系。

此外,您还可以保留一个有效模式的数据库项目,以便在有多个数据库需要更新时为您的数据库使用通用模板,假设所有目标数据库最终都应该具有相同的模式。

你也可以对数据库项目中的文件使用find/replace,如果你希望大量修改那里的设置(例如,你可以使用模式比较从无效的数据库创建项目,修改项目文件,然后在模式比较中切换源/目标,将你的更改推回DB)。

其他回答

在查询中使用collate子句:

LEFT JOIN C tO_C on tA.FieldName = 'CID' AND tA.oldValue COLLATE Latin1_General_CI_AS = tO_C.Name  

我可能没有完全正确的语法(检查BOL),但是您可以这样做来动态地更改查询的排序规则—您可能需要为每个连接添加子句。

编辑:我意识到这不是很正确- collate子句在你需要更改的字段之后-在这个例子中,我更改了tA上的collation。oldValue字段。

我以前有过类似的东西,我们发现两个表之间的排序是不同的。

检查它们是否相同。

当您有2个不同的数据库,特别是来自2个不同服务器的2个不同数据库时,很容易发生这种情况。最好的选择是将其更改为公共集合并进行连接或比较。

SELECT 
   *
FROM sd
INNER JOIN pd ON sd.SCaseflowID COLLATE Latin1_General_CS_AS = pd.PDebt_code COLLATE Latin1_General_CS_AS

你可以通过4个简单的步骤轻松做到这一点

备份你的数据库,以防万一 更改数据库排序规则:右键单击数据库,选择属性,转到选项并将排序规则更改为所需的排序规则。 生成一个脚本来删除和重新创建所有数据库对象:右键单击数据库,选择任务,选择生成脚本…(请确保在向导的高级选项中选择了删除和创建,并选择模式和数据) 运行上面生成的脚本

检查不匹配的排序规则级别(服务器、数据库、表、列、字符)。

如果是服务器,这些步骤曾经帮助过我:

停止服务器 找到sqlservr.exe工具 执行如下命令: sqlservr -m -T4022 -T3659 -s" name_of_instance " q“name_of_collation” 启动sql server: Net启动name_of_instance 再次检查服务器的排序规则。

以下是更多信息:

https://www.mssqltips.com/sqlservertip/3519/changing-sql-server-collation-after-installation/