Search code examples
sqlsql-serverasp-classicalterproc

Joining another table WHILST using ALTER PROC in SQL


Simple one for you guys.

Lots of threads regarding joining tables etc. However, I am yet to find one that also involves the ALTER PROC SQL.

My task is simple...

I just have to JOIN a table called 'STOCK_DESCRIPTORS' to the SQL below in order to display it on my database website.

USE [ShopDatabase]
GO
/****** Object:  StoredProcedure [dbo].[stpGet_StockUnitDetails]    Script Date:    09/17/2013     12:03:05 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[stpGet_StockUnitDetails]
@strStockUnitCode nvarchar(250)
As
SELECT * from VIEWSTOCKUNIT Where strStockUnitCode = @strStockUnitCode

I have tried merely adding: SELECT * from VIEWSTOCKUNIT, STOCK_DESCRIPTORS
WHERE strStockUnitCode = @strStockUnitCode

However whilst this parses successfully, it messes up the .ASP (strStockUnitCode exists on both tables).


Solution

  • All you have to do is join the tables together to be:

    Make sure you change the * to be the columns you need for your code. it is bad practice to select all the columns.

    USE [ShopDatabase]
    GO
    /****** Object:  StoredProcedure [dbo].[stpGet_StockUnitDetails]    Script Date:    09/17/2013     12:03:05 ******/
    SET ANSI_NULLS OFF
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROC [dbo].[stpGet_StockUnitDetails]
    @strStockUnitCode nvarchar(250)
    As
    SELECT 
         * -- change this to be the columns your code needs
     from VIEWSTOCKUNIT a
    inner join STOCK_DESCRIPTORS b on a.strStockUnitCode = b.strStockUnitCode
     Where a.strStockUnitCode = @strStockUnitCode