I have the following SQL statement:
SELECT 420, DueDate, ISNULL(Amount, 0)
FROM Payments
WHERE CurveID = ? AND DueDate >= ?;
which I am executing using Delphi 2010 and TADODataset. The statement executes correctly except that the evaluation of the clause DueDate >= ? is not correct. If I pass in Date() or Now() for second parameter I get values returned with dates earlier than today in the DueDate field.
In SSMS, I can run this SQL query successfully if I replace the date parameter with a string in the format ‘2011-09-09’ like this:
SELECT 420, DueDate, ISNULL(Amount, 0)
FROM Payments
WHERE CurveID = 19 AND DueDate >= '2011-09-09';
However, back in Delphi, even if I set my parameter value to a stringified version of the date in this format, I get the incorrect results (presumably because TADODataset is correctly converting the string back into a date).
What must I do, short of building dynamic SQL with the date hard-coded into the SQL to get this to evaluate correctly?
Here is the solution to my problem:
I was using TADOCommand to execute the SQL against the database (not TADODataset as I mistakenly wrote in the question, oops). Switching to TADOQuery solved the problem. The execute SQL interface is different between the two classes and TADOQuery unequivocally looks at the Parameters property of the dataset for it’s parameters. TADOCommand has a Parameters property, but also accept an array of Parameter as an argument to the .Execute() method.
With TQuery my parameters are correctly typed without additional effort on my part. This works with position or named parameters.