Umeh Chukwudi

Umeh Chukwudi

  • NA
  • 2
  • 1.4k

Using IF else statements in stored procedure

Jun 10 2016 10:45 AM

I used two databases, student database and PIN database. Am trying to create a login verification where by if PIN is used with student's reg-number, the stored procedure query, checks if user THE USER REG-NUMBER INPUT is valid in the student database.

It also checks if PIN input is valid in the PIN database. If true, it then updates the PIN database in the column of used PIN number, with other credentials as entered by the user on first time usage of PIN number.

And if user tries to use same PIN again the query checks if credentials are right else prompts wrong PIN, but if credentials are correct pin usage adds up by 1 and user can gain access. #pls I limited pin usage count by 4 times alone# here below is my procedure

pls below is my stored procedure as managed to fix up;
 
 
CREATE PROCEDURE [dbo].PinValidation     
@Pin_no Nvarchar(24),
@Reg_Num Nvarchar(24),
@Session Nvarchar(50),
@Program Nvarchar(50)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Tries INT= 0
IF EXISTS (SELECT PIN.Reg_Num
FROM
student

INNER JOIN
PIN
ON student.Id = PIN.Id
WHERE (student.Reg_Number = @Reg_Num)
AND (PIN.Reg_Num = @Reg_Num))
BEGIN
UPDATE [PIN] SET [Tries]= Tries +1
WHERE Reg_Num = @Reg_Num
AND Pin_no = @Pin_no AND p_Session = @Session AND Program = @Program -- increases the value of tries by 1 each time pin is used
SELECT [Tries]
FROM PIN
WHERE Pin_no = @Pin_no
END
ELSE
BEGIN
UPDATE [PIN]
SET Reg_Num = @Reg_Num,
 p_Session = @Session,
 Program
= @Program,
 Tries
= @Tries + 1
WHERE [Pin_no] = @Pin_no --INSERTS/UPDATES PIN values if pin_no=@Pin_no and the above INNER JOIN END IF @Tries IS NOT NULL BEGIN
IF EXISTS (SELECT Pin_no FROM PIN
WHERE Pin_no = @Pin_no)
BEGIN
DELETE FROM [PIN]
WHERE [Tries] = 4 --Check the Amount of times pin has been used if >3 delete entire pin row
END
END
ELSE
BEGIN SELECT -1
END
END
 

Answers (1)