this is my query
Create FUNCTION [dbo].[CountUses](@couponid INT)
RETURNS INT
AS
BEGIN
RETURN
(
SELECT c.Id,
c.Name,
c.CreateDate,
Count(cu.id) NofUses
FROM Coupon as c
JOIN CouponUse as cu
ON c.id = cu.couponid
GROUP BY c.Id,
c.Name,
c.CreateDate
)
END
its giving the error Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. where is the problem ?
Aside from the main answer, I’d also appreciate any comments you may have about optimizing my query…
If you want your function to return more than one value, then you need to look at Table-Valued Functions.
These types of functions return a table and not just one value. Your current function is set up as a scalar function so it can only return one value.
If you want a scalar value – let’s say just the
countthen your function will be similar to this:If you intention is to return a table of data, then your function will be similar to this: