views:

86

answers:

2

So here's the deal. In our database, we wrap most of our reads (i.e. select statements) in table valued functions for purposes of security and modularity. So I've got a TVF which defines one or more optional parameters.

I believe having a TVF with defaulted parameters mandates the use of the keyword "default" when calling the TVF like so:

select * from fn_SampleTVF(123, DEFAULT, DEFAULT)

That's fine, everything works in the query analyzer, but when it comes time to actually make this request from ADO.NET, I'm not sure how to create a sql parameter that actually puts the word "default" into the rendered sql.

I have something roughly like this now:

String qry = "select * from fn_SampleTVF(@requiredParam, @optionalParam)";

DbCommand command = this.CreateStoreCommand(qry, CommandType.Text);

SqlParameter someRequiredParam = new SqlParameter("@requiredParam", SqlDbType.Int);
someRequiredParam.Value = 123;
command.Parameters.Add(someRequiredParam);

SqlParameter optionalParam = new SqlParameter("@optionalParam", SqlDbType.Int);
optionalParam.Value = >>>> WTF? <<<<
command.Parameters.Add(optionalParam);

So, anybody got any ideas how to pass 'default' to the TVF?

A: 

You can pass Null as the parameter value.

This article shows examples: http://support.microsoft.com/kb/321902

Kevin Buchan
Thanks I'll try that, but I'm fairly certain that passing an explicit null will just override the defaults defined in the tvf definition, making the defaults useless.
Mason
I'd love to hear your findings. I'm amazed that I've never needed to know this before seeing your question. I would still expect Null to work and that DBNull would override the paramater's definition.
Kevin Buchan
Yeah I just checked, that's no good. And the reason is because there's a difference between:select * from fn_SampleTVF(123, DEFAULT)andselect * from fn_SampleTVF(123, null)Thanks though.
Mason
P.S. The problem is that you're actually right. DBNull does indeed override the TVF's parameter definition. What I'm looking to do is USE the TVF's parameter definition.
Mason

related questions