单词ON附近的语法错误不正确

时间:2014-03-31 19:38:19

标签: sql sql-server

我一直在单词ON附近得到错误的语法,这段代码来自之前帖子的建议更改,我试图找出错误的位置,到目前为止,我没有运气搞清楚我是什么标点符号失踪。代码是

SELECT AC.REG_NR, AC.DIS_NR, AC.GEMSID, AC.TMS_ID, AC.EMP_NA, AC.EMP_SEX_TYP_CD, AC.EMP_EOC_GRP_TYP_CD, AC.DIV_NR, AC.CTR_NR, 
           AC.JOB_CLS_CD_DSC_TE, AC.JOB_GRP_CD, AC.Job_Function, AC.Job_Group, AC.Meeting_Readiness_Rating, AC.Manager_Readiness_Rating, CD.Employee_ID, 
           CD.Meeting_Readiness_Rating AS Expr1, CD.Manager_Readiness_Rating AS Expr2, CD.Meeting_End_Date, CD.EmployeeFeedback, 
           CD.DevelopmentForEmployee1, CD.DevelopmentForEmployee2, CD.DevelopmentForEmployee3, CD.DevelopmentForEmployee4, CD.DevelopmentForEmployee5, 
           CD.Justification, CD.Changed, CD.Notes
FROM  dbo.AC_Source AS AC INNER JOIN
(SELECT EmployeeID AS Employee_ID, MeetingReadinessLevel AS Meeting_Readiness_Rating, ManagerReadinessLevel AS Manager_Readiness_Rating, 
                               logdate AS Meeting_End_Date, EmployeeFeedback, DevelopmentForEmployee1, DevelopmentForEmployee2, DevelopmentForEmployee3, 
                               DevelopmentForEmployee4, DevelopmentForEmployee5, Justification, Changed, Notes
FROM (SELECT EmployeeID AS Employee_ID, MeetingReadinessLevel AS Meeting_Readiness_Rating, ManagerReadinessLevel AS Manager_Readiness_Rating, 
                               logdate AS Meeting_End_Date, EmployeeFeedback, DevelopmentForEmployee1, DevelopmentForEmployee2, DevelopmentForEmployee3, 
                               DevelopmentForEmployee4, DevelopmentForEmployee5, Justification, Changed, Notes, ROW_NUMBER() OVER (PARTITION BY EmployeeID ORDER BY Meeting_End_Date DESC) As RowNum
FROM (SELECT EmployeeID AS Employee_ID, MeetingReadinessLevel AS Meeting_Readiness_Rating, ManagerReadinessLevel AS Manager_Readiness_Rating, 
                               logdate AS Meeting_End_Date, EmployeeFeedback, DevelopmentForEmployee1, DevelopmentForEmployee2, DevelopmentForEmployee3, 
                               DevelopmentForEmployee4, DevelopmentForEmployee5, Justification, Changed, Notes
                FROM   dbo.AC_CDData_1
                UNION ALL
                SELECT Employee_ID, Meeting_Readiness_Rating, Manager_Readiness_Rating, Meeting_End_Date, '' AS EmployeeFeedback, '' AS DevelopmentForEmployee1, 
                               '' AS DevelopmentForEmployee2, '' AS DevelopmentForEmployee3, '' AS DevelopmentForEmployee4, '' AS DevelopmentForEmployee5, '' AS Justification, 
                               '' AS Changed, '' AS Notes
                FROM  dbo.TMS_Data_Latest_Career_Meeting_Rating
                WHERE (Plan_Year = '2013')) AS InnerSelect) AS CD ON AC.TMS_ID = CD.Employee_ID
WHERE RowNum = 1)
ON AC.TMS_ID = CD.EMPLOYEE_ID;

3 个答案:

答案 0 :(得分:0)

首先,所有子查询都需要别名。因此,请为子查询指定名称。我认为它是这样的:

SELECT AC.REG_NR, AC.DIS_NR, AC.GEMSID, AC.TMS_ID, AC.EMP_NA, AC.EMP_SEX_TYP_CD,
       AC.EMP_EOC_GRP_TYP_CD, AC.DIV_NR, AC.CTR_NR, 
       AC.JOB_CLS_CD_DSC_TE, AC.JOB_GRP_CD, AC.Job_Function, AC.Job_Group,
       AC.Meeting_Readiness_Rating, AC.Manager_Readiness_Rating, CD.Employee_ID, 
       CD.Meeting_Readiness_Rating AS Expr1, CD.Manager_Readiness_Rating AS Expr2, 
       CD.Meeting_End_Date, CD.EmployeeFeedback, 
       CD.DevelopmentForEmployee1, CD.DevelopmentForEmployee2, CD.DevelopmentForEmployee3, 
       CD.DevelopmentForEmployee4, CD.DevelopmentForEmployee5, 
       CD.Justification, CD.Changed, CD.Notes
FROM  dbo.AC_Source AS AC INNER JOIN
       (SELECT EmployeeID AS Employee_ID, MeetingReadinessLevel AS Meeting_Readiness_Rating, ManagerReadinessLevel AS Manager_Readiness_Rating, 
                               logdate AS Meeting_End_Date, EmployeeFeedback, DevelopmentForEmployee1, DevelopmentForEmployee2, DevelopmentForEmployee3, 
                               DevelopmentForEmployee4, DevelopmentForEmployee5, Justification, Changed, Notes
FROM (SELECT EmployeeID AS Employee_ID, MeetingReadinessLevel AS Meeting_Readiness_Rating,
             ManagerReadinessLevel AS Manager_Readiness_Rating, 
             logdate AS Meeting_End_Date, EmployeeFeedback, DevelopmentForEmployee1, 
             DevelopmentForEmployee2, DevelopmentForEmployee3, 
             DevelopmentForEmployee4, DevelopmentForEmployee5, Justification, Changed, 
             Notes,
             ROW_NUMBER() OVER (PARTITION BY EmployeeID ORDER BY Meeting_End_Date DESC) As RowNum
      FROM (SELECT EmployeeID AS Employee_ID, MeetingReadinessLevel AS Meeting_Readiness_Rating, ManagerReadinessLevel AS Manager_Readiness_Rating, 
                               logdate AS Meeting_End_Date, EmployeeFeedback, DevelopmentForEmployee1, DevelopmentForEmployee2, DevelopmentForEmployee3, 
                               DevelopmentForEmployee4, DevelopmentForEmployee5, Justification, Changed, Notes
            FROM   dbo.AC_CDData_1
            UNION ALL
            SELECT Employee_ID, Meeting_Readiness_Rating, Manager_Readiness_Rating, Meeting_End_Date, '' AS EmployeeFeedback, '' AS DevelopmentForEmployee1, 
                               '' AS DevelopmentForEmployee2, '' AS DevelopmentForEmployee3, '' AS DevelopmentForEmployee4, '' AS DevelopmentForEmployee5, '' AS Justification, 
                               '' AS Changed, '' AS Notes
            FROM  dbo.TMS_Data_Latest_Career_Meeting_Rating
            WHERE (Plan_Year = '2013')) AS InnerSelect
          ) cd
     ) cd
     ON AC.TMS_ID = CD.EMPLOYEE_ID and rownum = 1

答案 1 :(得分:0)

这是您的查询应该是这样的。如果您使用CTE,则易于阅读且容易发现错误。

有关CTE http://technet.microsoft.com/en-us/library/ms190766(v=sql.105).aspx

的文件
    ;WITH   Query1
          AS (
               SELECT EmployeeID AS Employee_ID
                   ,MeetingReadinessLevel AS Meeting_Readiness_Rating
                   ,ManagerReadinessLevel AS Manager_Readiness_Rating
                   ,logdate AS Meeting_End_Date
                   ,EmployeeFeedback
                   ,DevelopmentForEmployee1
                   ,DevelopmentForEmployee2
                   ,DevelopmentForEmployee3
                   ,DevelopmentForEmployee4
                   ,DevelopmentForEmployee5
                   ,Justification
                   ,Changed
                   ,Notes
                FROM dbo.AC_CDData_1
               UNION ALL
               SELECT Employee_ID
                   ,Meeting_Readiness_Rating
                   ,Manager_Readiness_Rating
                   ,Meeting_End_Date
                   ,'' AS EmployeeFeedback
                   ,'' AS DevelopmentForEmployee1
                   ,'' AS DevelopmentForEmployee2
                   ,'' AS DevelopmentForEmployee3
                   ,'' AS DevelopmentForEmployee4
                   ,'' AS DevelopmentForEmployee5
                   ,'' AS Justification
                   ,'' AS Changed
                   ,'' AS Notes
                FROM dbo.TMS_Data_Latest_Career_Meeting_Rating
                WHERE Plan_Year = '2013'
             ),
        Query2
          AS (
               SELECT Employee_ID
                   ,Meeting_Readiness_Rating
                   ,Manager_Readiness_Rating
                   ,Meeting_End_Date
                   ,EmployeeFeedback
                   ,DevelopmentForEmployee1
                   ,DevelopmentForEmployee2
                   ,DevelopmentForEmployee3
                   ,DevelopmentForEmployee4
                   ,DevelopmentForEmployee5
                   ,Justification
                   ,Changed
                   ,Notes
                   ,ROW_NUMBER() OVER ( PARTITION BY Employee_ID ORDER BY Meeting_End_Date DESC ) AS Rn
                FROM Query1 AS q1
             )
    SELECT AC.REG_NR
           ,AC.DIS_NR
           ,AC.GEMSID
           ,AC.TMS_ID
           ,AC.EMP_NA
           ,AC.EMP_SEX_TYP_CD
           ,AC.EMP_EOC_GRP_TYP_CD
           ,AC.DIV_NR
           ,AC.CTR_NR
           ,AC.JOB_CLS_CD_DSC_TE
           ,AC.JOB_GRP_CD
           ,AC.Job_Function
           ,AC.Job_Group
           ,AC.Meeting_Readiness_Rating
           ,AC.Manager_Readiness_Rating
           ,CD.Employee_ID
           ,CD.Meeting_Readiness_Rating AS Expr1
           ,CD.Manager_Readiness_Rating AS Expr2
           ,CD.Meeting_End_Date
           ,CD.EmployeeFeedback
           ,CD.DevelopmentForEmployee1
           ,CD.DevelopmentForEmployee2
           ,CD.DevelopmentForEmployee3
           ,CD.DevelopmentForEmployee4
           ,CD.DevelopmentForEmployee5
           ,CD.Justification
           ,CD.Changed
           ,CD.Notes
        FROM dbo.AC_Source AS AC
        INNER JOIN Query2 AS cd
            ON AC.TMS_ID = CD.EMPLOYEE_ID
               AND cd.rn = 1

答案 2 :(得分:0)

这个查询有问题(好吧,没什么新东西......)。最终子查询中没有别名,但似乎还有更多。

简化且正确缩进的形式可以帮助您发现问题:

SELECT x
FROM  dbo.AC_Source AS AC 
INNER JOIN (
    SELECT x
    FROM (
        SELECT x, ROW_NUMBER() OVER (PARTITION BY EmployeeID ORDER BY Meeting_End_Date DESC) As RowNum
        FROM (
            SELECT x
            FROM   dbo.AC_CDData_1
            UNION ALL
            SELECT x
            FROM  dbo.TMS_Data_Latest_Career_Meeting_Rating
            WHERE Plan_Year = '2013'
        ) AS InnerSelect
    ) AS CD ON AC.TMS_ID = CD.Employee_ID
    WHERE RowNum = 1
) ON AC.TMS_ID = CD.EMPLOYEE_ID;