6

我正在尝试执行以下 SQL 2008 代码,它说“=”和“else”附近有问题......我无法理解代码有什么问题

ALTER PROCEDURE dbo.LoginEmp @username NVARCHAR(10),
                             @password NVARCHAR(10),
                             @confirm  INT output,
                             @emp_name NVARCHAR(50) output,
                             @emp_id   BIGINT output
AS
    IF EXISTS (SELECT @emp_id = emp_id,
                      @emp_name = emp_name_ara
               FROM   Employee
               WHERE  ( emp_username LIKE @username
                        AND emp_password LIKE @password ))
      BEGIN
          SET @confirm=1

          INSERT INTO EmployeeLog
                      (log_emp_id,
                       log_act_id,
                       log_date,
                       log_data)
          VALUES      (@emp_id,
                       1,
                       GETDATE(),
                       -1)
      END
    ELSE
      BEGIN
          SET @confirm=0
      END

    RETURN 
4

5 回答 5

2

与其尝试在执行分配中分配输出参数EXISTS,不如检查@@rowcount是否找到任何匹配的行。

ALTER PROCEDURE dbo.LoginEmp @username NVARCHAR(10),
                             @password NVARCHAR(10),
                             @confirm  INT output,
                             @emp_name NVARCHAR(50) output,
                             @emp_id   BIGINT output
AS
    SELECT @emp_id = emp_id,
           @emp_name = emp_name_ara
    FROM   Employee
    WHERE  ( emp_username = @username
             AND emp_password = @password )

    IF @@ROWCOUNT = 1
      BEGIN
          SET @confirm=1

          INSERT INTO EmployeeLog
                      (log_emp_id,
                       log_act_id,
                       log_date,
                       log_data)
          VALUES      (@emp_id,
                       1,
                       GETDATE(),
                       -1)
      END
    ELSE
      BEGIN
          SET @confirm=0
      END
于 2013-05-16T11:59:51.330 回答
1

Exists 运算符检查查询返回的结果集中是否存在任何行。在您的示例中,它只是为变量赋值。有关更多参考,请查看此链接:在 IF EXISTS 子句中分配变量

于 2013-05-16T12:05:41.593 回答
0

IF EXISTS以后可以搬吗BEGIN

于 2013-05-16T11:52:56.150 回答
0

尝试这个 :

ALTER PROCEDURE dbo.LoginEmp
@username nvarchar(10),
@password nvarchar(10),
@confirm int output,
@emp_name nvarchar(50) output,
@emp_id bigint output
AS 
Begin
if exists (SELECT emp_id, emp_name_ara FROM Employee WHERE        (emp_username LIKE @username AND emp_password LIKE @password)) begin
  // Here Retrieve the Data into Variable again. Like You did Above Before.
set @confirm=1

INSERT INTO EmployeeLog (log_emp_id,log_act_id,log_date,log_data) VALUES (@emp_id,1,GETDATE(),-1)

end
else begin

set @confirm=0
end 
end
RETURN
于 2013-05-16T11:53:12.047 回答
0

改变

SELECT @emp_id = emp_id,
                      @emp_name = emp_name_ara

SELECT emp_id, emp_name_ara

将@emp_id@emp_name变量附加到 WHERE 子句

IF EXISTS (SELECT  emp_id, emp_name_ara
               FROM   Employee
               WHERE  ( emp_username LIKE @username
                        AND emp_password LIKE @password AND emp_id=@emp_id AND emp_name_ara = @emp_name  ))
于 2013-05-16T11:55:42.533 回答