[英]error on a simple Stored Procedure in SQL Management Studio
I get a 'The multi-part identifier "Membership.Mem_Num" could not be bound.' 我得到一个“多部分标识符“ Membership.Mem_Num”无法绑定。” 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语句来验证表中是否存在成员编号。 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.
但是,我遇到的问题是if(@memnum!= Membership.mem_num)语句。
You want to use EXISTS() 您想使用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
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. 您正在尝试在不查询任何表的查询的一部分中引用Membership.memnum-它无法提取该数据。 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):
如果IF语句的目的是检查传递给存储过程的@MemNum在成员资格表中是否存在,请尝试以下逻辑(您可以替代地使用exist()代替count逻辑):
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
I need the if statement to validate if a member number exists in the table
我需要if语句来验证表中是否存在成员编号
You almost said it in sql 您几乎在sql中说过
if (not exists (select null from Membership where @MemNum = Membership.Mem_Num))...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.