I'm running Python pyodbc against SQL Server. I have a very complex query that I minimize here as
DECLARE @mydate DATETIME = '2021-02-08'
SELECT *
FROM atable
WHERE adate = @mydate AND afield = ?
On the Python side I'm executing the usual
crsr.execute(sql, field)
What is baffling me is that it returns all the results and it ignores the condition but with a strange order so that when I plot the graph it is very confused! Why does it happen?
(edit Of course I should have added an afield = field
with no other errorsORDER BY
)
I have already fixed the code with an initial
DECLARE @myfield VARCHAR(32) = ?
followed by the where condition ending with afield=@myfield
and now it works as expected: the order is the normal one even if I have not introduced an explicit ORDER BY
.
In other words, aside from the fact that the final correct fix is adding an ORDER BY
, e.g.
SELECT *
FROM atable
WHERE adate = @mydate AND afield = ?
ORDER BY id
I'm wondering why introducing the above said change was enough to change the order.