I have been using SQL queries with parameters on Lazarus for some time now but I'm stumped with this error. It seems pretty basic but I'm just lost.
I have the following Lazarus Pascal code:
strSQL:= 'Select "fCo", "fcFarmID", "fcFlockCode", ' +
'"fcWeekEnding","fcAge", ' +
'(:"pDate" - "fcWeekEnding")/7 as "AgeWeeks" ' +
'From (select "fCo", "FarmID", "FarmName" from "tblFarms" Where "fCo" = :"pp") as f Left Join "tblFlockCodes" ' +
'On "FarmID" = "fcFarmID" ';
dbQuery_FlockCodes.SQL.Text:= strSQL;
dbQuery_FlockCodes.Params.ParamByName('pDate').AsDate := dtWeekEndingDate.Date;
dbQuery_FlockCodes.Params.ParamByName('pp').AsString:= lstCo.Text;
The program reports a run-time error when the last line (with parameter pp) is run. The error is:
Paramater pp not found.
I have queries with multiple params but there's no problem. It's just this one. I even tried renaming the parameter name but I get the same error.
If I use a string literal in place of the parameter or insert a concatenated string like the following, it works just fine.
strSQL:= 'Select "fCo", "fcFarmID", "fcFlockCode", ' +
'"fcWeekEnding","fcAge", ' +
'(:"pDate" - "fcWeekEnding")/7 as "AgeWeeks" ' +
'From (select "fCo", "FarmID", "FarmName" from "tblFarms" Where "fCo" = ' + QuotedStr(lstCo.Text) + ' ) as f Left Join "tblFlockCodes" ' +
In the last example above, lstCo is a drop-down list box. In the whole code, dtWeekEndingDate is a date control.
Perhaps I'm overseeing something. Any idea? Thanks!