Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NULL DOESN'T BEHAVE IN STORED PROCEDURES

Status
Not open for further replies.

Rob1412

Programmer
Sep 10, 2003
9
VE
I have this function but the if does'nt seem to be working correctly , I believe it's because of the comparison with null, I've had this problem before but gotten around it using the 2nd method which i'm not really fond of.

---------------method wanted
ALTER FUNCTION RETURN_BLOQUEO
(@prmMsisdsn VARCHAR(13),@prmServicio VARCHAR(13))
RETURNS int
AS
BEGIN


IF (SELECT MSISDN FROM dbo.View_Blacklist_Aplication
WHERE MSISDN=@prmMsisdsn AND SERVICIO=@prmServicio) = NULL

RETURN 0

RETURN 1
END



---------------------second method
ALTER FUNCTION RETORNAR_BLOQUEO (@prmMsisdsn VARCHAR(13),@prmServicio VARCHAR(13))
RETURNS int
AS

BEGIN

DECLARE @MSISDN VARCHAR(13)

set @MSISDN=(SELECT MSISDN FROM dbo.View_Blacklist_Aplication
WHERE MSISDN=@prmMsisdsn AND SERVICIO=@prmServicio)

IF ISNULL(@MSISDN,'NULL') = 'NULL'
RETURN 0


RETURN 1
END



 
use IS NULL instead of = NULL


---------------method wanted
ALTER FUNCTION RETURN_BLOQUEO
(@prmMsisdsn VARCHAR(13),@prmServicio VARCHAR(13))
RETURNS int
AS
BEGIN


IF (SELECT MSISDN FROM dbo.View_Blacklist_Aplication
WHERE MSISDN=@prmMsisdsn AND SERVICIO=@prmServicio) IS NULL

RETURN 0

RETURN 1
END



---------------------second method
ALTER FUNCTION RETORNAR_BLOQUEO (@prmMsisdsn VARCHAR(13),@prmServicio VARCHAR(13))
RETURNS int
AS

BEGIN

DECLARE @MSISDN VARCHAR(13)

set @MSISDN=(SELECT MSISDN FROM dbo.View_Blacklist_Aplication
WHERE MSISDN=@prmMsisdsn AND SERVICIO=@prmServicio)

IF ISNULL(@MSISDN,'NULL') = 'NULL'
RETURN 0


RETURN 1
END


cheyney
 
a simple search in Books Online for "null" would lead you to "null values", and underneath that there is an entry entitled "comparison search conditions"...alot of problems posted to this forum can be easily resolved by spending 5 minutes with the documentation :)

Cheyney
 
Remember this:

= NULL looks for a word (string)

ISNULL looks for the NULL value

-SQLBill
 
As far as my knowledge goes it all depends on the setting
of ANSI NULL. using IS NULL for null values will always work but = NULL will work only based on the ANSI NULL settings.

Correct me if I am wrong.

Thanks,
Pankaj
 
Pankaj, you are correct. = NULL will only work if ANSI_NULLS are OFF.

In any case, a much better method of checking in this scenario is to use EXISTS:

Code:
IF NOT EXISTS (
  SELECT MSISDN
  FROM dbo.View_Blacklist_Aplication
  WHERE MSISDN = @prmMsisdsn
    AND SERVICIO = @prmServicio
)
  RETURN 0
ELSE
  RETURN 1

--James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top