0

i have the following code

  DECLARE @ProjectID INT
    DECLARE @getSLAPrjectID CURSOR
    SET @getSLAPrjectID = CURSOR FOR SELECT ProjectID FROM SLA

    OPEN @getSLAPrjectID

    FETCH NEXT
    FROM @getSLAPrjectID INTO @ProjectID
    WHILE @@FETCH_STATUS = 0
    BEGIN

    BEGIN

    SET @ScheduleVariance = (select case when (DATEDIFF(day,PlannedStartDate,PlannedEndDate)=0) THEN 0 ELSE (DATEDIFF(day,ActualStartDate,ActualEndDate)-DATEDIFF(day,PlannedStartDate,PlannedEndDate))/CAST(DATEDIFF(day,PlannedStartDate,PlannedEndDate) as float) END from SLA)

    -- other piece of code that is working fine

    END

    FETCH NEXT
    FROM @getSLAPrjectID INTO @ProjectID

    END

    CLOSE @getSLAPrjectID
    DEALLOCATE @getSLAPrjectID

    --end

I AM GETTING THE FOLLOWING ERROR : Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

please let me know if there is any alternative to either CASE statement in this piece of code or alternative to the scalar variable .

4

4 に答える 4

1

サブクエリ内でwhere条件SLA.ProjectID=@ProjectIDを試してください。

SET @ScheduleVariance = (select case when (DATEDIFF(day,PlannedStartDate,PlannedEndDate)=0) THEN 0 ELSE (DATEDIFF(day,ActualStartDate,ActualEndDate)-DATEDIFF(day,PlannedStartDate,PlannedEndDate))/CAST(DATEDIFF(day,PlannedStartDate,PlannedEndDate) as float) END from SLA where SLA.ProjectID = @ProjectID)
于 2012-08-08T13:08:54.687 に答える
0

SLAには複数の行があります。クエリが決定論的であることを確認するために使用するTOP 1限り、を使用してこれを回避できます。ORDER BY

SELECT  TOP 1 @ScheduleVariance = CASE WHEN (DATEDIFF(DAY, PlannedStartDate,PlannedEndDate) = 0) THEN 0 
                                ELSE (DATEDIFF(DAY, ActualStartDate, ActualEndDate) / CAST(DATEDIFF(DAY,PlannedStartDate, PlannedEndDate) AS FLOAT)) - 1 
                            END 
FROM    SLA
ORDER BY ProjectID

ただし、クエリからwhere句も欠落していると思われます。

SELECT  @ScheduleVariance = CASE WHEN (DATEDIFF(DAY, PlannedStartDate,PlannedEndDate) = 0) THEN 0 
                                ELSE (DATEDIFF(DAY, ActualStartDate, ActualEndDate) / CAST(DATEDIFF(DAY,PlannedStartDate, PlannedEndDate) AS FLOAT)) - 1 
                            END 
FROM    SLA
WHERE   ProjectID = @ProjectID

また

SET @ScheduleVariance = 
    (   SELECT  CASE WHEN (DATEDIFF(day,PlannedStartDate,PlannedEndDate) = 0) THEN 0 
                    ELSE (DATEDIFF(day, ActualStartDate, ActualEndDate) / CAST(DATEDIFF(DAY,PlannedStartDate, PlannedEndDate) AS FLOAT)) - 1 
                END 
        FROM    SLA
        WHERE   ProjectID = @ProjectID
    )

注、私もあなたの計算を整理したので、変更しました

(x - y) / y 

(x / y) - 1
于 2012-08-08T13:18:27.067 に答える
0

ほとんどの場合、テーブルSLAに複数の行が含まれていることが原因です。これを解決するには、結果を1行に絞り込むWHERE句を追加する必要があります。

行セットの最初の行(適切にソートされている)が探しているものであることが絶対に確実でない限り、TOP1を使用しないことを強くお勧めします。

于 2012-08-08T13:08:44.067 に答える
0

Clearly this line is returning more than one row:

SET @ScheduleVariance = (select case when (DATEDIFF(day,PlannedStartDate,PlannedEndDate)=0)
                                     THEN 0
                                     ELSE (DATEDIFF(day,ActualStartDate,ActualEndDate)-DATEDIFF(day,PlannedStartDate,PlannedEndDate))/CAST(DATEDIFF(day,PlannedStartDate,PlannedEndDate) as float)
                         END from SLA)

You need one of the following to get one row: WHERE clause, aggregation function (WHERE ?), or TOP 1.

于 2012-08-08T13:10:40.060 に答える