Solved

SQL Statement Problem

Posted on 2006-11-02
12
170 Views
Last Modified: 2010-04-05
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

0
Comment
Question by:Asmitagoyal
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 9

Expert Comment

by:sun4sunday
Comment Utility
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
0
 
LVL 3

Expert Comment

by:jpedef
Comment Utility
Maybe after Query.Prepared := true; sql shows correct sql.
0
 

Author Comment

by:Asmitagoyal
Comment Utility
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
0
 
LVL 6

Expert Comment

by:wimmeyvaert
Comment Utility
I'm interested too.
I haven't found how to do this, but I could make use of it too.

0
 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 250 total points
Comment Utility
Try this:

function GetSQLWithParamValues(SQL: string; Params: TParams):string;
var i,p: integer;
begin
  for i:= 0 to Params.Count-1 do
  begin
    p:= pos(':'+Params[i].Name, sql);
    while p <> 0 do
    begin
      Delete(sql, p, Length(Params[i].name)+1);
      Insert(Params[i].AsString, sql, p);
      p:= pos(':'+Params[i].Name, sql);
    end;
  end;
  result:= sql;
end;


usage example:
==========
ShowMessage(GetSQLWithParamValues(Query1.SQL.Text, Query1.Params));


Regards
Pierre
0
 
LVL 6

Expert Comment

by:wimmeyvaert
Comment Utility
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.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 14

Expert Comment

by:Pierre Cornelius
Comment Utility
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.
0
 
LVL 6

Expert Comment

by:wimmeyvaert
Comment Utility
Hi PierreC,

You have a point about not using a TADOQuery as a parameter.
0
 
LVL 21

Expert Comment

by:ziolko
Comment Utility
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.
0
 

Author Comment

by:Asmitagoyal
Comment Utility
hi PierreC

thank you ..
your code is working fine.
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
Comment Utility
glad to help, but why a B grade?
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
Comment Utility
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.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now