2

I have a stored procedure, uspUser_GetUserDetails, with several parameters. It gets called something like this:

uspUser_GetUserDetails 'LastName','FirstName', 'UserId', 'dtDate', 'DistrictId'

How do I run the stored procedure if the dtDate parameter is null. I tried

uspUser_GetUserDetails 'LastName','FirstName','UserId','','DistrictId'

but it doesn't do anything. Will something like this work?

uspUser_GetUserDetails 'LastName','FirstName','UserId',null,'DistrictId'

This takes forever to execute the query without returning any result. Note that the question is not about how to create a SP with a nullable parameter. My SP works fine from C#, it shows sqlvalue {null} in the parameters. I just want to test it quickly from SQL server management studio and know how to pass null parameter to it in this situation.

3
  • How are you executing the said stored procedure? Commented May 26, 2015 at 2:27
  • 2
    We need to see the SP and the indexes you have on your user table Commented May 26, 2015 at 3:25
  • "This takes forever to execute the query without returning any result.". Yes. Your proc is running with a NULL value but there is now a performance issue. Commented May 26, 2015 at 6:46

2 Answers 2

2
CREATE PROCEDURE MyProcName
    @Parameter1 INT = 1,
    @Parameter2 VARCHAR (100) = 'StringValue',
    @Parameter3 VARCHAR (100) = NULL
AS
BEGIN

END

Use Optional parameter, then no need to pass in calling of procedure.

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

1 Comment

But sometimes it is more convenient to always pass the parameter (e.g. if it can be null or not null).
2

Turns out the syntax is fine for all the SPs I tried later. And there is no performance issue. The problem with uspUser_GetUserDetails 'LastName','FirstName','UserId',null,'DistrictId' before is probably related to "Timeout expired".

Alternatively, EXEC [dbo].[uspSPName] @LastName = N'LastName' @FirstName = N'', @UserId = 0, @dtDate = NULL, @DistrictId = 1

Comments

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.