T SQL - null variable
Solution 1
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
Solution 2
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
Solution 3
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.
Solution 4
i suggest you to read this page => ANSI NULLS
actually if @var = null
is not wrong, everything depends on the value of ANSI_NULLS :)
Computer Guy
Updated on June 27, 2022Comments
-
Computer Guy about 2 years
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!
-
asawyer over 11 yearsIs
@parameterVariable
actuallynull
? -
Computer Guy over 11 yearsI'm using
Declare @variable
andset @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. -
asawyer over 11 yearsYou probably have an issue somewhere else, this works fine. See: sqlfiddle.com/#!3/d41d8/6290
-
Coding Duchess over 11 yearscan you post the code for your stored procedure?
-
Martin Smith over 11 yearsAll those comparisons with
NULL
(except forIS NULL
) result inunknown
nottrue
orfalse
. Read up on three valued logic.
-
Computer Guy over 11 yearsWhere can I find the "Pass null value" checkbox?
-
Frank Thomas over 11 yearsRight click a stored proc, and select 'Execute Stored procedure', and fill in the arguments you want.
-
Computer Guy over 11 yearsThats a great tool. Thanks, still troubleshooting, not sure if this is what I needed yet. (yeppers I'm still noob)
-
Robert Kaucher over 11 yearsDominic I added a SQL Fiddle as an example.
-
Robert Kaucher over 11 years
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! -
Robert Kaucher over 11 yearsThat 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".
-
Computer Guy over 11 yearsThis 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! -
Martin Smith over 11 yearsThis 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"
-
fnurglewitz over 11 yearsok :) then I admit that it should be "wrong" without quotes :)