Search code examples
sql-serverdelphisqlparameter

Get Params of a QueryString and their datatypes


Let's say you have an delphi interface that has a TEdit where an user types and SQL Query

SELECT * FROM X WHERE   X.A = :A and  X.B = :B and X.C= :C

Is there a way to get the types of the :A,:B,:C Params? I managed to get the name of the Params. using this code

procedure TfrmA.Button1Click(Sender: TObject);
var
    queryInput, queryOutput , aux : string;
    pozitie : Integer;
    param : string;
    Ch,Chx : Char;
begin
    queryInput :=  TEdit.Text;
    queryOutput := StringReplace(queryInput,':','@',[rfReplaceAll, rfIgnoreCase]);
    aux := Copy(queryOutput,AnsiPos('@',queryOutput),200);
    while Length(aux ) > 0 do   begin
    if   AnsiPos('@',aux ) = 0 then break;
    for Ch in aux do
    begin
      if Ch = ' ' then begin
      param := param + ' ';
      break
      end else param := param + Ch;
    end;
    ShowMessage(param);
    test := Copy(test,2,200);
    test := Copy(test,AnsiPos('@',test),200);
    end;
end;

Param string will containt now : @A @B @C

Is there anyway I can find out the datatype of the params?


Solution

  • If you are able to use a Adodataset you might use this to get the needed informations. uses TypInfo;

    procedure TForm5.Button1Click(Sender: TObject);
    var
     i:Integer;
    begin
      Ads.CommandText := 'Update Adressen set geboren=:birthdate where name=:n and ID=:i';
      Memo1.Lines.Clear;
      for I := 0 to Ads.Parameters.Count - 1 do
          begin
             Memo1.Lines.Add(Ads.Parameters[i].Name + ' : ' + GetEnumName(TypeInfo(TDataType), Integer(Ads.Parameters[i].DataType)) + ' : '+ IntToStr(Ads.Parameters[i].Size) )
          end;
    end;
    

    The Output of the example will be:

    birthdate : ftDateTime : 16
    n : ftWideString : 40
    i : ftInteger : 4