SQL - 外部应用在 SQL Server 2000 中不起作用?
SQL - Outer Apply does not work in SQL Server 2000?
我之前在我公司的测试服务器上测试过创建一个新的视图设计
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 Server 2000.
现在的问题是,真的是这样吗?如果是,是否有解决方法?请注意,我没有办法或权限升级我的实时服务器。
对于内部查询中的按使用排序,请在查询中使用前 100%。
GO
select col1, col2 from
( select top 100 percent col1, col2,
... from yourtable) as DC
GO
您收到错误是因为 SQL 服务器允许从 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
我之前在我公司的测试服务器上测试过创建一个新的视图设计
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 Server 2000.
现在的问题是,真的是这样吗?如果是,是否有解决方法?请注意,我没有办法或权限升级我的实时服务器。
对于内部查询中的按使用排序,请在查询中使用前 100%。
GO
select col1, col2 from
( select top 100 percent col1, col2,
... from yourtable) as DC
GO
您收到错误是因为 SQL 服务器允许从 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