以下SQL语句为我提供了“无法解决SELECT语句中第2列的排序规则冲突”。错误。
我可以告诉罪魁祸首是RTRIM(NSG_AUTHORITY.AUTHORITY_NAME)
,因为如果删除该语句,它就会起作用。但是,如果在使用COLLATE之前收到语法错误。我该如何解决这个问题?谢谢
SELECT USRN, RTRIM(STREET_DESCRIPTOR) + ', ' + RTRIM(NSG_TOWN.TOWN_NAME)
AS FEATURE_NAME, KEYWORDS = CAST(USRN AS VARCHAR(15)) + ' ' + RTRIM(STREET_DESCRIPTOR) + '' +
RTrim(NSG_LOCALITY.LOCALITY_NAME) + ' ' + RTRIM(NSG_TOWN.TOWN_NAME) + '' +
RTRIM(NSG_AUTHORITY.AUTHORITY_NAME)
From NSG_STREET INNER Join NSG_TOWN On NSG_TOWN.TOWN_UID = NSG_STREET.TOWN_UID INNER Join
NSG_LOCALITY ON NSG_LOCALITY.LOCALITY_UID = NSG_STREET.LOCALITY_UID INNER JOIN NSG_AUTHORITY ON NSG_AUTHORITY.AUTHORITY_UID = NSG_STREET.AUTHORITY_UID
WHERE CAST(USRN As VARCHAR(15)) + ' ' + RTRIM(STREET_DESCRIPTOR) + ' ' + RTRIM(NSG_LOCALITY.LOCALITY_NAME) + ' ' + RTRIM(NSG_TOWN.TOWN_NAME) + '' +
RTrim(NSG_AUTHORITY.AUTHORITY_NAME) COLLATE DATABASE_DEFAULT LIKE '%3%'
答案 0 :(得分:1)
在您的代码之前运行此代码:
declare @db_collation nvarchar(100) = (select cast( databasepropertyex(db_name(),'collation') as nvarchar) )
declare @altertablequery nvarchar(500)
set @altertablequery = '
alter table NSG_AUTHORITY alter column AUTHORITY_NAME nvarchar (max) collate '+@db_collation+';'
exec (@altertablequery)
答案 1 :(得分:0)
一个更好的答案是向查询中简单添加“ COLLATE DATABASE_DEFAULT”,这根本不需要修改架构。
SELECT USRN, RTRIM(STREET_DESCRIPTOR) + ', ' + RTRIM(NSG_TOWN.TOWN_NAME)
AS FEATURE_NAME, KEYWORDS = CAST(USRN AS VARCHAR(15)) + ' ' + RTRIM(STREET_DESCRIPTOR) + '' +
RTrim(NSG_LOCALITY.LOCALITY_NAME) + ' ' + RTRIM(NSG_TOWN.TOWN_NAME) + '' +
RTRIM(NSG_AUTHORITY.AUTHORITY_NAME) COLLATE DATABASE_DEFAULT
From NSG_STREET INNER Join NSG_TOWN On NSG_TOWN.TOWN_UID = NSG_STREET.TOWN_UID INNER Join
NSG_LOCALITY ON NSG_LOCALITY.LOCALITY_UID = NSG_STREET.LOCALITY_UID INNER JOIN NSG_AUTHORITY ON NSG_AUTHORITY.AUTHORITY_UID = NSG_STREET.AUTHORITY_UID
WHERE CAST(USRN As VARCHAR(15)) + ' ' + RTRIM(STREET_DESCRIPTOR) + ' ' + RTRIM(NSG_LOCALITY.LOCALITY_NAME) + ' ' + RTRIM(NSG_TOWN.TOWN_NAME) + '' +
RTrim(NSG_AUTHORITY.AUTHORITY_NAME) COLLATE DATABASE_DEFAULT LIKE '%3%' COLLATE DATABASE_DEFAULT
您只需在引用的所有位置使用它,并与之进行比较(以确保安全)即可。