How to use optional parameters in table valued function in sql server? can some one tell me this with some examples?
Optional Parameters in Table Valued Function
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.


Jayraj ChhayaRank #304 · 8.2K reputationPosted Feb 27, 2024, 8:23 AM EST
Hi Aradhana Tripathi,
In SQL Server, table-valued functions can have optional parameters by assigning default values to those parameters. This allows flexibility in function calls where certain parameters can be omitted. Here's an example demonstrating the use of optional parameters in a table-valued function:
CREATE FUNCTION dbo.GetEmployees ( @DepartmentID INT, @IsManager BIT = NULL ) RETURNS TABLE AS RETURN ( SELECT EmployeeID, EmployeeName FROM Employees WHERE DepartmentID = @DepartmentID AND (@IsManager IS NULL OR IsManager = @IsManager) )Above example, the
@IsManagerparameter is optional with a default value ofNULL. When calling the function, you can choose to provide or omit this parameter. If omitted, the function will return all employees in the specified department regardless of their managerial status.