0

I have created a view in which I have inserted another select statement and labelled that statement as another fields with "alias"

The view got executed successfully but when trying to fire select statement from view (select * from view) it throws an error of

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression

Now I'm aware the reason of this error coming. It is because of that the inner select query returns more than 1 row. I cannot go for TOP 1 as I need to have more than one row.

 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

My Question: Is there any way that the error can be resolved?

4

1 回答 1

0

尝试在子查询上获得多个结果是不可能的。您似乎正在尝试在内部查询和主查询的第二个 LEFT OUTER JOIN 上加入 MSP_EpmProject_UserView 和 DataRepository 。如果没有这些表的结构,我猜你有几个不同的 ProjectUID 用于一个项目名称,这就是返回多行的原因。如果是这种情况,DISTINCT 可能会很有用。如果没有,请尝试使用 GROUP BY 子句。再一次,在不知道表格结构的情况下,我无法说出更多信息。希望能帮助到你。

于 2012-05-29T07:18:26.737 回答