SQL Server:尽管转换,但无法解决排序规则冲突

时间:2016-11-08 19:56:47

标签: sql-server sql-server-2012 collation

我仍然收到错误代码

  

无法解决“Latin1_General_CI_AS”之间的排序规则冲突   和“SQL_Latin1_General_CP1_CI_AS”中的等于操作。

尽管我按照these两个answers使用转换。这是我的代码:

-- ...
from #TmpResult a
left join #GroupMemberTable b
on (a.DBO_Owner_Login COLLATE SQL_Latin1_General_CP1_CI_AS) = (b.login_name COLLATE SQL_Latin1_General_CP1_CI_AS)

我已将它们转换为:SQL_Latin1_General_CP1_CI_AS

谢谢

更新: 以下是完整的摘录:

create table #TmpTableSec3 (database_name varchar(100), Database_Owner varchar(200), DBO_Owner_Login varchar(200), type varchar (100), privilege varchar(100), group_name varchar(500))
insert into #TmpTableSec3
select a.database_name, a.principal, a.DBO_Owner_Login, b.type, b.privilege, b.group_name
from #TmpResult a
left join #GroupMemberTable b
on (a.DBO_Owner_Login COLLATE SQL_Latin1_General_CP1_CI_AS) = (b.login_name COLLATE SQL_Latin1_General_CP1_CI_AS)
    --a.DBO_Owner_Login = b.login_name
    --fieldname COLLATE DATABASE_DEFAULT = otherfieldname COLLATE DATABASE_DEFAULT
where b.group_name is not null --and b.privilege = 'admin'
order by a.database_name

然后该表填充:

insert into #TmpResult
select a.database_name, a.Owner, b.DBO_Owner_Login from #TmpTableSec1 a
join #TmpTableSec2 b
on a.database_name = b.database_name


  set @cmd = 'select name,  suser_sname(owner_sid) from master.sys.databases where name = '''+@name+''''
  --select @cmd
  insert #TmpTableSec1 exec (@cmd)
  --select @dbowner = (select suser_sname(owner_sid) from master.sys.databases where name = @name)
  set @cmd = 'use '+ @name +'
  select db_name(), name, suser_sname(sid)
  from sys.database_principals where name = ''dbo'''
  --select ''@DBO'' = (select suser_sname(sid) from sys.database_principals where name = ''dbo'') '
  INSERT #TmpTableSec2 exec (@cmd) 

1 个答案:

答案 0 :(得分:0)

drop table #test
drop table #test2

create table #test
(id int, name varchar(100) collate Latin1_General_CI_AI
CONSTRAINT [PK_test] PRIMARY KEY CLUSTERED ([id] ASC)
)

create table #test2
(id int, name varchar(100) collate SQL_Latin1_General_CP1_CI_AS
CONSTRAINT [PK_test2] PRIMARY KEY CLUSTERED ([id] ASC)
)

insert into #test values (1, 'premonition')
insert into #test values (2, 'premònition')
insert into #test values (3, 'lowmotion')

insert into #test2 values (1, 'premonition')
insert into #test2 values (2, 'premònition')
insert into #test2 values (3, 'lowmotion')

select * 
from #test 
inner join #test2 
    on #test.name = #test2.name collate SQL_Latin1_General_CP1_CI_AS

collate SQL_Latin1_General_CP1_CI_AS强制对此collat​​e进行比较,我使用#test2.name

中定义的相同排序规则

也许这个'整理优先'一文可以帮到你。

https://msdn.microsoft.com/en-us/library/ms179886.aspx