SQL Server 2008 – Inner Join in Select Statement Within Stored Procedure

sql serversql-server-2008

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.

CREATE PROCEDURE sp_Select
AS
BEGIN
    SELECT [ProductID]
        ,[Name]
        , CategoryName = (SELECT TOP 1 tblCategories.CategoryName
                            FROM tblCategories
                            WHERE 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

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.

CREATE PROCEDURE sp_Select
AS
BEGIN
    SELECT P.[ProductID]
        ,P.[Name]
        , C.CategoryName
        ,P.[Model]
        , Cast(P.[RAM] AS NVARCHAR(10)) + ' ' + P.[RAMMemUnit] AS RAM
        ,Cast(P.[SecondryMem] AS NVARCHAR(10)) + ' ' + P.[SecondMemUnit] AS Memory
        ,P.[OSVersion]
        ,P.[Processor]
        ,CAST(P.[Battery] AS NVARCHAR(10)) + ' ' + P.[BatteryType] AS Battery
        ,P.UnitsInStock AS 'Quantity In Stock'
    FROM [tblProducts] AS P
        LEFT OUTER JOIN tblCategories AS C ON C.CategoryId = P.CategoryID;
END