Solved

SQL Statement Problem

Posted on 2006-11-02
12
174 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
ID: 17864717
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
ID: 17864774
Maybe after Query.Prepared := true; sql shows correct sql.
0
 

Author Comment

by:Asmitagoyal
ID: 17864860
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 17864976
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
ID: 17866847
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
ID: 17867283
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
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 17872536
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
ID: 17879353
Hi PierreC,

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

Expert Comment

by:ziolko
ID: 17879881
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
ID: 17880141
hi PierreC

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

Expert Comment

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

Expert Comment

by:Pierre Cornelius
ID: 17883197
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

735 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