如何使用SQL Server查询修复此错误

时间:2016-05-02 09:35:26

标签: sql sql-server

我尝试执行此查询时遇到错误。

这是错误 (Msg 156,Level 15,State 1,Procedure SP_GetAllProducts,Line 43 关键字'as'附近的语法不正确。)

这些是我的查询代码

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

1 个答案:

答案 0 :(得分:1)

你错过了一个结束括号,看到IF块中的同一行,它有三个右括号,而else部分只有两个!

(P.ProductQuantity - Isnull(Sum(CP.TotalProduct),0)) as AvailableStock