0

I have come across a strange scenario while executing a query. I was surprised by the way Sql Server was executing the query.

Quote:

SELECT TOP 10 
   MR.MerchantContactFirstName +' '+ MR.MerchantContactLastName 
      AS MerchantName, 
   AISLTrans.DeviceID,AISLTrans.TransactionID, 
   AISLTrans.CardNumber, 
   AISLTrans.TransactionDateTime, 
   AISLTrans.TransactionStatus, 
   AISTRNSTYPE.TransactionType 
      AS TransactionRequestType,AISLTrans.TransactionAmount,
   (AISLTrans.BasePointEarn+AISLTrans.BonusPointEarn) 
      AS 'Point Awarded'
FROM AISLoyaltyTransactions AISLTrans 
   INNER JOIN MerchantRegistration MR 
      ON AISLTrans.MerchantID = MR.MerchantID 
   INNER JOIN AISTransactionTypes AISTRNSTYPE 
      ON AISLTrans.ProcessingCode = AISTRNSTYPE.ProcessingCode 
         AND AISLTrans.TransactionType = AISTRNSTYPE.MessageType 
WHERE **AISLTrans.TransactionType = 0200** 
    AND AISLTrans.ProcessingCode = 071000 
ORDER BY AISLTrans.TransactionDateTime DESC

In above query the field ISLTrans,TransactionType works fine for all the values except 0200 it accepts all interger values though the field type is of varchar.

But when I give the value as 0200 it throws exception saying cannot convert varchar to int.

I am looking for why the query is still executing though I am providing integer value instead of varchar and if it is executing why its not accepting all the integer values which I provide.

1
  • 3
    Make your life easier and fix your code. WHERE AISLTrans.TransactionType = '0200'. Don't rely on implicit conversions. It is terrible for performance and you run into these types of random issues. The reason this happens sometimes is because the execution plan may vary slightly and it can change the order that the predicates are resolved internally. You most likely have some rows in that table where TransactionType is something other than an integer. Commented Jul 17, 2014 at 14:36

1 Answer 1

1

Implement a standard way while giving input.

If the AISLTrans.TransactionType filed type is integer, Use these code

AISLTrans.TransactionType = CAST(0200 AS INT)

else use these code

AISLTrans.TransactionType = '0200'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.