I am new to sql programming; trying to develop this function to get rows of clients who have a specific number of visits from a view:
ALTER FUNCTION [dbo].[fn_NumberOfVisit]
(
@nv int
)
RETURNS varchar(500)
AS
BEGIN
DECLARE @ret varchar(500)
select *
from (
select
*,
rn = row_number() over (partition by ClientId order by VisitId)
from
Visit
) activityWithRn
inner join vw_MasterView on vw_MasterView.VisitId = activityWithRn.VisitId
where activityWithRn.rn =@nv
RETURN @ret
END
I get the following error:
Select statements included within a function cannot return data to a client
I would appreciate your support. Thanks in advance.
The error is telling you that your subquery is returning too many rows. You need to return only one row is you are assigning the result to a variable.
Change
To