Article View: borland.public.cppbuilder.database.ado
Article #1622Re: Flexible SQL Queries through ADO.
From: Viatcheslav V. Vassiliev
Date: Thu, 03 Jun 2004 13:00
Date: Thu, 03 Jun 2004 13:00
204 lines
7375 bytes
7375 bytes
Do not use ParseSQL.
query->SQL->Text = FlexibleSQLStatement;
query->Parameters->Refresh();
... set parameters.
//------------------------------------------
Regards,
Vassiliev V. V.
http://www.managed-vcl.com - using .Net objects in Delphi for Win32 +
ADO.Net
http://www.oledbdirect.com - The fastest way to access MS SQL Server,
MS Jet (Access) and Interbase (through OLEDB)
"Heinz Ketchup" <heinz_like_the_ketchup@h0tmail.n0spam.c0m.n0spam>
�������/�������� � �������� ���������:
news:40be3a4f$1@newsgroups.borland.com...
>
> Whoops:
> The Below Statement is wrong
> > QueryOperations->Parameters->ParamBYName("Unit4")->Value =
UserDeclared4 = "HY01";
>
> when it should be:
>
> QueryOperations->Parameters->ParamBYName("Unit4")->Value = UserDeclared4;
>
> It was a copy paste mistake into the Post.
>
> Heinz.
>
>
>
>
> "Heinz Ketchup" <heinz_like_the_ketchup@h0tmail.n0spam.c0m.n0spam> wrote:
> >
> >Hi Mark and Viatcheslav,
> >
> >Here is my method. It's rather basic, as I was just trying to test your
example. So here it is.
> >
> >
> >void TStatus6DatabaseModule::FetchUnitHeatRateDetails(TADOQuery*
QueryOperations)
> >{
> >
> >AnsiString FlexibleSQLStatement;
> >AnsiString UserDeclared1, UserDeclared2, UserDeclared3, UserDeclared4;
> >
> > UserDeclared1 = "GN01";
> > UserDeclared2 = "CB03";
> > UserDeclared3 = "RD09";
> > UserDeclared4 = "HY01";
> >
> > QueryOperations->Close();
> > QueryOperations->SQL->Clear();
> >
> > FlexibleSQLStatement += "SELECT UNIT, LONGNAME, A, B, C, D ";
> > FlexibleSQLStatement += "FROM emc.allUnits ";
> > FlexibleSQLStatement += "WHERE UNIT = :Unit1 ";
> > FlexibleSQLStatement += "OR UNIT = :Unit2 ";
> > FlexibleSQLStatement += "OR UNIT = :Unit3 ";
> > FlexibleSQLStatement += "OR UNIT = :Unit4 ";
> >
> > QueryOperations->Parameters->ParseSQL(FlexibleSQLStatement,true);
> > QueryOperations->Parameters->ParamByName("Unit1")->Value =
UserDeclared1;
> > QueryOperations->Parameters->ParamByName("Unit2")->Value =
UserDeclared2;
> > QueryOperations->Parameters->ParamByName("Unit3")->Value =
UserDeclared3;
> > QueryOperations->Parameters->ParamBYName("Unit4")->Value =
UserDeclared4 = "HY01";
> >
> > QueryOperations->Open();
> >
> >}
> >
> >I hope this helps?
> >
> >Error Message is:
> >Project DataModuleTestingProject.exe raised exception class EOleException
with message 'No value give for one or more required parameters'. Process
stopped. Use Step or Run to continue.
> >
> >The error message isn't overly helpful, but I'm assuming that the
Variables Unit1, Unit2, Unit3, and Unit4 are not being properly bound?
> >
> >Thanks for all your help guys.
> >
> >Heinz
> >
> >
> >
> >
> >"Mark Guerrieri" <mguerrieri@cmsgrp.com> wrote:
> >>Ah yes, I missed that- the column and table names can not be parameters-
you
> >>need to dynamically add them to your SQL string.
> >>
> >>Mark
> >>
> >>"Viatcheslav V. Vassiliev" <support@oledbdirect.com> wrote in message
> >>news:40bd7880@newsgroups.borland.com...
> >>> SELECT ?, ?
> >>>
> >>> Most (if not all) DBMS will not allow parameters at this place.
Enclose
> >>also
> >>> column names into quotes - Date is probably keyword. Use TADODataSet
and
> >>set
> >>> CommandText property.
> >>>
> >>> //------------------------------------------
> >>> Regards,
> >>> Vassiliev V. V.
> >>> http://www.managed-vcl.com - using .Net objects in Delphi for Win32 +
> >>> ADO.Net
> >>> http://www.oledbdirect.com - The fastest way to access MS SQL Server,
> >>> MS Jet (Access) and Interbase (through OLEDB)
> >>>
> >>> "Heinz Ketchup" <heinz_like_the_ketchup@h0tmail.no_spam.c0m.no_spam>
> >>> �������/�������� � �������� ���������:
> >>> news:40bb4d03$1@newsgroups.borland.com...
> >>> >
> >>> > Thanks Mark,
> >>> >
> >>> > The first method works fine for me.
> >>> >
> >>> > Although I did try your second method, and it didn't seem to work.
> >>> > Is there something special that changes in the SQL Query?
> >>> >
> >>> > i.e.
> >>> > C++ Code
> >>> > DynamicFlexSQLStmt = "SELECT :Var1, :Var2 ";
> >>> > DynamicFlexSQLStmt += "FROM mySQLTable ";
> >>> > DynamicFlexSQLStmt += "WHERE Date >= :StartDate ";
> >>> > DynamicFlexSQLStmt += "AND Date <= :EndDate ";
> >>> >
> >>> > TADOQuery1->Parameters->ParseSQL(DynamicFlexSQLStmt, true);
> >>> > TADOQuery1->Parameters->ParamByName("Var1")->Value = "Date";
> >>> > etc...
> >>> >
> >>> > Debugging the DynamicFlexSQLStmt
> >>> > DynamicFlexSQLStmt = "SELECT ?, ? FROM mySQLTable WHERE Date >= ?
AND
> >>Date
> >>> <= ?"
> >>> > It starts when the ParseSQL statement command is executed,
> >>> > and remains that way after all the ParamByName statements are called
> >>> >
> >>> > I get an error "No Value given for one or more required Parameters"
> >>> >
> >>> > Any thoughts?
> >>> > And THANKS A LOT for the your reply!
> >>> > You've helped a whole heaps load!
> >>> >
> >>> > Heinz.
> >>> >
> >>> >
> >>> >
> >>> > "Mark Guerrieri" <markg11@comcast.net> wrote:
> >>> > >>
> >>> > >> AnsiString DynamicFlexSQLStmt = "SELECT Date " + ""... (Whatever
I
> >>add
> >>> for
> >>> > >Column Selections)
> >>> > >>
> >>> > >> I cannont use TADOQuery1->SQL->Add(":MyFlexibleSQLStmt");
> >>> > >>
TADOQuery1->SQL->Parameters->ParamByName("MyFlexibleSQLStmt")->Value
> >>=
> >>> > >DynamicFlexSQLStmt;
> >>> > >>
> >>> > >
> >>> > >Try this-
> >>> > >
> >>> > >AnsiString DynamicFlexSQLStmt = "SELECT Date " + ""... (Whatever I
add
> >>> for
> >>> > >Column Selections)
> >>> > >TADOQuery1->SQL->Text = DynamicFlexSQLStmt;
> >>> > >
> >>> > >I do this all the time and it works fine. I don't bother with
> >>> parameters,
> >>> > >I just build the values right into the SQL statement. If you need
> >>> > >parameters, add this:
> >>> > >
> >>> > >TADOQuery1->Parameters->ParseSQL(DynamicFlexSQLStmt, true);
> >>> > >TADOQuery1->Parameters->ParamByName("ParamName")->Value =
ParamValue;
> >>> > >
> >>> > >Mark
> >>> > >
> >>> > >
> >>> >
> >>>
> >>>
> >>
> >>
> >
>
Technical Details
Message-ID:
<40bf20f9$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<40ba50b3$1@newsgroups.borland.com> <40baa32b$1@newsgroups.borland.com> <40bb4d03$1@newsgroups.borland.com> <40bd7880@newsgroups.borland.com> <40be1ca0$1@newsgroups.borland.com> <40be3902$1@newsgroups.borland.com> <40be3a4f$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 10698b,88aab88ffc95b09 X-Google-Attributes: gid10698b,public From: "Viatcheslav V. Vassiliev" <support@oledbdirect.com> Newsgroups: borland.public.cppbuilder.database.ado References: <40ba50b3$1@newsgroups.borland.com> <40baa32b$1@newsgroups.borland.com> <40bb4d03$1@newsgroups.borland.com> <40bd7880@newsgroups.borland.com> <40be1ca0$1@newsgroups.borland.com> <40be3902$1@newsgroups.borland.com> <40be3a4f$1@newsgroups.borland.com> Subject: Re: Flexible SQL Queries through ADO. Date: Thu, 3 Jun 2004 17:00:44 +0400 Lines: 206 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 213.234.218.42 Message-ID: <40bf20f9$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1086267641 213.234.218.42 (3 Jun 2004 06:00:41 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail