Now that C# supports optional parameters, is there a way to write SQL CLR stored procedures so that publishing from Visual Studio will create the stored procedures in SQL server using optional parameters where defined in C#?
The only way to do it in the past was by manually writing wrapper functions:
Default parameter values/optional parameters for .NET stored procedures in SQL Server 2005
I'd rather avoid that as it requires writing the interface twice, as well as maintaining two interfaces when things change.
Example:
[Microsoft.SqlServer.Server.SqlProcedure]
public static void help(string FunctionName = "")
{
SqlPipe pipe = SqlContext.Pipe;
pipe.Send("help> " + FunctionName + "<");
}
Publishing from VS2013 yields (in the generated publish script):
CREATE PROCEDURE [dbo].[help] @FunctionName [nvarchar](MAX)
AS EXTERNAL NAME [YOURDB].[StoredProcedures].[help];
Running it:
exec help
Procedure or function 'help' expects parameter '@FunctionName', which was not supplied.
However, if you manually write the procedure creation as so:
drop procedure [help]
CREATE PROCEDURE [dbo].[help]
( @FunctionName nvarchar(100) = null)
AS EXTERNAL NAME [YOURDB].[StoredProcedures].[help];
exec help
help> <
....it now runs as expected.
Visual Studio has all the information it needs to publish parameters as optional now that C# supports them, so either I'm doing something wrong, or they simply haven't updated the publishing implementation to recognize optional parameters.