I am trying to create a stored procedure which will use one of the optional parameters provided. In the code below they are @nStudentId and @nStudentIds set to NULL initially. Only one of them will be sent when the proc is called. When @nStudentIds are sent they will come-in as comma separated values.
CREATE PROCEDURE [dbo].[usp_GetStudentReferrals] 
    (
        @ProfessorId BIGINT,
        @nStudentId BIGINT = NULL, 
        @nStudentIds NVARCHAR(999) = NULL
                
    )
AS
BEGIN
    SELECT DISTINCT SR.StudentReferralId
    FROM StudentReferral SR WITH(NOLOCK)
    INNER JOIN PotentialCandidate PC WITH(NOLOCK) ON PC.PotentialCandidateId = SR.StudentId
    WHERE SR.ProfessorId = @nProfessorId
    AND -- this is where I am not able to figure out the logic to use either @nStudentId or @nStudentIds, whichever is passed in.
END 
So when @nStudentId is sent it should be this in the AND
SR.StudentId = @nStudentId 
When @nStudentIds is available I can use 'IN' like so:
SR.StudentId IN (SELECT value FROM STRING_SPLIT @nStudentIds, ','))
The limitation of my knowledge of SQL shows in this IF, which obviously does not work:
AND (if(@nStudentId <> 0 AND @nStudentId <> -1 AND @nStudentId IS NULL)
                    SR.StudentId = @nStudentId;
            else if(@nStudentIds IS NOT NULL)
                    SR.StudentId IN (SELECT value FROM STRING_SPLIT@nStudentIds,','))                       
        )
Any suggestions are appreciated. Thanks in advance.
Regards.
 
     
    