Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
create procedure DeleteProductID
@productid int = NOTNULL,
@forceDelete int,
@posCount int
as
select * from Sales.OrderDetails
if @forceDelete = 1
	delete from Sales.OrderDetails where orderid = @productid
else if @forceDelete = 0
	delete from Sales.OrderDetails where count(productid) < @posCount
	go


Msg 147, Level 15, State 1, Procedure DeleteProductID, Line 11 [Batch Start Line 0]
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.


What I have tried:

declare @CountOrderId int
select @CountOrderId = count(productid)
from Sales.OrderDetails
group by productid

create procedure DeleteProductID
@productid int = NOTNULL,
@forceDelete int,
@posCount int,
as
select * from Sales.OrderDetails
if @forceDelete = 1
	delete from Sales.OrderDetails where orderid = @productid
else if @forceDelete = 0
	delete from Sales.OrderDetails where @CountOrderId < @posCount
Posted
Updated 13-Nov-22 8:31am

1 solution

Read the error message: it tells you what you need to know.
If you then google the error message, it will take you to many discussions of the problem, including this one: SQL SERVER - Fix: Error 147 An aggregate may not appear in the WHERE clause - SQL Authority with Pinal Dave[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900