我创建了一个视图,其中我插入了另一个select语句,并将该语句标记为带有“alias”的另一个字段
视图已成功执行但在尝试从视图中激活select语句时(从视图中选择*)会抛出错误
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression
现在我知道这个错误的原因。这是因为内部选择查询返回多于1行。我不能去TOP 1,因为我需要有一行以上。
ALTER VIEW [dbo].[TestView_new]
AS
SELECT TOP ( 100 ) PERCENT
dbo.DataRepository.PeriodStatus ,
dbo.DataRepository.TimesheetName ,
dbo.DataRepository.TimesheetLineClass ,
dbo.DataRepository.TimesheetLineClassUID ,
dbo.MSP_EpmResource_UserView.ResourceName ,
dbo.MSP_EpmProject_UserView.ProjectName AS ProjectName ,
( SELECT MSP_EpmProject_UserView.ProjectName
FROM dbo.MSP_EpmProject_UserView
LEFT JOIN dbo.DataRepository ON dbo.MSP_EpmProject_UserView.[ProjectUID] = dbo.DataRepository.[ProjectUID]
WHERE LTRIM(RTRIM(dbo.DataRepository.[ProjectUID])) IS NOT NULL
OR LTRIM(RTRIM(dbo.DataRepository.[ProjectUID])) <> '') AS ProjectName2 ,
FROM dbo.DataRepository
INNER JOIN dbo.MSP_TimesheetActual ON dbo.DataRepository.TimesheetLineUID = dbo.MSP_TimesheetActual.TimesheetLineUID
LEFT OUTER JOIN dbo.MSP_EpmResource_UserView ON dbo.DataRepository.ResourceUID = dbo.MSP_EpmResource_UserView.ResourceUID
LEFT OUTER JOIN dbo.MSP_EpmProject_UserView ON dbo.DataRepository.ProjectUID = dbo.MSP_EpmProject_UserView.ProjectUID
我的问题:是否有任何方法可以解决错误?
答案 0 :(得分:0)
尝试在子查询上获得多个结果是不可能的。您似乎尝试在内部查询和主查询上的第二个LEFT OUTER JOIN上加入MSP_EpmProject_UserView和DataRepository。如果没有这些表的结构,我猜你有一个项目名称的几个不同的ProjectUID,这就是为什么返回多行。如果是这种情况,DISTINCT可能很有用。如果没有,那么尝试使用GROUP BY子句。再一次,如果不了解表格结构,我无法说清楚。希望它有所帮助。