I'm having an error when I try to execute this query.
This is the error (Msg 156, Level 15, State 1, Procedure SP_GetAllProducts, Line 43 Incorrect syntax near the keyword 'as'.)
and these are my query 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
You are missing a closing bracket, see the same line in the IF block, it has three closing brackets whereas the else part has only two!
(P.ProductQuantity - Isnull(Sum(CP.TotalProduct), 0) ) ) as AvailableStock
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.