Article View: borland.public.cppbuilder.database.ado
Article #1603Flexible SQL Queries through ADO.
From: Heinz Ketchup
Date: Sun, 30 May 2004 21:22
Date: Sun, 30 May 2004 21:22
86 lines
3418 bytes
3418 bytes
Hi,
Is there a clever way of designing a flexible SQL Statement through ADO?
I have a database that is in the shape of the following
Column 1: Date
Column 2: DataWhateverColumn1
Column 3: DataWhateverColumn2
Column 4: DataWhateverColumn3
Column 5: DataWhateverColumn4
Column 6: DataWhateverColumn5
etc...
Up to 30
<To Break Things Down Simplistically>
I'm using a TDataModule.
I Have a TADOQuery Associated with a TDatasource.
The SQL Statement TString in the TADOQuery is:
Select *
From mySQLTableData
Where Date>=:InputStartDate and Date<=:InputEndDate
The corresponding TParameter(s) are
InputStartDate
InputEndDate
My problem comes in where:
1. I need only certain data columns ("X" Combinations)
So my SQL Query could look like
Select Date, DataWhateverColumn1
From mySQLTableData
Where Date>=:InputStartDate and Date<=:InputEndDate
or
Select Date, DataWhateverColumn2
From mySQLTableData
Where Date>=:InputStartDate and Date<=:InputEndDate
or
Select Date, DataWhateverColumn1, DataWhateverColumn2
From mySQLTableData
Where Date>=:InputStartDate and Date<=:InputEndDate
I Use the Following Method:
TADOQuery1->Close();
TADOQuery1->SQL->Clear();
TADOQuery1->SQL->ADD("Select Date, DataWhateverColumn1 ")
TADOQuery1->SQL->ADD("From mySQLTableData ");
TADOQuery1->SQL->ADD("Where Date>=:InputStartDate ");
TADOQuery1->SQL->ADD("and Date<=:InputEndDate ");
TADOQuery1->SQL->Parameters->ParamByName("InputStartDate")->Value = HappyStartDate;
TADOQuery1->SQL->Parameters->ParamByName("InputEndDate")->Value = HappyEndDate;
TADOQuery1->Open();
Unfortunately, I discovered that it is not possible to design a flexible SELECT statement.
AnsiString DynamicFlexSQLStmt = "SELECT Date " + ""... (Whatever I add for Column Selections)
I cannont use TADOQuery1->SQL->Add(":MyFlexibleSQLStmt");
TADOQuery1->SQL->Parameters->ParamByName("MyFlexibleSQLStmt")->Value = DynamicFlexSQLStmt;
There must be a way where I can dynamically bind a SQL SELECT Statement to an ADOQuery? Shouldn't there be one? Other than the SQL->ADD() method which is rather limited.
Is there something even more advanced that I should do?
The alternative is a frightenly large set of switch statements.
case whatever:
SQL->Add("Select This")
next case:
SQL->Add("Select That")
next case:
SQL->ADD("Select This, That")
... more combinations
But being a programmer... it's obvious that there's a better way, but I just don't know what it is yet.
Thanks,
Heinz.
Technical Details
Message-ID:
<40ba50b3$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!controlnews3.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-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10698b,88aab88ffc95b09,start X-Google-Attributes: gid10698b,public From: "Heinz Ketchup" <heinz_like_the_ketchup@h0tmail.no_spam_.c0m.no_spam> Sender: "Heinz Ketchup" <heinz_like_the_ketchup@h0tmail.no_spam_.c0m.no_spam> Reply-To: "Heinz Ketchup" <heinz_like_the_ketchup@h0tmail.no_spam_.c0m.no_spam> Subject: Flexible SQL Queries through ADO. Newsgroups: borland.public.cppbuilder.database.ado X-User-Info: 209.89.134.137 NNTP-Posting-Host: 207.105.83.66 Message-ID: <40ba50b3$1@newsgroups.borland.com> Date: 30 May 2004 14:22:59 -0700 X-Trace: newsgroups.borland.com 1085952179 207.105.83.66 (30 May 2004 14:22:59 -0700) Lines: 86 Path: arc1-ca.pugleaf.net!controlnews3.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