I'm trying to Create a stored procedure but there is a field actually exist in another field but its id is used as foreign key in current table, SQL Query is:
Create Procedure sp_Select
As
Begin
SELECT [ProductID]
,[Name]
,SELECT tblCategories.CategoryName
FROM tblCategories INNER JOIN tblProducts
ON tblCategories.CategoryId = tblProducts.CategoryID;
,[Model]
,Cast([RAM] As nvarchar(10)) +' '+[RAMMemUnit] As RAM
,Cast([SecondryMem] As nvarchar(10)) + ' ' + [SecondMemUnit] As Memory
,[OSVersion]
,[Processor]
,CAST([Battery] As nvarchar(10)) +' '+ [BatteryType] As Battery
,UnitsInStock AS 'Quantity In Stock'
FROM [tblProducts]
End
Error in this following portion:
SELECT tblCategories.CategoryName FROM tblCategories INNER JOIN tblProducts ON tblCategories.CategoryId = tblProducts.CategoryID;
Someone help me please.
Best Answer
There are actually two problems with your first query. The first is that you have an extra semi-colon for the subquery where you are getting the category name. The second is that if that query returns more than one row then you will receive an error. I've corrected it for you to eliminate both of those issues.
However, queries that have sub-queries like the above generally perform worse than ones that can achieve the same results with a sub-query. It lets you access other fields from the tblCategories table without incurring additional sub-query costs, and you can more easily search for products that are in particular categories.
Some of the comments have mentioned changing the join from using a LEFT OUTER to be an INNER join. I chose LEFT OUTER initially because that more closely matched the result set you would have received in your first query. Essentially, it would have NULL's in place where the CategoryName would be assuming you had Products without Categories. An INNER JOIN would eliminate products that had no matching category.