Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Statement Problem

Posted on 2006-11-02
12
Medium Priority
?
179 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 750 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

Technology Partners: 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

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 Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

773 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