473,508 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

master detail relationship when querying for data

Hello All,

Suppose if I have a SQL query like

select p.ID, p.NAME, p.UNIT_PRICE, o.QUANTITY from PRODUCT p ORDERS o
where p.ID = X AND P.ID = O.ID

Here one product can have many orders. Essentially Product is the master
table and Orders is the details table.

When I execute this query and populate a DataSet object I get only one
table in the DataSet object.

But this not what is ideal. Ideally I would like to get two tables in
the DataSet one for Products and other for Orders.

I can get two tables is to execute two queries.
select id, name, unit_price from products where id=x
select quantity from orders, products where products.id = x and
orders.id = products.id

But this involves two sql queries.... so not good.

Second solution is to iterate thru the sql data reader and create a
datarows into handbuilt DataSet. if the product id is the same, then
just add another DataRow to the order table, if the product id changes
then, add another DataRow into product table and keep iterating for
orders for this product. But this solution is very error prone and also
it complicates the code.

Another solution is to execute SQL with FOR XML AUTO clause. which
returns XML which contains data in proper relationship. But XML is an
overkill for this kind of solution.

Is there any other solution to this problem none of the above solutions
are very good ones.

regards,
Abhishek.
Nov 15 '05 #1
2 2441
Abhishek,

The best solution for this, in fact, is to generate two queries. Your
SQL statement:

select p.ID, p.NAME, p.UNIT_PRICE, o.QUANTITY from PRODUCT p ORDERS o where
p.ID = X AND P.ID = O.ID

Is intended to return just one table. This is one result set, and is
represented by one table.

Why are two queries bad, if they will give you the result that you want?
Depending on how your application is set up, you could do all the processing
yourself to separate out the single table into two tables, but why bother?
SQL server already spent the time putting them together, and it would be a
waste to pull them apart. Basically you are doing twice the work for zero
gain.

I think that you should make two calls, wrapping them in a transaction
if you need to insure the integrity of the data between calls.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Abhishek Srivastava" <ab******@nospam.net> wrote in message
news:ur**************@TK2MSFTNGP12.phx.gbl...
Hello All,

Suppose if I have a SQL query like

select p.ID, p.NAME, p.UNIT_PRICE, o.QUANTITY from PRODUCT p ORDERS o
where p.ID = X AND P.ID = O.ID

Here one product can have many orders. Essentially Product is the master
table and Orders is the details table.

When I execute this query and populate a DataSet object I get only one
table in the DataSet object.

But this not what is ideal. Ideally I would like to get two tables in
the DataSet one for Products and other for Orders.

I can get two tables is to execute two queries.
select id, name, unit_price from products where id=x
select quantity from orders, products where products.id = x and
orders.id = products.id

But this involves two sql queries.... so not good.

Second solution is to iterate thru the sql data reader and create a
datarows into handbuilt DataSet. if the product id is the same, then
just add another DataRow to the order table, if the product id changes
then, add another DataRow into product table and keep iterating for
orders for this product. But this solution is very error prone and also
it complicates the code.

Another solution is to execute SQL with FOR XML AUTO clause. which
returns XML which contains data in proper relationship. But XML is an
overkill for this kind of solution.

Is there any other solution to this problem none of the above solutions
are very good ones.

regards,
Abhishek.

Nov 15 '05 #2
Hi Abhishek,

I got ur concern.
What should be done in this?
I feel write a Stored Prcedure where in write ur query and get the data in
temporary table.
And then wirite two different select queries and fetch the data from this
temp table.
This way ur procedure will return two different tables.
Using data reletion, u can display this data in repeater or grid or
whatever.

Actually what happened is : though u r writing two different queries it's
all at backend.
This may be little costly but it has to in this way only.

Hope this will be useful...
--
With Regards,
Girish Chopade

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uI**************@tk2msftngp13.phx.gbl...
Abhishek,

The best solution for this, in fact, is to generate two queries. Your
SQL statement:

select p.ID, p.NAME, p.UNIT_PRICE, o.QUANTITY from PRODUCT p ORDERS o where p.ID = X AND P.ID = O.ID

Is intended to return just one table. This is one result set, and is
represented by one table.

Why are two queries bad, if they will give you the result that you want? Depending on how your application is set up, you could do all the processing yourself to separate out the single table into two tables, but why bother?
SQL server already spent the time putting them together, and it would be a
waste to pull them apart. Basically you are doing twice the work for zero
gain.

I think that you should make two calls, wrapping them in a transaction
if you need to insure the integrity of the data between calls.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Abhishek Srivastava" <ab******@nospam.net> wrote in message
news:ur**************@TK2MSFTNGP12.phx.gbl...
Hello All,

Suppose if I have a SQL query like

select p.ID, p.NAME, p.UNIT_PRICE, o.QUANTITY from PRODUCT p ORDERS o
where p.ID = X AND P.ID = O.ID

Here one product can have many orders. Essentially Product is the master
table and Orders is the details table.

When I execute this query and populate a DataSet object I get only one
table in the DataSet object.

But this not what is ideal. Ideally I would like to get two tables in
the DataSet one for Products and other for Orders.

I can get two tables is to execute two queries.
select id, name, unit_price from products where id=x
select quantity from orders, products where products.id = x and
orders.id = products.id

But this involves two sql queries.... so not good.

Second solution is to iterate thru the sql data reader and create a
datarows into handbuilt DataSet. if the product id is the same, then
just add another DataRow to the order table, if the product id changes
then, add another DataRow into product table and keep iterating for
orders for this product. But this solution is very error prone and also
it complicates the code.

Another solution is to execute SQL with FOR XML AUTO clause. which
returns XML which contains data in proper relationship. But XML is an
overkill for this kind of solution.

Is there any other solution to this problem none of the above solutions
are very good ones.

regards,
Abhishek.


Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1100
by: Andrew Mueller | last post by:
Hello, Not sure how to give a quick explaination, but... I have an application which queries a proprietary archive. I do this using OLEDB with datasets and OLEDBDataAdapter. I am making...
4
2295
by: Michael Rodriguez | last post by:
Suppose I have a data entry screen with two strongly-typed datasets, dsCustomers and dsOrders, as the master and child, respectively. Because the two tables in the relationship are in two...
8
2035
by: Christopher Weaver | last post by:
I'm having the hardest time doing the simplest thing. I have a DataGrid bound to a table in a master detail DataRelation within a DataSet relTaskActivities = new DataRelation("TaskActivities",...
7
1509
by: erniej | last post by:
as per instructions in the walkthrough, I have managed to create a master/detail relationship between two datagrids. However I have a requirement to apply this to three datagrids and the third...
1
1920
by: Good | last post by:
Hi all I have created a master-detail relationship dataset under VS2005. However, when I update the tables, it seems the foreign key information couldn't be passed from the master table,...
13
3304
by: CindySue | last post by:
I have a table of providers that has a license number and fullname field, among others. Deman helped me with being able to enter a nickname and having it change to the fullname if a nickname exists...
3
2946
by: Gina_Marano | last post by:
Hey All, Working with the .Net data has been the hardest part of my transition. It just doesn't seem very intuitive. I want to have a master/detail relationship between 2 grids. When the...
2
2266
by: John | last post by:
Hi I am trying to create a master/detail form. I have my master and details tables dragged onto the dataset. I have also dragged the fields from master table on the form which has created the...
2
1738
by: jvdub22 | last post by:
I have a vb.net windows form application that has two datagrids on it that have a master - detail relationship. The underlying database is an sql database. The application works fine when I first...
0
7132
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7063
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.