When running a query and specifically calling for a PaymentId, I see it, and when running it generally I cannot find it

Asaf Oren 1 Reputation point
2021-10-31T14:56:31.807+00:00

Hi,
This is a very weird scenario.
When I'm running this, I see the PaymentId:
SELECT a.PaymentId,a.PaymentDate FROM DW_Staging..Payments_Gen2_Payoneer_iACHPayments_Full AS a
where a.FundsOriginType = 1
AND a.PaymentId = '322765709'
AND a.PaymentDate > '2021-08-30 16:03:00.017'
AND a.PaymentDate < '2021-08-30 16:03:20.017'
ORDER BY a.145271-with-322765709.png145272-without-322765709.pngPaymentId

And when I'm running this I cannot:
SELECT a.PaymentId,a.PaymentDate FROM DW_Staging..Payments_Gen2_Payoneer_iACHPayments_Full AS a
where a.FundsOriginType = 1
--AND a.PaymentId = '322765709'
AND a.PaymentDate > '2021-08-30 16:03:00.017'
AND a.PaymentDate < '2021-08-30 16:03:20.017'
ORDER BY a.PaymentId

What can it be?

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
{count} votes

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 128.9K Reputation points MVP Volunteer Moderator
    2021-10-31T17:20:00.947+00:00

    A couple of possible explanations come to mind. The most likely is maybe corruption. That is, a value is present in one index, but not in another. Another possibility is a bug in SQL Server, so that the query plan for the latter query missing the the ID for some reason. I can think of a few more reasons, but they are more esoteric.

    I would recommend that you start with running DBCC CHECKDB(DW_staging) to see what it says. Share the output from this command. Please also share the output of "SELECT @@version".

    2 people found this answer helpful.
    0 comments No comments

  2. Tom Phillips 17,781 Reputation points
    2021-11-01T20:35:28.577+00:00

    As Erland said, this is almost always due to a bad index. Refresh your indexes, and it will likely fix itself.

    Also, make sure you have the most current patches installed. This problem is extremely rare, and is likely an issue with the version you have installed.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.