0

Function A, scalar, returns smallint (a year)
Function B, scalar, returns smallint (a month)
Function C, table, displays users based on year and month.

They work fine.

I would like to set default parameter of Function C to come from Func A & B like:

@year smallint = SELECT FunctA(), @month smallint = SELECT FunctB()

Is there anyway around this?

We are on Azure SQL Managed Instance.

4
  • What RDBMS are you using? Commented Dec 31, 2020 at 20:04
  • Azure SQL. I added to above Commented Dec 31, 2020 at 20:56
  • I added the azure-sql-database tag. In the future, tagging your question accurately helps you because it attracts the attention of the people most able to give you good answers. Commented Dec 31, 2020 at 21:16
  • Don't use scalars unless you're on SQL2019 Commented Dec 31, 2020 at 22:36

1 Answer 1

1

On thing you could use is something like

IF @year IS NULL
BEGIN
  SET @year = functa();
END;

IF @month IS NULL
BEGIN
  SET @month = functb();
END;

(or whatever the analog syntax is in the DBMS you didn't disclose).
And when you want the defaults, pass NULLs as parameters.

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

2 Comments

Tried that. Not allowed in table function. Unless I am putting the 'IF' in the wrong place
@Amommy It is allowed in a multistep table-valued function. It is not allowed in an inline table-valued function. If you have an inline table-valued function, you can use isnull(@year, functa()) everywhere where @year is expected, but that is likely to kill performance. You can play with various ways to work around that, such as outer apply (select isnull(@year, functa())) as actual_value(year), but it is not guaranteed to perform better.

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.