隐式转换 - SQL
Implicit conversion - SQL
我有以下 SQL 查询
DECLARE @table table(dbname sysname)
INSERT INTO @table(dbname)
SELECT NAME FROM sys.databases where name like '%AccountsLive'
DECLARE @Sql NVARCHAR(MAX) = NULL;
SELECT @Sql = COALESCE(@Sql + ' UNION ALL ' + CHAR(13) + CHAR(10), '' ) +
'SELECT ST_ALOC_POINTER, ST_TRANTYPE, ST_HEADER_REF, ST_GROSS , ST_CURRENCYCODE , ST_CURR_VALU , ST_DESCRIPTION FROM '
+ QUOTENAME(dbname) + '.dbo.SL_TRANSACTIONS
WHERE ST_YEAR = ''N''
and ST_PERIODNUMBER = ''1''
and ST_ALOC_POINTER like ''%LC%''
and ST_GROSS <> ''0''
UNION ALL
SELECT ST_ALOC_POINTER, ST_TRANTYPE, ST_HEADER_REF, ST_GROSS , ST_CURRENCYCODE , ST_CURR_VALU , ST_DESCRIPTION FROM '
+ QUOTENAME(dbname) + '.dbo.SL_TRANSACTIONS
WHERE ST_YEAR = ''N''
and ST_PERIODNUMBER = ''1''
and ST_ALOC_POINTER like ''%CR%''
and ST_GROSS <> ''0'''
FROM @table
EXEC( @Sql );
但是,我收到如下隐式转换错误:
Implicit conversion of varchar value to varchar cannot be performed because the collation of the value is unresolved due to a collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AS" in UNION ALL operator.
有谁知道这是什么或如何解决我的查询?提前致谢!
当我们将 UNION ALL 放在多个数据库中时,我们遇到了排序规则错误,因为数据库表中的列之间存在排序规则冲突。
为避免这种情况,我们可以针对出现问题的特定列修改查询,如下所示。
我们不需要对每一列都这样做。但是,我不知道是哪一列出现了这个问题,我将查询中的所有列都放入了。
SELECT @Sql = COALESCE(@Sql + ' UNION ALL ' + CHAR(13) + CHAR(10), '' ) +
'SELECT ST_ALOC_POINTER COLLATE DATABASE_DEFAULT, ST_TRANTYPE COLLATE DATABASE_DEFAULT,
ST_HEADER_REF COLLATE DATABASE_DEFAULT, ST_GROSS COLLATE DATABASE_DEFAULT,
ST_CURRENCYCODE COLLATE DATABASE_DEFAULT, ST_CURR_VALU COLLATE DATABASE_DEFAULT,
ST_DESCRIPTION COLLATE DATABASE_DEFAULT FROM '
+ QUOTENAME(dbname) + '.dbo.SL_TRANSACTIONS
错误是关于你的 table 和 sys.databases table 的不同排序规则你应该在 select 语句中使用显式排序规则(问题是 dbname)并且只是改变这一行:
SELECT NAME COLLATE Latin1_General_CI_AS FROM sys.databases where name like '%AccountsLive'
同时将您的 @table
更改为:
DECLARE @table table(dbname sysname COLLATE Latin1_General_CI_AS)
如果不能解决问题请也用这个:
... QUOTENAME(dbname) COLLATE Latin1_General_CI_AS ...
我有以下 SQL 查询
DECLARE @table table(dbname sysname)
INSERT INTO @table(dbname)
SELECT NAME FROM sys.databases where name like '%AccountsLive'
DECLARE @Sql NVARCHAR(MAX) = NULL;
SELECT @Sql = COALESCE(@Sql + ' UNION ALL ' + CHAR(13) + CHAR(10), '' ) +
'SELECT ST_ALOC_POINTER, ST_TRANTYPE, ST_HEADER_REF, ST_GROSS , ST_CURRENCYCODE , ST_CURR_VALU , ST_DESCRIPTION FROM '
+ QUOTENAME(dbname) + '.dbo.SL_TRANSACTIONS
WHERE ST_YEAR = ''N''
and ST_PERIODNUMBER = ''1''
and ST_ALOC_POINTER like ''%LC%''
and ST_GROSS <> ''0''
UNION ALL
SELECT ST_ALOC_POINTER, ST_TRANTYPE, ST_HEADER_REF, ST_GROSS , ST_CURRENCYCODE , ST_CURR_VALU , ST_DESCRIPTION FROM '
+ QUOTENAME(dbname) + '.dbo.SL_TRANSACTIONS
WHERE ST_YEAR = ''N''
and ST_PERIODNUMBER = ''1''
and ST_ALOC_POINTER like ''%CR%''
and ST_GROSS <> ''0'''
FROM @table
EXEC( @Sql );
但是,我收到如下隐式转换错误:
Implicit conversion of varchar value to varchar cannot be performed because the collation of the value is unresolved due to a collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AS" in UNION ALL operator.
有谁知道这是什么或如何解决我的查询?提前致谢!
当我们将 UNION ALL 放在多个数据库中时,我们遇到了排序规则错误,因为数据库表中的列之间存在排序规则冲突。
为避免这种情况,我们可以针对出现问题的特定列修改查询,如下所示。
我们不需要对每一列都这样做。但是,我不知道是哪一列出现了这个问题,我将查询中的所有列都放入了。
SELECT @Sql = COALESCE(@Sql + ' UNION ALL ' + CHAR(13) + CHAR(10), '' ) +
'SELECT ST_ALOC_POINTER COLLATE DATABASE_DEFAULT, ST_TRANTYPE COLLATE DATABASE_DEFAULT,
ST_HEADER_REF COLLATE DATABASE_DEFAULT, ST_GROSS COLLATE DATABASE_DEFAULT,
ST_CURRENCYCODE COLLATE DATABASE_DEFAULT, ST_CURR_VALU COLLATE DATABASE_DEFAULT,
ST_DESCRIPTION COLLATE DATABASE_DEFAULT FROM '
+ QUOTENAME(dbname) + '.dbo.SL_TRANSACTIONS
错误是关于你的 table 和 sys.databases table 的不同排序规则你应该在 select 语句中使用显式排序规则(问题是 dbname)并且只是改变这一行:
SELECT NAME COLLATE Latin1_General_CI_AS FROM sys.databases where name like '%AccountsLive'
同时将您的 @table
更改为:
DECLARE @table table(dbname sysname COLLATE Latin1_General_CI_AS)
如果不能解决问题请也用这个:
... QUOTENAME(dbname) COLLATE Latin1_General_CI_AS ...