Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have developed a website for registering to an exam which is published
flow goes like this
1. student register Application id generated Identity col
2. he fills the application form
3. saves
4. when he submits form his roll number is generated so i have written a stored procedure which updates student information and assigns him with a roll number


What I have tried:

now problem is that often two students are getting same roll number who hit save button at same time . so how can i avoid this. i can not apply unique constraint because the site is published and it will through exception and i can not halt site for maintenance. currently i am manually firing query on data base for finding duplicate record and update it manually with the missing roll number
Posted
Updated 7-Feb-17 0:55am

1 solution

I guess your SP does a SELECT MAX to generate tol roll number?
In case you are using MSSQL Server 2012 or later I ywould suggest you to use SEQUENCES:
tps://msdn.microsoft.com/en-us/library/ff878091.aspx[^]
 
Share this answer
 
Comments
manjiriV 7-Feb-17 7:47am    
no i dont use max here is my sp.. there are four region for exam A city, B city ,C, city and D city. so if student is from A city his roll number will be A1928 if 1000 or A10045 if 45 students registered




ALTER PROCEDURE [SPI].[GenerateRollNumber]
@ApplicantID INT
AS

BEGIN
--Declare variables
DECLARE @ExamCenterId INT=0;
DECLARE @ExamCenterName VARCHAR(20);
DECLARE @RollNumber VARCHAR(20)=NULL;
DECLARE @ToatlApplications INT=0;
--SELECT Exam Center and Center Name
SELECT @ExamCenterId=ExamCenter,@ExamCenterName=tblExamDistrict.ExamDistrict FROM tblApplications INNER JOIN tblExamDistrict ON tblApplications.ExamCenter=tblExamDistrict.ExamDistrictID WHERE ApplicantID=@ApplicantID
--Get Total COunt
SELECT @ToatlApplications=COUNT(ApplicationID)+1 FROM tblApplications where ExamCenter=@ExamCenterId and tblapplications.RollNumber is not null

IF @ToatlApplications<10
BEGIN
SET @RollNumber = CONCAT(UPPER(SUBSTRING(@ExamCenterName, 1, 1)) , '1000' , @ToatlApplications);
END
ELSE IF (@ToatlApplications < 100 AND @ToatlApplications >= 10)
BEGIN
SET @RollNumber =CONCAT(UPPER(SUBSTRING(@ExamCenterName, 1, 1)) , '100' , @ToatlApplications);
END
ELSE IF (@ToatlApplications < 1000 AND @ToatlApplications >= 100)
BEGIN
SET @RollNumber = CONCAT(UPPER(SUBSTRING(@ExamCenterName, 1, 1)) , '10' , @ToatlApplications);
END
ELSE IF (@ToatlApplications >= 1000)
BEGIN
SET @RollNumber = CONCAT(UPPER(SUBSTRING(@ExamCenterName, 1, 1)) , '1' , @ToatlApplications);
END

UPDATE tblApplications SET RollNumber=@RollNumber WHERE ApplicantID=@ApplicantID

END

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900