I have a Table-Valued User-Defined Functions named "GetRelatedUsersIds", which in turn return all sub users (including itself) for any particular user if any.
And I have a scenario where I want this function to be used in where in clause as given below:-
Create procedure [ABC]
(
@AddedBy Int,
@LoggedUser int=0
)
as
SELECT * FROM MyTable
where AddedBy in (case when @AddedBy =0 then (select UserId from dbo.GetRelatedUsersIds(@LoggedUser)) else @AddedBy end)
Here I looking for variable @AddedBy if its 0 then I will go for variable @LoggedUser.
CREATE function [dbo].[GetRelatedUsersIds]
(
@ccManagerId INT
)
returns @tableUsersId table(UserId int)
AS
.... code
But as UDF returns a table so I am getting an error of "Subquery returned more than 1 value".
How to solve this issue, TIA.