BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SQL statement for master/detail ADODatasets

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers)
View previous topic :: View next topic  
Author Message
Bill C
Guest





PostPosted: Thu Dec 18, 2003 10:37 am    Post subject: SQL statement for master/detail ADODatasets Reply with quote




G’day all
D7 Pro, MS Server 7, ADODatasets, new to SQL

My app has a master and 3 detail ADODatasets and the user is
allowed to ‘construct’ an SQL query by clicking on field names,
the operators (and, or, etc) and typing what they want to search
for. The actual SQL statement is constructed and validated
underneath all this as the user clicks the selections.

The SQL statement finishes up as…

SELECT * from Patients P
Left outer JOIN samples S on P.Link = S.LinkSam
Left Outer JOIN tests T on P.Link = T.LinkTests
Left outer JOIN prenatal R on P.Link = PNDLink
WHERE (upper(diagnosis) = ‘DISORDER’ )

This SQL actually looks just in the Master table as the user
doesn’t want to find anything else in the Tests, Samples or
Prenatal tables. There is no way I can predict whether the user
is going to want data from ALL details, just one or even just
the Master. Also, the details may or may not have data at all.

When the above SQL is executed via CommandText, the ADODataset
has multiple occurrences of the same patient record, I think
where there are multiple entries in the other detail tables.
Interestingly, this SQL seemed to work OK when I was using it on
Ttables.

Is there a way to stop this? Any other suggestions gratefully
received.
Thanks for your help
Cheers
Bill



Back to top
Bill Todd (TeamB)
Guest





PostPosted: Thu Dec 18, 2003 2:09 pm    Post subject: Re: SQL statement for master/detail ADODatasets Reply with quote



Just change the query to:

SELECT * from Patients P
WHERE (upper(diagnosis) = ‘DISORDER’ )

Since you are not selecting any columns from the detail tables there
is no reason to include the left outer joins.

--
Bill (TeamB)
(TeamB cannot respond to questions received via email)
Back to top
Bill C
Guest





PostPosted: Thu Dec 18, 2003 10:58 pm    Post subject: Re: SQL statement for master/detail ADODatasets Reply with quote



That's OK if the user chooses to only want the patients with
the DISORDER... but what if they want the Sample to be
= 'BLOOD' as well? So the SQL then becomes

SELECT * from Patients P
Left outer JOIN samples S on P.Link = S.LinkSam
Left Outer JOIN tests T on P.Link = T.LinkTests
Left outer JOIN prenatal R on P.Link = PNDLink
WHERE (upper(diagnosis) = ‘DISORDER’ ) and (upper(type) = 'BLOOD')

What I am trying to have is a generic SQL statement that covers
all options that the user MAY choose to search for from the
master and details. The SQL 'grows' as the user selects
different items from the detail tables
Sorry if I'm not clear.
Cheers
Bill







"Bill Todd (TeamB)" <no (AT) no (DOT) com> wrote:
Quote:
Just change the query to:

SELECT * from Patients P
WHERE (upper(diagnosis) = ‘DISORDER’ )

Since you are not selecting any columns from the detail tables there
is no reason to include the left outer joins.

--
Bill (TeamB)
(TeamB cannot respond to questions received via email)


Back to top
Bill Todd (TeamB)
Guest





PostPosted: Fri Dec 19, 2003 1:12 am    Post subject: Re: SQL statement for master/detail ADODatasets Reply with quote

Sorry. I misread the original SQL statement. The only way to cover
every option in a single query is to link in all of the fields from
all of the tables as you are doing. The only other alternative is to
dynamically build the SQL statement based on which tables and fields
the user has selected.

--
Bill (TeamB)
(TeamB cannot respond to questions received via email)
Back to top
Bill C
Guest





PostPosted: Fri Dec 19, 2003 10:07 am    Post subject: Re: SQL statement for master/detail ADODatasets Reply with quote


Thanks Bill
I had come to the same conclusion and will dynamically build the SQL statement with more control ... I guess there are some things that just HAVE to be re-done from scratch when changing from a desktop app to Server7
Cheers
Bill

"Bill Todd (TeamB)" <no (AT) no (DOT) com> wrote:
Quote:
Sorry. I misread the original SQL statement. The only way to cover
every option in a single query is to link in all of the fields from
all of the tables as you are doing. The only other alternative is to
dynamically build the SQL statement based on which tables and fields
the user has selected.

--
Bill (TeamB)
(TeamB cannot respond to questions received via email)


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.