Solved

SQL Statement Problem

Posted on 2006-11-02
12
171 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

18 Experts available now in Live!

Get 1:1 Help Now