Alex
Alex

Reputation: 11

error on a simple Stored Procedure in SQL Management Studio

I get a 'The multi-part identifier "Membership.Mem_Num" could not be bound.' error whenever I tried to execute this code:

Alter Proc Procedure1
(
@MemNum int
)
AS
BEGIN 
if (@MemNum != Membership.Mem_Num)
    Begin
        Print 'This Member Number does not exist.'
    End
    Else
    Select @MemNum AS 'MemNum', Movie.Movie_Num, Movie_Title, Movie_Year, Movie_Cost, Movie_Genre ,Price_Code
    From Movie, Membership, Video, DetailRental, Rental
    Where @MemNum = Membership.Mem_Num AND
    Movie.Movie_Num = Video.Movie_Num AND 
    Video.Vid_Num = DetailRental.Vid_Num AND
    DetailRental.Rent_Num = Rental.Rent_Num AND
    Rental.Mem_Num = Membership.Mem_Num END

I need the if statement to validate if a member number exists in the table. If it does not, then the code should print "member number does not exist." However, the problem I am getting is in the if (@memnum != Membership.mem_num) statement.

Upvotes: 1

Views: 34

Answers (3)

Serg
Serg

Reputation: 22811

I need the if statement to validate if a member number exists in the table

You almost said it in sql

if (not exists (select null from Membership where @MemNum = Membership.Mem_Num))...

Upvotes: 0

Hogan
Hogan

Reputation: 70538

You want to use EXISTS()

if NOT EXITS(SELECT 1 FROM Membership WHERE Mem_Num = @MemNum)
BEGIN 
-- etc

You should also use the modern syntax for joins... it is much clearer.

Select @MemNum AS 'MemNum', Movie.Movie_Num, Movie_Title, Movie_Year, Movie_Cost, Movie_Genre ,Price_Code
From Movie  
JOIN Video ON Movie.Movie_Num = Video.Movie_Num
JOIN DetailRental ON Video.Vid_Num = DetailRental.Vid_Num
JOIN Rental ON DetailRental.Rent_Num = Rental.Rent_Num
JOIN Membership ON Rental.Mem_Num = Membership.Mem_Num  
Where @MemNum = Membership.Mem_Num

Upvotes: 2

Adeeb Armalite
Adeeb Armalite

Reputation: 704

You are trying to reference membership.memnum in a part of the query which is not querying any table - it is unable to pull that data. If the purpose of the IF statement is to check whether the @MemNum passed into the stored procedure exists in the membership table, try this logic (you can alternatively use exists() instead of the count logic):

ALTER PROC Procedure1 (@MemNum INT) 
AS 
  BEGIN 
      IF (SELECT COUNT(*) FROM membership WHERE mem_num = @MemNum) = 0
        BEGIN 
            PRINT 'This Member Number does not exist.' 
        END 
      ELSE 
        SELECT @MemNum AS 'MemNum', 
               movie.movie_num, 
               movie_title, 
               movie_year, 
               movie_cost, 
               movie_genre, 
               price_code 
        FROM   movie, 
               membership, 
               video, 
               detailrental, 
               rental 
        WHERE  @MemNum = membership.mem_num 
               AND movie.movie_num = video.movie_num 
               AND video.vid_num = detailrental.vid_num 
               AND detailrental.rent_num = rental.rent_num 
               AND rental.mem_num = membership.mem_num 
  END 

Upvotes: 0

Related Questions