Quantcast
Channel: SCN: Message List - SAP SQL Anywhere
Viewing all articles
Browse latest Browse all 2182

SELECT when one column might be NULL

$
0
0

Hi,

 

Inside of a stored procedure I'm doing the following

 

SELECT T.LineId,T.Qty

FROM TransactionRows AS T

WHERE T.ArticleId = @ArticleId AND T.LotNumber = @LotNumber;

 

 

@ArticleId contains the "article number" that I want to select, and @LotNumber contains the "lot number".

 

 

The problem is that @LotNumber might be an actual value, OR IT MIGHT BE NULL.

 

If @LotNumber is NULL, the SELECT above FAILS.   However, if it is NOT NULL, it doesn't fail.

 

After many years working with databases I hadn't realized this until now ....

 

 

To get the correct row(s), if @LotNumber is null, I would have to write:

 

SELECT T.LineId,T.Qty

FROM TransactionRows AS T

WHERE T.ArticleId = @ArticleId AND T.LotNumber IS NULL

 

 

The complete statement would be:

 

IF @LotNumber IS NULL THEN

  SELECT T.LineId,T.Qty

  FROM TransactionRows AS T

  WHERE T.ArticleId = @ArticleId AND T.LotNumber IS NULL;

ELSE

  SELECT T.LineId,T.Qty

  FROM TransactionRows AS T

  WHERE T.ArticleId = @ArticleId AND T.LotNumber = @LotNumber;

END IF;

 

 

Is there a more elegant way of doing this?

 

Thanks,

Edgard


Viewing all articles
Browse latest Browse all 2182

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>