Passing DBNull to stored procedure parameters

Posts   
 
    
tuppers
User
Posts: 16
Joined: 09-Feb-2009
# Posted on: 10-Feb-2009 11:59:53   

I am currently experiencing the same issue as in thread http://www.llblgen.com/tinyforum/NewThread.aspx?ForumID=10 using v2.6 with the standard set of templates against SQL Server.

The exact problem is this:

I have a stored proc with the following parameters:

CREATE PROCEDURE [dbo].[Search] ( @SearchType BIT, @Name VARCHAR(100) = null, @SecForename VARCHAR(100) = null, @SecSurname VARCHAR(100) = null, @CountyID INT, @UniqueID INT, @SecID INT, @AffiliationNum VARCHAR(100) = NULL, @ViewingCountyID INT, @ShowAffiliatedOnly BIT = 0 )

Where @CountyID can be null but has no default value.

If I mark the parameter as nullable and pass null into the auto generated method the following SQL is executed:

exec [Onside].[dbo].[usp_Search] @SearchType=0,@Name='kent',@SecForename=default,@SecSurname=default,@CountyID= default,@UniqueID=default,@SecID=default,@AffiliationNum=default,@ViewingCountyID=1234,@ShowAffiliatedOnly=1 go

Predictably this throws the exception “Procedure or function 'usp_Search' expects parameter '@CountyID', which was not supplied.” as “@CountyID= default“ is requesting the default value is used which has not been specified. What I need to happen is for “@CountyID=default” to be changed to “@CountyID=null”.

Unfortunately I am not able to update the procs as they are out of my control.

Any help would be gratefully received.

Thanks.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 10-Feb-2009 21:00:49   

The solution is to modify the templates to allow null to be passed as the value of the paramter. Please see this thread for a description of how to do this.

Matt

tuppers
User
Posts: 16
Joined: 09-Feb-2009
# Posted on: 11-Feb-2009 13:33:46   

Thanks for your response Matt.

I had seen this thread but was hoping there may have been a more elegant solution as losing type safety and (as a result) type prompting for developers unfamiliar with the stored procedure is not a great option.