Michiel T
Michiel T

Reputation: 537

How do I output the value of a parameter in SQL after it has been inserted?

Is it possible to retrieve the SQL statement with the values of parameters after it has been set and inserted into the SQL component?

SQL Statement:

SELECT * FROM SomeTable
WHERE SomeColumn = :pSomeValue;

Code:

procedure LoadParams(aValue: string);
begin
  Qry.Close;
  Qry.ParamByName('pSomeValue').AsString := aValue;
  MessageDlg(Qry.SQL.Text, mtInformation, [mbOK], 0); // this does not show the value but the parameter name.     
  Qry.Open;
end;

I want to be able to see the statement before it is opened but when I try this now I get the param name instead of the value.

Upvotes: 0

Views: 1951

Answers (2)

Pieter B
Pieter B

Reputation: 1967

One thing you can do is duplicate the parameter and put it in the SELECT:

SELECT :pSomeValueDuplicate AS paraminput, * FROM SomeTable
WHERE SomeColumn = :pSomeValue;

Qry.ParamByName('pSomeValue').AsString := aValue;
Qry.ParamByName('pSomeValueDuplicate').Value := Qry.ParamByName('pSomeValue').Value;

Then you can check the field: paraminput for what you used as input. Somehow I can't get Delphi to use the same parameter twice in 1 query.

SELECT 'SELECT * FROM SomeTable WHERE SomeColumn ='+ :pSomeValueduplicate AS thesqlinput,* FROM SomeTable WHERE SomeColumn = :pSomeValue;

Actually you can do it with just the one parameter:

SELECT 'SELECT * FROM SomeTable WHERE SomeColumn ='+ SomeColumn AS thesqlinput,* FROM SomeTable WHERE SomeColumn = :pSomeValue;

Upvotes: 1

Query parameters are normally substituted in the DBMS, i.e. the values are sent over the connection separately, and not as part of the SQL statement. If you wish to see the SQL and the values together, the DB's logs might help you with that.

Upvotes: 4

Related Questions