Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if I call ParamByName for a parameter that doesn't exist?

I'm very new to Delphi and received the following piece of code (left out some irrelevant parts) for which I'm trying to understand what it does:

object SelectCosts: TIBQuery
    SQL.Strings = (
      'SELECT * FROM costs '
      'WHERE code = :code')
    ParamData = <
      item
        DataType = ftUnknown
        Name = 'code'
        ParamType = ptUnknown
      end>
  end

In another file, that query is used, but a parameter that is not defined in the query is added.

DM_HRV.SelectCosts.ParamByName('part').Value := 1;

Does this parameter 'part' change anything about the selection made? In other words: is the SQL query automatically changed into the following?

'SELECT * FROM costs '
  'WHERE code = :code'
  'AND part = :part'
like image 811
Maza89 Avatar asked Jan 16 '12 12:01

Maza89


2 Answers

That means that the SQL statement might be changed at run-time. so when that query is used the SQL already contains AND part = :part.

If the SQL statement does not contain this extra parameter part, an Exception will be raised when assigning ParamByName('part').Value := 1.

I'm assuming you didn't confuse SelectCosts reference (which is in DM_HRV and not other DM).

like image 128
kobik Avatar answered Nov 14 '22 17:11

kobik


The following statement in your post does not add a parameter, it sets its value:

DM_HRV.SelectCosts.ParamByName('part').Value := 1;

To add a parameter at runtime, use CreateParam as follows:

if DM_HRV.SelectCosts.Params.FindParam('Part') = nil then
   DM_HRV.SelectCosts.Params.createParam(ftString, 'Part', ptInput);

The query doesn't get modified automatically, you have to do that yourself.

In your first snip, the ParamType and InputType are not defined, you can change that in the IDE by access the parameter list (Params) property editor, and updating those values.

like image 5
John Easley Avatar answered Nov 14 '22 17:11

John Easley