“不明确的列名称”错误

时间:2016-09-05 07:56:01

标签: sql sql-server

我收到错误

  

其他信息:不明确的列名'WeekNo'

我将此SQL添加到查询后出现此错误:

JOIN 
    sip.StudentJournalDate sjd ON sjd.WeekNo = sj.WeekNo 
                               AND sjd.AdminNo = sj.AdminNo 
                               AND sjd.ProjectId = sj.ProjectId         

完整查询:

SELECT
    sj.LOComment,      
    CONVERT(varchar, sjd.StartDateEntry,103) WeekStartDate,
    CONVERT(varchar, sjd.EndDateEntry,103) WeekEndDate,
    supervisor.SupervisorEmails,
    LO.LOEmails,
    sj.AdminNo,
    sj.AcadYear, 
    sj.WeekNo, 
    sj.StartDateEntry,                         
    sj.Day1Journal, sj.Day2Journal, sj.Day3Journal, sj.Day4Journal, sj.Day5Journal, sj.Day6Journal, sj.Day7Journal, 
    sj.WeeklyReflection, 
    sj.Day1TypeOfLeaveCode, sj.Day2TypeOfLeaveCode, sj.Day3TypeOfLeaveCode, 
    sj.Day4TypeOfLeaveCode, sj.Day5TypeOfLeaveCode, sj.Day6TypeOfLeaveCode, 
    sj.Day7TypeOfLeaveCode, 
    sj.ProjectId, sj.coursesiprunid
FROM  
    sip.StudentJournal sj 
JOIN 
    sip.StudentJournalDate sjd ON sjd.WeekNo = sj.WeekNo 
                               AND sjd.AdminNo = sj.AdminNo 
                               AND sjd.ProjectId = sj.ProjectId
LEFT OUTER JOIN 
     (SELECT  
          adminno, projectId, coursesiprunid,
          STUFF((SELECT ',' + CAST(EmailAddress AS VARCHAR(320)) [text()]
                 FROM sip.ApplicationProjectLO a
                 INNER JOIN V_SIP_ALLSTAFF vsa ON a.StaffEmailId = vsa.LOGINID
                 WHERE adminno = t.adminno
                   AND a.type IN ('L','A')
                 FOR XML PATH(''), TYPE).value('.','NVARCHAR(MAX)'), 1, 1, ' ') LOEmails
      FROM 
          sip.ApplicationProjectLO t
      GROUP BY 
          adminno, projectId, coursesiprunid) Lo ON sj.AdminNo = Lo.AdminNo 
                                                 AND sj.CourseSIPRunId = Lo.CourseSIPRunId
                                                 AND sj.projectid = Lo.ProjectId
LEFT OUTER JOIN 
    (SELECT  
         adminno, projectId, coursesiprunid,
         STUFF((SELECT ',' + CAST(vsa.StaffEmail AS VARCHAR(320)) [text()]
                FROM sip.ApplicationProjectSupervisor  a
                INNER JOIN sip.CompanyStaff vsa ON a.CompanyStaffId = vsa.CompanyStaffId
                WHERE adminno = t.adminno
                  AND a.type IN ('S', 'MR')
                FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, ' ') SupervisorEmails
     FROM 
         sip.ApplicationProjectSupervisor t
     GROUP BY 
         adminno, projectId, coursesiprunid) Supervisor ON sj.AdminNo = Supervisor.adminno
                                                        AND sj.CourseSIPRunId =Supervisor.CourseSIPRunId
                                                        AND sj.projectid = Supervisor.ProjectId
WHERE
    sj.AdminNo = @adminno 
    AND sj.CourseSIPRunId = @coursesiprunid
    AND sj.projectid = @projectId
    AND WeekNo = @weekNo";  

2 个答案:

答案 0 :(得分:0)

  

你没有提到别名。 alias.WeekNO = @WeekNo。

WHERE
    sj.AdminNo = @adminno 
    AND sj.CourseSIPRunId = @coursesiprunid
    AND sj.projectid = @projectId
    AND sj.WeekNo = @weekNo";

答案 1 :(得分:0)

让我举一个例子。当房间里有两个人同名时(user3807187),当有人用同一个名字打电话时,你们两个都会感到困惑。这就是您的代码所发生的事情。为此,您可以使用名称添加姓氏来临时重命名它们,然后可以清除由别名名称调用的两个人。数据库也需要相同的内容。当两个表具有相同的colume名称时,当您选择该字段时,您附加表名及其列名以清除歧义。

您需要为没有别名的列添加别名,例如

WHERE tablename.adminno = t.adminno

同样适用于所有ambigious名称字段(CourseSIPRunId,..)