2016-05-02 15 views
-1

Ich habe einen Fehler, wenn ich versuche, diese Abfrage auszuführen.Wie kann ich diesen Fehler mit SQL Server-Abfrage beheben

Dies ist der Fehler (Msg 156, Ebene 15, Status 1, Prozedur SP_GetAllProducts, Linie 43 falsche Syntax nahe dem Schlüsselwort 'als').

und das ist meine Abfrage-Codes

create procedure [dbo].[SP_GetAllProducts] (@CategoryID INT) 
as 
    begin 
     begin try 
      if(@CategoryID <> 0) 
       begin 
        select * 
        from (select P.CategoryID, 
           P.ProductID, 
           P.Name, 
           P.Price, 
           P.ImageUrl, 
           C.CategoryName, 
           P.ProductQuantity, 
           Isnull(Sum(CP.TotalProduct), 0)      as ProductSold, 
           (P.ProductQuantity - Isnull(Sum(CP.TotalProduct), 0)) as AvailableStock 
          from Products P 
           inner join Category C 
              on C.CategoryID = P.CategoryID 
           left join  CustomerProducts CP 
              on CP.ProductID = P.ProductID 
          group by P.ProductID, 
            P.Name, 
            P.Price, 
            P.ImageUrl, 
            C.CategoryName, 
            P.ProductQuantity, 
            P.CategoryID) StockTable 
          where AvailableStock > 0 
           and CategoryID = @CategoryID 
       end 
      else 
       begin 
        select * 
        from (select P.CategoryID, 
           P.ProductID, 
           P.Name, 
           P.Price, 
           P.ImageUrl, 
           C.CategoryName, 
           P.ProductQuantity, 
           Isnull(Sum(CP.TotalProduct), 0)      as ProductSold, 
           (P.ProductQuantity - Isnull(Sum(CP.TotalProduct), 0) as AvailableStock 
          from Products P 
           inner join Category C 
             on C.CategoryID = P.CategoryID 
           left join CustomerProducts CP 
             on CP.ProductID = P.ProductID 
          group by P.ProductID, 
            P.Name, 
            P.Price, 
            P.ImageUrl, 
            C.CategoryName, 
            P.ProductQuantity, 
            P.CategoryID) StockTable 
          where AvailableStock > 0 
       end 
      end try 

      begin catch 
       print('Error occurd') 
      end catch 
    end 
+0

Welche ist die Linie 43? (Bitte streichen Sie den Rest des Verfahrens) –

Antwort

1

Ihnen fehlt eine schließende Klammer, siehe die gleiche Zeile im IF-Block, sie hat drei schließende Klammern, während der else-Teil nur zwei hat!

(P.ProductQuantity - Isnull (Sum (CP.TotalProduct), 0)) ) als availablestock

+0

Bro vielen Dank –

Verwandte Themen