我有以下SQL应该抛出错误:
不明确的列名
因为conferencetitle
列在2个表中; course
和coursesession
。
请注意我只选择此列一次。
SELECT
CourseSessionID, CourseSessionNum,
usrCourseSessionNum,
CourseSession.ConferenceTitle,
ConferenceDescription, MaxParticipants,
NumParticipants,
openSeats = ISNULL(MaxParticipants, 0) - ISNULL(NumParticipants, 0),
WaitingList, WaitingListCap,
SessionCancelled,
StartDate, SchedTime, SchedDate, StartTime,
ShowThruDate, CourseSession.RegStartDate,
ExtraCostCreditInfo, Days, IsMaster, IsMasterNoCredit,
CutOffDate, DisableConfRes, MasterSessionId,
Case When Days = 1 Then 'Scheduled Date/Time' Else 'Scheduled Date(s)/Time(s)' End As daysMessage,
Location.Name, econtent, InstructorId, PaymentInstr,
SpecialInstr, ContactName, ContactEmail, ContactPhone,
ConfCWFunctionType, ConfCWFunctionType_opt, PageNumber,
VideoTeleConference, CourseType, ConferenceRequiredInd,
DisableConfRes, ShowSeatsRemainingOnPublic, LinkedSessionID,
CourseSession.CourseNum, PublicStandardKey,
PublicMasterKey, PublicBreakoutKey
FROM
CourseSession
LEFT OUTER JOIN
Location ON CourseSession.LocNum = Location.LocNum
INNER JOIN
Course On CourseSession.CourseNum = Course.CourseNum
WHERE
Course.CourseNum = 1944
AND (Course.DisplayStartingDate IS NULL
OR DATEDIFF(n, Course.DisplayStartingDate, GETDATE()) >= 0)
AND (CourseSession.DisplayDate IS NULL
OR DATEDIFF(n, CourseSession.DisplayDate, GETDATE()) >= 0)
AND ((CourseType = 0 AND CourseSession.ShowThruDate + ' 11:59:59 PM' >= GetDate()) OR (CourseType = 1 And Course.ConfShowThruDate + ' 11:59:59 PM' >= GetDate() AND CourseSession.ShowThruDate + ' 11:59:59 PM' >= GetDate())) AND MasterSessionID Is Null AND SessionCancelled = 0 Order By PageNumber, ConferenceSortOrder, StartDate, StartTime, LinkedSessionID, ConferenceTitle,CASE WHEN numparticipants < Maxparticipants THEN 0 ELSE 1 END, Location.Name
对于使用该应用程序的不同客户,我有类似的(实际上相同的)数据库结构。
我观察到此查询仅为少数客户端提供'Ambiguous column name'
错误,而对于其他客户端则无效。
我无法弄清楚原因。
编辑:我在相同版本的SQL Server 2008上运行查询。
答案 0 :(得分:2)
问题出在ORDER
声明中,您没有指定要使用的ConferenceTitle
:
ORDER BY PageNumber, ConferenceSortOrder, StartDate, StartTime, LinkedSessionID, Course.ConferenceTitle
答案 1 :(得分:0)
2008错误地使用可用的别名来识别要排序的内容。根据我的经验,2008R2解决了这个问题。
答案 2 :(得分:0)
更改order by子句修复了这个问题;但我需要找到原因 即使我运行相同的查询,其他数据库上也没有错误 在他们有相同的数据库结构。
SQL Server 2000和SQL Server 2005之间的行为已发生变化。
阅读Microsoft对以下连接项的回答,了解有关完成原因的详细信息:
感谢您的举报。这种变化是按设计完成的 整体努力的一部分,以更多地绑定列名称 可预测并使其与SQL标准保持一致。其中 变化是:不要忽略列前缀和限制列 引用2部分名称而不是允许更多前缀。这是 完成以帮助发现未作为作者运行的查询 打算或有拼写错误或剪切/粘贴错误。在大多数调查中 这种情况证明是有益的。正如你所说,原作 行为在向后compat模式下维护,所以如果你需要这个 查询继续按原样运行,您需要使用compat模式。
如果确实是这种情况,您可以做些什么来运行以下查询:
SELECT compatibility_level
FROM sys.databases WHERE name = 'DatabaseName'
如果结果为80或更低,那么您运行的是SQL Server 2000兼容性,因此您不会遇到此错误。
有关其他详细信息,请参阅Differences Between Compatibility Level 80 and Level 90的第3项。