Link to home
Start Free TrialLog in
Avatar of Asmitagoyal
Asmitagoyal

asked on

SQL Statement Problem

hi all
  i am working on delphi7 and backend oracle 8i.
the problem which i am facing is that how to get the query which has been written in SQL.ADD() statement.
eg:
SQL.Add('Insert Into table1 (a,b,c) Values(:a,:b,:c)');
Parambyname('a').ASString := 'XYZ';
Parambyname('b').ASString := 'ABC';
Parambyname('b').ASString := 'DEF';

i wrote statement
   Str := SQL.GetText;
It returned
'Insert Into table1 (a,b,c) Values(:a,:b,:c)'
now what i want is instead of (:a,:b,:c) i want the actual values i.e

'Insert Into table1 (a,b,c) Values('XYZ','ABC','DEF')'

i hope the question is understood.
thank you

Avatar of sun4sunday
sun4sunday
Flag of India image

if you can pass it with out parameters you can do it in other way

var
sqlstring : String;
begin
sqlstring := 'Insert Into table1 (a,b,c) Values (' +''''+ edit2.Text+'''' + ', '+''''+ edit2.Text+  ')' +'''' ;
SHowmessage(sqlstring);

QuotedStr function can use for the quotes

sun4sunday
Maybe after Query.Prepared := true; sql shows correct sql.
Avatar of Asmitagoyal
Asmitagoyal

ASKER

hi
i had thought of the above but the problem is that the count of fields in the tables is very large .
Is there any other solution apart from this .......
and
Query.prepared := true    did not work

thank you
I'm interested too.
I haven't found how to do this, but I could make use of it too.

ASKER CERTIFIED SOLUTION
Avatar of Pierre Cornelius
Pierre Cornelius
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hm,

Reading PierreC's comment, I made another function which does basically the same, but only needs a TADOQUery as parameter:

function GetSQLWithParamValues(qry: TADOQuery): String;
var
  i: Integer;
  sTmp: String;
begin
  sTmp := qry.SQL.Text;
  for i:=0 to qry.Parameters.Count-1 do
  begin
    sTmp := StringReplace( sTmp, ':'+qry.Parameters[i].Name, qry.Parameters[i].Value, [] );
  end;
  Result := sTmp;
end;


The function just replaces the parameter-name by its parameter-value.
I would have used the rfReplaceAll flag in the StringReplace function.

The reason I used the SQL and params as parameters is to make the function dataset independant e.g. TADOQuery, TQuery, TZQuery, etc.
Hi PierreC,

You have a point about not using a TADOQuery as a parameter.
1. first add params then set SQL Command property
2. You will not get SQL command with params replaced by actual values from ADO components reason is very simple, what if params point to BLOB values how wuld You like it to bo parsed into SQL command?

ziolko.
hi PierreC

thank you ..
your code is working fine.
glad to help, but why a B grade?
a quote from the help files:

Grading at Experts Exchange is not like school. It's more like the "10-point Must" system in professional boxing; in other words, an answer is worth an A, unless it doesn't resolve your issue. If it requires you to do a little more research, or figure out one more piece of code, then it's worth a B. If you think it's not worth a B, the custom is to offer the Experts an opportunity to earn a better grade.