我有一个使用 CTE 的存储过程,它运行良好。我最近搬到了一个新服务器,但是现在它出现了一个错误
无法绑定多部分标识符“DOWNLOADS_downloadCategoryLink.categoryID”
我可以通过将数据库的兼容性级别更改为 80 (SQL Server 2000) 来解决此问题,但我更愿意正确解决问题 - 有人知道我哪里出错了吗?
存储过程是:
;WITH CTE( CategoryID, ParentCategoryID )
AS
(
SELECT CategoryID, ParentCategoryID
FROM DOWNLOADS_fileCategories
WHERE CategoryID = @startCategoryID
UNION ALL
SELECT a.CategoryID, a.ParentCategoryID
FROM DOWNLOADS_fileCategories a
INNER JOIN CTE b ON b.CategoryID = a.ParentCategoryID
)
SELECT CategoryID INTO #tempLinkTable FROM CTE
set @query = 'SELECT TOP ' + cast(@noRecordsToReturn as varchar(5)) + ' DOWNLOADS_files.downloadID, DOWNLOADS_files.fileTypeID, DOWNLOADS_files.downloadName, DOWNLOADS_files.downloadFileName, DOWNLOADS_files.sizeInKb, DOWNLOADS_files.dateAdded, DOWNLOADS_files.visible, SYS_fileTypes.fileTypeName, SYS_fileTypes.fileTypeExtension
FROM DOWNLOADS_files LEFT OUTER JOIN SYS_fileTypes ON DOWNLOADS_files.fileTypeID = SYS_fileTypes.fileTypeID INNER JOIN
#tempLinkTable ON DOWNLOADS_downloadCategoryLink.categoryID = #tempLinkTable.categoryID
ORDER BY DOWNLOADS_files.dateAdded DESC'
exec(@query)
我有的表是:
下载S_files
downloadID
downloadFileName
DOWNLOADS_fileCategories
categoryID
parentCategoryID
categoryName
DOWNLOADS_fileCategoryLink
categoryID
downloadID
非常感谢!
鲍勃