我有一个使用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)
我的表格是:
DOWNLOADS_files
downloadID
downloadFileName
DOWNLOADS_fileCategories
categoryID
parentCategoryID
categoryName
DOWNLOADS_fileCategoryLink
categoryID
downloadID
非常感谢!
鲍勃
答案 0 :(得分:3)
您的动态SQL会生成类似
的查询SELECT TOP 10 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
内连接条件
INNER JOIN #tempLinkTable
ON DOWNLOADS_downloadCategoryLink.categoryID = #tempLinkTable.categoryID
引用查询中不存在的表DOWNLOADS_downloadCategoryLink
。我对查询曾经工作感到惊讶,但也许有一些解析器错误已被修复。
要正确解决这个问题,我们需要知道categoryID
实际属于哪个表。
编辑:可能
SELECT TOP (@noRecordsToReturn) 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
INNER JOIN DOWNLOADS_downloadCategoryLink
ON DOWNLOADS_downloadCategoryLink.downloadID = DOWNLOADS_files.downloadID
INNER JOIN #tempLinkTable
ON DOWNLOADS_downloadCategoryLink.categoryID = #tempLinkTable.categoryID
LEFT OUTER JOIN SYS_fileTypes
ON DOWNLOADS_files.fileTypeID = SYS_fileTypes.fileTypeID
ORDER BY DOWNLOADS_files.dateAdded DESC