2

I have a need to create a table valued function in SQL Server. The function has input parameters, but all of them have default values. If I want to SELECT from the function using default values for every input parameter, I cannot figure out how to call the function.

Below is the CREATE FUNCTION as well as my four attempts to call it without providing input-parameter values. All four attempts failed:

CREATE FUNCTION fx_my_table_valued_function
(@my_input_parameter AS VARCHAR(30) = 1)
RETURNS TABLE
RETURN SELECT 'x' AS my_column;

select * from fx_my_table_valued_function;
select * from fx_my_table_valued_function();
select * from [fx_my_table_valued_function];
select * from [fx_my_table_valued_function]();

Thanks in advance for any help.

1
  • 1
    You have to use the DEFAULT keyword. Which is not very useful. rextester.com/ILQTK99749. There is no possibility to omit the parameter entirely as in other languages. Or as for stored procedures. Commented Dec 22, 2017 at 21:44

1 Answer 1

2

Try this:

CREATE FUNCTION fx_my_table_valued_function
(@my_input_parameter AS VARCHAR(30) = 1)
RETURNS @tbl TABLE (my_column VARCHAR(max))
AS
BEGIN
RETURN 
END

Then:

SELECT * FROM fx_my_table_valued_function(default)

SQL Fiddle

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.