左连接在'ON'上返回语法错误

时间:2012-07-25 21:39:09

标签: sql sql-server sql-server-2008

Msg 156, Level 15, State 1, Line 107
Incorrect syntax near the keyword 'on'.

我有一个相当大的查询,我只提供一小部分。我在ON SplitAddresses.Client_id=d_client.CLIENT_ID

上收到语法错误
...
left join
(SELECT  distinct
   T.MLIS_CLIENT_ADDRESS_1,T.Client_id,
   Left(T.MLIS_CLIENT_ADDRESS_1, IsNull(SplitAddresses.Pos - 1, 2147483647)) Address1,
   Substring(T.MLIS_CLIENT_ADDRESS_1, SplitAddresses.Pos + 1, 2147483647) Address2
FROM

      d_client
    T 
   OUTER APPLY (
      SELECT TOP 1 NullIf(PatIndex(Delimiter, T.MLIS_CLIENT_ADDRESS_1), 0) Pos
      FROM (
         VALUES ('% Suite %'), ('% Ste %'), ('% Bldg %'), ('% Building %'), ('%#%')
      ) SplitAddresses (Delimiter)
      WHERE T.MLIS_CLIENT_ADDRESS_1 LIKE SplitAddresses.Delimiter 
      ORDER BY Pos
   ) SplitAddresses
on 
SplitAddresses.Client_id=d_client.CLIENT_ID


WHERE...
我是在做一些明显错误的语法还是有更深层次的问题?非常感谢你的帮助。

3 个答案:

答案 0 :(得分:4)

以下是对SQL的重新格式化,可能会让您更清楚自己的问题:

left join
(
SELECT Distinct
    T.MLIS_CLIENT_ADDRESS_1,
    T.Client_id,
    Left(T.MLIS_CLIENT_ADDRESS_1, IsNull(SplitAddresses.Pos - 1, 2147483647)) Address1,
    Substring(T.MLIS_CLIENT_ADDRESS_1, SplitAddresses.Pos + 1, 2147483647) Address2
FROM
    d_client T 
        OUTER APPLY 
        (
            SELECT TOP 1 
                NullIf(PatIndex(Delimiter, T.MLIS_CLIENT_ADDRESS_1), 0) Pos
            FROM 
            (
                VALUES 
                    ('% Suite %'), 
                    ('% Ste %'), 
                    ('% Bldg %'), 
                    ('% Building %'), 
                    ('%#%')
            ) SplitAddresses (Delimiter)
            WHERE 
                T.MLIS_CLIENT_ADDRESS_1 LIKE SplitAddresses.Delimiter 
            ORDER BY Pos
        ) SplitAddresses
on SplitAddresses.Client_id=d_client.CLIENT_ID

看起来你错过了第二个)在SplitAddresses之前做到了:

left join
    (
    SELECT Distinct
        T.MLIS_CLIENT_ADDRESS_1,
        T.Client_id,
        Left(T.MLIS_CLIENT_ADDRESS_1, IsNull(SplitAddresses.Pos - 1, 2147483647)) Address1,
        Substring(T.MLIS_CLIENT_ADDRESS_1, SplitAddresses.Pos + 1, 2147483647) Address2
    FROM
        d_client T 
            OUTER APPLY 
            (
                SELECT TOP 1 
                    NullIf(PatIndex(Delimiter, T.MLIS_CLIENT_ADDRESS_1), 0) Pos
                FROM 
                (
                    VALUES 
                        ('% Suite %'), 
                        ('% Ste %'), 
                        ('% Bldg %'), 
                        ('% Building %'), 
                        ('%#%')
                ) SplitAddresses (Delimiter)
                WHERE 
                    T.MLIS_CLIENT_ADDRESS_1 LIKE SplitAddresses.Delimiter 
                ORDER BY Pos
            ) SA
    ) SplitAddresses
    on SplitAddresses.Client_id=d_client.CLIENT_ID

答案 1 :(得分:1)

您似乎正在为d_client提供T的别名,而不是在on声明中使用它。

答案 2 :(得分:1)

on之前的结束括号结束outer apply,而不是您想要使用它的select。添加另一个结束括号。