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

Re: SELECT when one column might be NULL

$
0
0

Without further information I think the shortest form besides Koichi's ansinull approach would be:

  SELECT T.LineId,T.Qty

  FROM TransactionRows AS T

  WHERE T.ArticleId = @ArticleId

    AND (T.LotNumber = @LotNumber or (T.LotNumber IS NULL and @LotNumber IS NULL))

If there is an out-of-range value, you can shorten that to (assuming -7 is out-of-range)

  SELECT T.LineId,T.Qty

  FROM TransactionRows AS T

  WHERE T.ArticleId = @ArticleId

    AND ISNULL (T.LotNumber, -7) = ISNULL (@LotNumber, -7)

If you need the restriction on T.LotNumber to be an indexed SARG for performance reasons, either of these approaches will fail to do the job. Thern the options are:

- use the ansinull approach or

- if there is an out-of-range value

  -- create an index on the ISNULL expression instead of the plain column value, which would improve the variant with the ISNULL function calls or

  -- populate the rows with this value instead of NULL. You can continue to use the ISNULL (@LotNumber, ...) expression for the parameter, the optimizer only requires a suitable SARG on the column side of the condition.

I expect the expression index approach the one with the lightest impact on the behavior of existing operations.

 

HTH

 

Volker


Viewing all articles
Browse latest Browse all 2182

Trending Articles



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