我以前在公司测试服务器中进行了测试,以创建新的视图设计
SELECT
a.[CourseID], a.[Course Code], a.[Course Title] AS [Course Description],
a.[Result], a.[Employee No], a.[Employee Name], a.[Section],
CONVERT(VARCHAR, b.ActStartDate, 105) AS [Last Training Date],
CONVERT(VARCHAR, b.ValidTo, 105) AS [Validity Period],
a.[Status], a.[Flag]
FROM
(SELECT
c.CourseID, c.CourseCode AS [Course Code], c.CourseTitle AS [Course Title],
c.CourseCode + ' - ' + c.CourseTitle AS Result,
b.EMPLOYEE_NO AS [Employee No], b.EMPLOYEE_NAME AS [Employee Name],
b.SECTION_CODE + ' - ' + b.SECTION_DESCRIPTION AS Section,
b.EMPLOYEE_STATUS AS Status, a.Flag
FROM
dbo.ZOJT_EMP_COURSE AS a
INNER JOIN
dbo.vEMPLOYEE_PROFILE AS b ON a.Employee_No = b.EMPLOYEE_NO
INNER JOIN
dbo.tTMS_Course AS c ON c.CourseCode = a.CourseCode) A
OUTER APPLY
(SELECT TOP 1 *
FROM
(SELECT
b.CourseID, a.EmpNo, b.ActStartDate, a.ValidTo
FROM
dbo.tTMS_Training_Record AS a
INNER JOIN
(SELECT
SchID, CourseID, MAX(ActStartDate) AS ActStartDate
FROM
dbo.tTMS_Training_Schedule AS z
GROUP BY
SchID, CourseID) AS b ON a.SchID = b.SchID) AS y
WHERE
ActStartDate IS NOT NULL
AND CourseID = A.CourseID
AND EmpNo = A.[Employee No]
ORDER BY
ActStartDate DESC, ValidTo DESC) B
但是,当我尝试在实时环境中移动此查询时,会出现2个错误的语法错误。
一个在Outer Apply
附近,另一个在Order By ActStartDate
附近。当我问到这个问题时,我的前辈认为这可能是由于我用来测试的测试服务器是SQL Server 2008而实时服务器是SQL Server2000。
现在的问题是,真的是这样吗?如果是,是否有解决方法?请注意,我无权或无权升级实时服务器。
答案 0 :(得分:2)
您收到此错误,因为SQL Server允许从SQL Server 2005到更高版本的APPLY运算符。要使用APPLY,数据库兼容性级别必须至少为90,该级别从2005年开始可用。您必须使用子查询替换outer apply
SELECT
a.[CourseID], a.[Course Code], a.[Course Title] AS [Course Description], a.[Result], a.[Employee No], a.[Employee Name], a.[Section], CONVERT(varchar, b.ActStartDate,
105) AS [Last Training Date], CONVERT(varchar, b.ValidTo, 105) AS [Validity Period], a.[Status], a.[Flag]
FROM (SELECT c.CourseID, c.CourseCode AS [Course Code], c.CourseTitle AS [Course Title], c.CourseCode + ' - ' + c.CourseTitle AS Result, b.EMPLOYEE_NO AS [Employee No],
b.EMPLOYEE_NAME AS [Employee Name], b.SECTION_CODE + ' - ' + b.SECTION_DESCRIPTION AS Section, b.EMPLOYEE_STATUS AS Status, a.Flag
FROM dbo.ZOJT_EMP_COURSE AS a INNER JOIN
dbo.vEMPLOYEE_PROFILE AS b ON a.Employee_No = b.EMPLOYEE_NO INNER JOIN
dbo.tTMS_Course AS c ON c.CourseCode = a.CourseCode)
A left join
(SELECT max(ActStartDate) as ActStartDate,
max(ValidTo) as ValidTo,CourseID,EmpNo
FROM (SELECT b.CourseID, a.EmpNo, b.ActStartDate, a.ValidTo
FROM dbo.tTMS_Training_Record AS a INNER JOIN
(SELECT SchID, CourseID, MAX(ActStartDate) AS ActStartDate
FROM dbo.tTMS_Training_Schedule AS z
GROUP BY SchID, CourseID) AS b ON a.SchID = b.SchID) as y
WHERE ActStartDate IS NOT NULL
--AND CourseID = A.CourseID AND EmpNo = A.[Employee No]
--ORDER BY ActStartDate DESC, ValidTo DESC
group by CourseID,EmpNo
) B
on a.courseid = b.courseid and a.[employee no] = b.empno
答案 1 :(得分:0)
对于在内部查询中使用的订单,请在查询中使用前100%。
GO
select col1, col2 from
( select top 100 percent col1, col2,
... from yourtable) as DC
GO