1

我尝试创建有序的#Temp 表:

WITH Ordered AS (
SELECT ROW_NUMBER() OVER (ORDER BY [Quality] DESC) AS RowNumber, ImageID, Quality, Border, IsBest
  FROM [CatalogImages].[dbo].[Images] ORDER BY Quality)

SELECT *
  INTO #Temp
  FROM Ordered ;

 SELECT * FROM #Temp ;

但我得到错误:

消息 1033,级别 15,状态 1,第 82 行
ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效,除非还指定了 TOP 或 FOR XML。

如何创建有序临时表?

4

1 回答 1

1

错误是因为您试图在公用表表达式中执行 Order By,这是不允许的。

仅从您发布的内容来看,您的 CTE 似乎不是必需的。你可以这样做:

SELECT ROW_NUMBER() OVER (ORDER BY [Quality] DESC) AS RowNumber, ImageID, Quality, Border, IsBest 
INTO #Temp 
ORDER BY Quality; 

SELECT * 
FROM #Temp 
ORDER BY Quality; 

(这也不意味着需要临时表......)

于 2012-07-31T20:41:15.733 回答