2010-12-11 36 views
1


Ich möchte eine "Select-Klausel" je nach Bedingungsbedingung schreiben! bu Ich habe Fehler:
WHERE Etwas IN (CASE WHEN Anweisung)?

Msg 512, Level 16, State 1, Line 2 
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 

Wie kann ich es beheben ??
hier ist mein vereinfachte Code:

SELECT UnitsAllocation.UnitID 
     , OrganizationUnits.Title AS UnitTitle 
     , 'Title' AS ExpenseTitle1 
     , SUM(UnitsAllocationDetails1.ExpenseAmount1) AS ExpenseAmount1 
FROM [bdg_UnitsAllocation] UnitsAllocation 
     LEFT OUTER JOIN (
      SELECT UnitsAllocationDetails.UnitsAllocationID 
        , SUM(UnitsAllocationDetails.Amount)/1 AS ExpenseAmount1 
      FROM [bdg_UnitsAllocationDetails] UnitsAllocationDetails 
      WHERE UnitsAllocationDetails.ExpenseID IN (
        CASE 1 WHEN 1 
        THEN (SELECT Id FROM bdg_Expenses WHERE ParentId = 1) 
        ELSE (SELECT Id FROM bdg_Expenses WHERE Id = 1) 
        END 
       ) 
      GROUP BY 
        UnitsAllocationDetails.UnitsAllocationID 
     ) UnitsAllocationDetails1 ON UnitsAllocationDetails1.UnitsAllocationID = UnitsAllocation.ID 
     LEFT OUTER JOIN [bdg_OrganizationUnits] OrganizationUnits ON UnitsAllocation.UnitID = OrganizationUnits.ID 
GROUP BY 
     UnitsAllocation.UnitID, OrganizationUnits.Title 

bitte auf "CASE" aussehen und "IN" Aussage.

+1

Der '1 = 1' Fall wird immer wahr bleiben. Warum benutzt du dort einen 'CASE'? Warum nicht einfach loswerden, also wird die 'SELECT ID FROM bdg_Expenses WHERE ParentId = 1' kein Ausdruck sein? – outis

+0

Ich habe es vereinfacht. Die When-Anweisung lautet ursprünglich "WHEN @Level = 1 THEN ..." – mahdiahmadirad

Antwort

1

warum ein fall verwenden? können Sie nicht einfach tun

where (@Level = 1 and ExpenseId in (select id from bdg_expenses where parentid = 1)) or 
     (@Level <> 1 and ExpenseId in (select id from bdg_expenses where id = 1))