我收到此错误:
ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效,除非还指定了TOP或FOR XML。
在下面的代码中,当尝试从视图中订购时。我该如何解决这个问题?
USE [MYDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER VIEW [dbo].[vw_FCoord]
AS
SELECT FeatId, ISNULL(MLNumber, '') AS MLNumber,
(SELECT Text
FROM dbo.tblFType AS ft
WHERE (FTypeId = dbo.tblFeat.FTypeId)) AS FType, Height, Width, Depth, ISNULL(Description, '') AS Description,
Latitude, Longitude * - 1 AS Longitude, IsSubsidence, ISNULL(ProjectName, '')
AS ProjectName, ISNULL
((SELECT Text
FROM dbo.tblFStatusType AS fst
WHERE (FStatusTypeId = dbo.tblFeat.FStatusTypeId)), 'Not Set') AS FStatus, ISNULL
((SELECT Text
FROM dbo.tblSGType AS st
WHERE (SGTypeId = dbo.tblFeat.SGTypeId)), 'Not Set') AS SGType, SGIsBackfillMaterial, SGDate,
ISNULL(SGDetails, '') AS SGDetails, SGCost, SGIsBatCompatible,
(Select COUNT(*) from tblProjectFeat pf where pf.FeatId=dbo.tblFeat.FeatId) NumProjects
FROM dbo.tblFeat
WHERE (Latitude > 0) AND (Longitude > 0)
order by NumProjects asc
GO
答案 0 :(得分:4)
您无法在视图中指定ORDER BY
子句。在没有ORDER BY
的情况下创建视图,并在从视图中选择时指定ORDER BY
子句:
ALTER VIEW [dbo].[vw_FCoord]
...
GO
SELECT
*
FROM vw_FCoord
ORDER BY NumProjects ASC