3

I am trying to create a stored procedure like this,

CREATE PROCEDURE [dbo].[SP_Name]
(
    @ID varchar(50),
    @URL varchar(256)
)
AS
    SELECT DISTINCT 
       Table1.CID, Table2.Name, Table2.aID, Table2.bID,
       Table3.SchemeName, Table2.cURL
    FROM Table4
    INNER JOIN Table5 ON Table5.eID = Table1.eID
    INNER JOIN Table2 ON Table2.ID = Table1.CID
    INNER JOIN [Table3] ON Table3.aID = Table2.aID AND Table3.bID = Table2.bID
    WHERE 
        Table5.ID = @ID 
        AND Table2.cURL LIKE '%' + @URL + '%'

but I'm getting this error:

Msg 4104, Level 16, State 1, Procedure SP_Name, Line 7
The multi-part identifier "Table1.eID" could not be bound.
Msg 4104, Level 16, State 1, Procedure SP_Name, Line 7
The multi-part identifier "Table1.cID" could not be bound.
Msg 4104, Level 16, State 1, Procedure SP_Name, Line 7
The multi-part identifier "Table1.cID" could not be bound.

Even though syntax is right.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mathematics
  • 7,314
  • 25
  • 77
  • 152

1 Answers1

4

You have to add inner join with Table1

SELECT DISTINCT Table1.CID, 
Table2.Name, Table2.aID, Table2.bID,
Table3.SchemeName, Table2.cURL

FROM Table4
INNER JOIN Table1 ON ....
INNER JOIN Table5 ON Table5.eID = Table1.eID
INNER JOIN Table2 ON Table2.ID = Table1.CID
INNER JOIN [Table3] ON Table3.aID = Table2.aID AND Table3.bID = Table2.bID

WHERE Table5.ID=@ID AND Table2.cURL LIKE '%' + @URL + '%'

or use Table1 instead of Table4

SELECT DISTINCT Table1.CID, 
Table2.Name, Table2.aID, Table2.bID,
Table3.SchemeName, Table2.cURL

FROM Table1
INNER JOIN Table1 ON ....
INNER JOIN Table5 ON Table5.eID = Table1.eID
INNER JOIN Table2 ON Table2.ID = Table1.CID
INNER JOIN [Table3] ON Table3.aID = Table2.aID AND Table3.bID = Table2.bID
WHERE Table5.ID=@ID AND Table2.cURL LIKE '%' + @URL + '%'
Robert
  • 25,425
  • 8
  • 67
  • 81
  • Thanks but I am trying to create a procedure based on a procedure which is already there, not sure why one already there works but this one doesn't executes – Mathematics Aug 01 '13 at 08:59