0
Answer

pin check/ verification on stored procedure

Umeh Chukwudi

Umeh Chukwudi

8y
348
1
I am new to stored procedures, and i am trying to create a procedure<br /> to check pin validation, and also create count for amount of times a given PIN is used.<br /> the database values are;<br /> id;<br /> Pin_no;- these holds the PIN numbers.<br /> Reg_num; - These holds the registration number of the specified PIN user. <br /> P_Session; - these holds the school session PIN number was used. <br /> Program; - these holds the school program PIN number was used.<br /><div> Tries; - these holds the amount of times PIN has been used and if more than 3 times PIN row deletes.</div><div>i have designed this code as below . pls help ;</div><div>&nbsp;</div><div>&nbsp;<div class="dp-highlighter"><ol class="dp-sql" start="1"><li class="alt"><span><span class="keyword">CREATE</span><span>&nbsp;</span><span class="keyword">PROCEDURE</span><span>&nbsp;[dbo].PinValidation&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;@Pin_no&nbsp;<span class="keyword">varchar</span><span>(24),&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;@Reg_Num&nbsp;<span class="keyword">varchar</span><span>(24),&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;@Session&nbsp;<span class="keyword">varchar</span><span>(50),&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;@Tries&nbsp;<span class="keyword">INT</span><span>,&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;@Program&nbsp;<span class="keyword">varchar</span><span>(50)&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">AS</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">begin</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">SELECT</span><span>&nbsp;Pin_no&nbsp;</span><span class="keyword">FROM</span><span>&nbsp;PIN&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">WHERE</span><span>&nbsp;Pin_no=@Pin_no&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;@Reg_Num&nbsp;<span class="keyword">is</span><span>&nbsp;</span><span class="op">not</span><span>&nbsp;</span><span class="op">Null</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">BEGIN</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;IF&nbsp;<span class="op">NOT</span><span>&nbsp;EXISTS&nbsp;(</span><span class="keyword">SELECT</span><span>&nbsp;Reg_Num&nbsp;</span><span class="keyword">FROM</span><span>&nbsp;PIN&nbsp;</span><span class="keyword">WHERE</span><span>&nbsp;Reg_Num=@Reg_Num&nbsp;</span><span class="op">and</span><span>&nbsp;Program=@Program&nbsp;</span><span class="op">and</span><span>&nbsp;P_Session=@Session)&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">BEGIN</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">UPDATE</span><span>&nbsp;PIN&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">SET</span><span>&nbsp;Tries=&nbsp;Tries+1&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">WHERE</span><span>&nbsp;Reg_Num=@Reg_Num</span><span class="comment">--incrases&nbsp;the&nbsp;value&nbsp;of&nbsp;tries&nbsp;by&nbsp;1&nbsp;each&nbsp;time&nbsp;pin&nbsp;is&nbsp;used</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">SELECT</span><span>&nbsp;@Tries[Tries]&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">end</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">ELSE</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">UPDATE</span><span>&nbsp;PIN&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">SET</span><span>&nbsp;Reg_num&nbsp;=&nbsp;@Reg_Num,&nbsp;P_Session=@Session,&nbsp;Tries=Tries+1,&nbsp;Program=@Program&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">Where</span><span>&nbsp;Pin_no=@Pin_no&nbsp;</span><span class="comment">--Updates&nbsp;database&nbsp;pin&nbsp;value&nbsp;for&nbsp;used&nbsp;pin&nbsp;on&nbsp;first&nbsp;usage</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">end</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">end</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If&nbsp;@Tries&nbsp;<span class="keyword">is</span><span>&nbsp;</span><span class="op">not</span><span>&nbsp;</span><span class="op">Null</span><span>&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">BEGIN</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;<span class="op">NOT</span><span>&nbsp;EXISTS&nbsp;(</span><span class="keyword">SELECT</span><span>&nbsp;Pin_no&nbsp;</span><span class="keyword">FROM</span><span>&nbsp;PIN&nbsp;</span><span class="keyword">WHERE</span><span>&nbsp;Pin_no=@Pin_no)&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">BEGIN</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">DELETE</span><span>&nbsp;</span><span class="keyword">FROM</span><span>&nbsp;[PIN&nbsp;]&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">WHERE</span><span>&nbsp;&nbsp;((&nbsp;[Tries]&nbsp;=&nbsp;3))</span><span class="comment">--Check&nbsp;the&nbsp;Amount&nbsp;of&nbsp;times&nbsp;pin&nbsp;has&nbsp;been&nbsp;used&nbsp;if&nbsp;&gt;3&nbsp;delete&nbsp;entire&nbsp;pin&nbsp;row</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">end</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">end</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">ELSE</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">BEGIN</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">SELECT</span><span>&nbsp;-1&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">END</span><span>&nbsp; <br /></span></span></li></ol></div></div>