SQL可以整理两次吗?

时间:2015-04-07 14:59:46

标签: sql-server tsql collate

您好我已经创建了以下脚本,我遇到的问题是我查询的两个表的颜色不同,我试图将数据插入第三个表,但由于表的排序规则我不能这样做,由于数据库和依赖它的程序的限制,我也无法自己整理表。 那么可以正确整理这个吗?

Error:    Msg 468, Level 16, State 9, Line 69
    Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AS" in the equal to operation.





        --Create Table #tmp2(FK_clientids varchar(50))
        --Create table #tmp (phonenums varchar(50))
        --Create table #tmp3 (phonenum varchar(50),fk_applicationid_solref varchar(50),calldata varchar(50),sourcetable varchar(50))
        Delete from #tmp2
        Delete from #tmp
        Use DATABASE2
        INSERT INTO #tmp2
        SELECT fk_clientid
        FROM DM_ClientApplicants
        where FK_ApplicationID in (39591,
        39594,
        39598,
        39596,
        39600,
        39601,
        39603,
        39609,
        39613,
        39585,
        39560)

        Use DATABASE2
        INSERT INTO #tmp
        Select phonenum2 from DM_PhoneNumbers
        where FK_ApplicationID in 
        (
           Select FK_clientIDs from #tmp2
        )
        INSERT INTO #tmp
        Select PhoneNum1 from DM_PhoneNumbers
        where FK_ApplicationID in 
        (
           Select FK_clientIDs from #tmp2
        )
        INSERT INTO #tmp
        Select PhoneNum2 from DM_PhoneNumbers
        where FK_ApplicationID in 
        (
           Select FK_clientIDs from #tmp2
        )

        INSERT INTO #tmp
        Select PhoneNum3 from DM_PhoneNumbers
        where FK_ApplicationID in 
        (
           Select FK_clientIDs from #tmp2
        )
        INSERT INTO #tmp
        Select Partnerphonehome from DM_PhoneNumbers
        where FK_ApplicationID in 
        (
           Select FK_clientIDs from #tmp2
        )
        INSERT INTO #tmp
        Select Partnerphonemobile from DM_PhoneNumbers
        where FK_ApplicationID in 
        (
           Select FK_clientIDs from #tmp2
        )
        INSERT INTO #tmp
        Select Partnerphonework from DM_PhoneNumbers
        where FK_ApplicationID in 
        (
           Select FK_clientIDs from #tmp2
        )
        Use DATABASE1
        insert into #tmp3

Select sourcetable,CallData,PhoneNum,P.FK_ApplicationID from Dial D

        join DATABASE2.dbo.DM_PhoneNumbers P on PhoneNum = PhoneNum1
        join DATABASE2.dbo.DM_Sol s on S.FK_ApplicationID = P.FK_ApplicationID
       Collate Latin_general_CI_AS

        where PhoneNum in
        (
        Select phonenums from #tmp
        )

1 个答案:

答案 0 :(得分:1)

您可以使用collat​​e子句作为ON子句的一部分。根据您发布的代码,我不能'告诉哪个是哪个,但它会是这样的:

    join DATABASE2.dbo.DM_PhoneNumbers P 
            on PhoneNum collate SQL_Latin1_General_CP1_CI_AS  = PhoneNum1 

关键是您需要使用整理来修改其中一个连接列的排序规则以匹配另一个连接列。