这个SQL Server存储过程出了什么问题?

时间:2015-12-03 19:13:23

标签: sql sql-server procedure

我试图编写一个程序,在我的会员表中搜索输入用户名和密码的匹配成员。如果找到匹配,它应输出1。

这是我的代码:

CREATE PROC LoginProcedure
    @email varchar(50),
    @password varchar(50),
    @found bit output
AS
    IF(EXISTS(SELECT * 
              FROM Members  
              WHERE email = @email AND password = @password))
    BEGIN
        @found = 1; ---HERE
    END
    ELSE
    BEGIN
        @found = 0;  ----HERE
    END
GO  ---HERE

我已经在我遇到错误的所有行旁边做了评论。所有3行中的错误都是相同的。

  

语法不正确

1 个答案:

答案 0 :(得分:2)

SQL Server使用setselect来分配值:

CREATE PROCEDURE LoginProcedure (
    @email varchar(50),
    @password varchar(50),
    @found bit output
)
AS BEGIN
    IF (EXISTS (SELECT * FROM Members where email = @email and password = @password  
               ))
    BEGIN
        SET @found = 1; ---HERE
    END;
    ELSE BEGIN
        SELECT @found = 0;  ----HERE
    END;
END; -- LoginProcedure

我认为将它写成case语句会更简单:

BEGIN
   SET @found = (CASE WHEN EXISTS (SELECT * FROM Members where email = @email and password = @password)
                      THEN 1 ELSE 0
                 END);
END;