6

i'm trying to execute the following SQL 2008 code it says there is a problem near "=" and "else"... i cant understand what is wrong with the code

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 
Robby Cornelissen
  • 72,308
  • 17
  • 104
  • 121
Monir Tarabishi
  • 688
  • 1
  • 17
  • 28

5 Answers5

2

Instead of trying to assign to the output parameters inside the EXISTS do the assignment then check @@rowcount to see if any matching row(s) were found.

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
Martin Smith
  • 402,107
  • 79
  • 682
  • 775
  • 1
    @iceDragon - You may well want to use `IF @@ROWCOUNT = 1` instead of `IF @@ROWCOUNT > 0` I just used the second one to preserve as far as possible what the original semantics with `EXISTS` would have been. – Martin Smith May 16 '13 at 12:10
1

Exists operator checks if any row present in resultset returned by query. In your example it's just assigning values to variable. For more reference please check this link : Assigning a variable inside an IF EXISTS clause

Community
  • 1
  • 1
Rohan
  • 2,970
  • 1
  • 18
  • 26
0

Can you move IF EXISTS after BEGIN

Semih Yagcioglu
  • 3,733
  • 1
  • 23
  • 42
0

Try This :

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
Binod
  • 447
  • 3
  • 12
0

Change

SELECT @emp_id = emp_id,
                      @emp_name = emp_name_ara

to

SELECT emp_id, emp_name_ara

Append @emp_id and @emp_name variables to WHERE clause

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  ))
Mudassir Hasan
  • 26,105
  • 18
  • 90
  • 124