Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In TransactSQL, is it possible to create a function that takes a variable number of parameters (optional parameters)?

Yes I know I can do this with stored procedures. I'm wondering if it's possible with functions.

like image 448
Mark A. Donohoe Avatar asked Apr 19 '11 02:04

Mark A. Donohoe


People also ask

How do I set optional parameters in SQL?

A parameter is considered optional if the parameter has a default value specified when it is declared. It is not necessary to provide a value for an optional parameter in a procedure call. The default value of a parameter is used when: No value for the parameter is specified in the procedure call.

How can we make input parameter optional in stored procedure in SQL Server?

It is better to use the " = NULL" if you are adding a new optional parameter to an existing stored proc. The reason is, you may not be aware of ALL the code that calls this proc. Hence, unless you make it optional using the " = NULL", for all the places that you may have missed to pass in a value, it will break.

Which hint do you use to execute a query using a specific query plan?

SQL Server provides query hints in which you can force query optimizer to use a particular operator in the execution plan. To use a specific operator, SQL Server might need to change other operators as well.

What is option optimize for unknown?

The 'Optimize for Unknown' feature follows the premise that trying to get a consistent execution time for a given set of parameters and re-using a stable execution plan is better than spending CPU to compile a special, unique flower of an execution plan every time a query runs.


1 Answers

Not like in a stored proc - a parameter still needs to be placed in each argument position, but you can specify that it use the default from the declaration:

SELECT yourschema.yourudf(x, y, z, DEFAULT, a, b, c, DEFAULT)
like image 162
Cade Roux Avatar answered Oct 13 '22 14:10

Cade Roux