尝试查找特定 table 和列时无法解决排序规则冲突

Cannot resolve the collation conflict when try to find specific table and column

我使用 SQL Server 2016。

我需要在服务器上的所有数据库中查找 table 名称和列名。

DECLARE @SQL NVARCHAR(max)

SET @SQL = STUFF((
            SELECT '
UNION
SELECT ' + quotename(NAME, '''') + ' as Db_Name, t.Name collate SQL_Latin1_General_CP1_CI_AS as Table_Name,
c.Name as Column_Name
FROM ' + quotename(NAME) + '.sys.tables as t 
INNER JOIN ' + quotename(NAME) + '.sys.columns as c ON t.object_id = c.object_id
WHERE t.NAME LIKE ''%'' + @TableName + ''%'' 
AND c.NAME LIKE ''%'' + @ColumnName + ''%'' '  
            FROM sys.databases
            ORDER BY NAME
            FOR XML PATH('')
                ,type
            ).value('.', 'nvarchar(max)'), 1, 8, '')

--PRINT @SQL;

EXECUTE sp_executeSQL @SQL
    ,N'@TableName varchar(30), @ColumnName varchar(30)'
    ,@TableName = 'Bid'
    ,@ColumnName = 'bidid'

我遇到了这个错误

Cannot resolve the collation conflict between "Latin1_General_100_CI_AS_KS_WS" and "SQL_Latin1_General_CP1_CI_AS" in the UNION operation.

关注此话题:

Cannot resolve the collation conflict?

我尝试通过添加到我的内部联接或 where 子句来将整理设置为 'DATABASE_DEFAULT',但这并没有解决错误。

通过如下添加到 select 列表排序规则:“,c.Name collate SQL_Latin1_General_CP1_CI_AS as Column_Name”效果非常好