I have a Database Table that defines a Product.
Each Product has 3 Different Options, each of which is actually a set of items in a different table, referred to by a foreign key.
eg, Colours this item is available in, Sizes it is available in, Accessories that may be supplied.
I now have a requirement to retrieve all the options for a product in the most efficient way possible.
It seems to me that 3 stored procedures, each of which returns one of the subsets is inefficient, as a single operation to find all the options will involve 3 calls from my code to the SQL, and make the DB perform 3 almost identical options.
My next thought was to retrieve all the data in a single stored procedure, and use UNION to return a single table of all the options.
i.e.
that works, but the foreign keys are not available to my code at this time. Now this means that I either need to expand my objects to allow for 3 additional keys, simply so I can query the DB, or I use the Id of the product to determine the foreign keys.
Which leads me to this.
Which again works, but it seems inefficient to be making what is now 6 select statements to retrieve my data.
So, is there a way of setting 3 Local Variables at once (I tried using set and a select but that didnt work)
or is there a better way of retrieving and returning the data from 3 tables, where I only have the Id of a record in another table that has foreign keys to the 3 sets?
Each Product has 3 Different Options, each of which is actually a set of items in a different table, referred to by a foreign key.
eg, Colours this item is available in, Sizes it is available in, Accessories that may be supplied.
I now have a requirement to retrieve all the options for a product in the most efficient way possible.
It seems to me that 3 stored procedures, each of which returns one of the subsets is inefficient, as a single operation to find all the options will involve 3 calls from my code to the SQL, and make the DB perform 3 almost identical options.
My next thought was to retrieve all the data in a single stored procedure, and use UNION to return a single table of all the options.
i.e.
Code:
SELECT * FROM ColoursAvailable where ColourSetId = pfkColourSetId
UNION
SELECT * FROM SizesAvailable where SizeSetId = pfkSizeSetId
UNION
SELECT * FROM AccessoriesAvailable where AccessorySetId = pfkAccessorySetId
that works, but the foreign keys are not available to my code at this time. Now this means that I either need to expand my objects to allow for 3 additional keys, simply so I can query the DB, or I use the Id of the product to determine the foreign keys.
Which leads me to this.
Code:
DECLARE @Colours AS INT
DECLARE @Sizes AS INT
DECLARE @Accessories AS INT
SET @Colours =
(SELECT pfkColourSetId from Products
WHERE Product.Id = @Id)
SET @Sizes =
(SELECT pfkSizeSetId from Products
WHERE Product.Id = @Id)
SET @Accessories =
(SELECT pfkAccessorySetId from Products
WHERE Product.Id = @Id)
SELECT * FROM ColoursAvailable where ColourSetId = @Colours
UNION
SELECT * FROM SizesAvailable where SizeSetId = @Sizes
UNION
SELECT * FROM AccessoriesAvailable where AccessorySetId = @Accessories
Which again works, but it seems inefficient to be making what is now 6 select statements to retrieve my data.
So, is there a way of setting 3 Local Variables at once (I tried using set and a select but that didnt work)
or is there a better way of retrieving and returning the data from 3 tables, where I only have the Id of a record in another table that has foreign keys to the 3 sets?