1

我有一个使用 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

非常感谢!

鲍勃

4

1 回答 1

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 
于 2013-01-16T15:45:27.237 回答