4

I have a stored procedure being called from an .aspx.cs page. I have a parameter that sometimes cannot be sent when the sproc is called. Because of this I'm doing the following:

 IF @variable is null
     BEGIN
         ...do this...
     END
 Else 
         ...do that...

My problem is in the IF statement. As far as I can tell when I use any of the following:

  • if @parameterVariable = null
  • if @parameterVariable = ''
  • if @parameterVariable <= 0

Nothing happens!? When I debug the sproc in SSMS I find that (even though the parameter is empty (no user selection)) that the cursor goes to and runs the code in the ELSE statement. Am I doing something wrong?

Thanks!

5
  • Is @parameterVariable actually null ? Commented Nov 16, 2012 at 19:25
  • I'm using Declare @variable and set @variable = null Typically however the parameter will contain a value. However I found and am correcting the sproc to manage when the user doesn't make the selection and the parameter isn't defined. Commented Nov 16, 2012 at 19:27
  • 1
    You probably have an issue somewhere else, this works fine. See: sqlfiddle.com/#!3/d41d8/6290 Commented Nov 16, 2012 at 19:28
  • can you post the code for your stored procedure? Commented Nov 16, 2012 at 19:37
  • All those comparisons with NULL (except for IS NULL) result in unknown not true or false. Read up on three valued logic. Commented Nov 16, 2012 at 20:02

4 Answers 4

5

use optional parameter:

 CREATE PROCEDURE uspTest
    @param1 varchar(50) = null,

AS
    BEGIN
        SELECT col1, col2
        FROM Table1
        WHERE
                ((@Param1 IS NULL) OR (col1 = @Param1)) 
    END
Sign up to request clarification or add additional context in comments.

1 Comment

This works for other scenarios, no doubt ...and thank you ElenaDBA... however the ((@Param1 IS NULL is the exact issue that caused me to need to handle the entire rest of the procedure in an if else scenario. I'll spare you the details, thank you again!
3

if @parameterVariable = null is wrong. Change it to if @parameterVariable IS NULL.

Here is a SQL Fiddle demonstrating this: http://www.sqlfiddle.com/#!6/6cb42/1

1 Comment

Dominic I added a SQL Fiddle as an example.
1

when debugging in SMSS, you must check the box that says "Pass null value". otherwise your value is an empty string or somesuch.

I use the pattern you suggest all the time, and it works well for me.

3 Comments

Where can I find the "Pass null value" checkbox?
Right click a stored proc, and select 'Execute Stored procedure', and fill in the arguments you want.
Thats a great tool. Thanks, still troubleshooting, not sure if this is what I needed yet. (yeppers I'm still noob)
1

i suggest you to read this page => ANSI NULLS

actually if @var = null is not wrong, everything depends on the value of ANSI_NULLS :)

4 Comments

When SET ANSI_NULLS is OFF, the Equals (=) and Not Equal To (<>) comparison operators do not follow the ISO standard. A SELECT statement that uses WHERE column_name = NULL returns the rows that have null values in column_name. A SELECT statement that uses WHERE column_name <> NULL returns the rows that have nonnull values in the column. Also, a SELECT statement that uses WHERE column_name <> XYZ_value returns all rows that are not XYZ_value and that are not NULL. I would argue that this qualifies for "wrong". But really, to each their own!
That is why I used quotes. I'm in no way making a moral judgement. But I do think one should use ANSI SQL as much as possible. Just because it is there does not mean it is "good".
This option goes back to Sybase days. It is deprecated and as your link says "in a future version of SQL Server, ANSI_NULLS will always be ON"
ok :) then I admit that it should be "wrong" without quotes :)

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.