Friday, March 30, 2012

relationships problem

VB.NET 05 (ADO.NET) / SQL SERVER 05.
i have a many-to-many relationship. therefore, according to the normalisation theory, i introduced a new table which acts as a link between them.

1) tblOrders
PK: orderID

2) tblOrdersItems
FK: orderID
FK: itemID

3) tblItems
PK: itemID

the user wishes to view the details of a particular order which should include all the items
contained withing that particular order.

question: what is the technique to be used in order to get this done?

i) dim varOrderID as integer
ii) select tblOrdersItems.itemID where orderID = varOrderID
iii) HOW TO NOW FETCH the details of the items contained in this order from tblItems which should be displayed on a datagrid?

I believe you could use query similar to

SELECT list of field here From TblItems I INNER JOIN tblOrdersItems OI ON I.itemID=OI.itemID

WHERE OI.orderID=MyIDHere

|||

You can use the following query,

Code Snippet

Select * from tblOrders A

Join tblOrdersItems B on A.orderId = b.orderId

Join tblItems C on C.itemId = B.ItemId

Where

A.orderId = @.YourOrderId

sql

No comments:

Post a Comment