ARV
ARV

Reputation: 1141

SQL stored procedure variable null checking

Below is part of a stored procedure that I have, it would be of great help if you can point me why is that even when @DefaultID is null after executing the select statement it does not enter the if condition, is there are different method I have to follow to assign the value to @DefaultID for it to be available for null checking.

Thank you in advance.

DECLARE @DefaultID varchar(25)
DECLARE @ISDefault varchar(1)

SELECT @DefaultID = (SELECT TABLE1.DEFID as DEFID
                     FROM TABLE1
                     WHERE TABLE1.ID = '123')

IF (@DefaultID = '')
    SET @ISDefault = '1'
ELSE
    SET @ISDefault = '0'

Upvotes: 8

Views: 54716

Answers (4)

TechDo
TechDo

Reputation: 18629

Please check using

IF ISNULL(@DefaultID, '') = '' instead of IF(@DefaultID = '')

I guess you are using MS Sql server.

Upvotes: 3

AnandPhadke
AnandPhadke

Reputation: 13496

IF(len(@DefaultID) > 0)
 SET @ISDefault = '1'
ELSE
    SET @ISDefault = '0'

Upvotes: 0

Pranav
Pranav

Reputation: 8871

simply use this :-

  IF(@DefaultID is NULL)

Upvotes: 4

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79929

Use

IF @DefaultID IS NULL

Instead of IF @DefaultID =''

NULL and '' are two different things.

Upvotes: 16

Related Questions