CREATE PROCEDURE [dbo].[Login] @AUserIdCode VARCHAR(20), @APasswd VARCHAR(10), @AUserIdNumber INT OUTPUT, @AErrorCode INT OUTPUT  AS

/*
  Created by Kent Johansen May 2009

  ErrorCodes:
  0: Login ok
  1: Wrong UserIdCode or Passwd
  2: User inactive or deleted

*/

DECLARE @ActivePerson BIT
DECLARE @MarkAsDeleted TINYINT

SELECT @AUserIdNumber = UserIdNumber, @ActivePerson = ActivePerson, @MarkAsDeleted = @MarkAsDeleted
FROM Personel WITH (NOLOCK)
WHERE UserIdCode = @AUserIdCode
AND Passwd = @APasswd

PRINT '@ActivePerson'
PRINT @ActivePerson
PRINT '@MarkAsDeleted'
PRINT @MarkAsDeleted

IF (ISNULL(@AUserIdNumber, 0) = 0) OR ((ISNULL(@ActivePerson, 1) = 0) OR (ISNULL(@MarkAsDeleted, 0) <> 0))
BEGIN
  IF (ISNULL(@AUserIdNumber, 0) = 0)
    SET @AErrorCode = 1 -- Wrong UserIdCode or Passwd
  ELSE
    SET @AErrorCode = 2 -- User inactive or deleted
  SET @AUserIdNumber = -1
END
ELSE
  SET @AErrorCode = 0 -- Login ok
