My table and data as following,declare @t1 table(idx int identity, programID int, quota int, dtVersion timestamp);/*programID is unique*/insert into @t1(programID, quota) values(1, 100);insert into @t1(programID, quota) values(3, 70);insert into @t1(programID, quota) values(4, 100);--and so ondeclare @tResult table(idx int identity, studentID int, programID int);/*Combination of studentID and programID will generate a unique rowprogramID is foreign key to @t1(programID)*/insert into @tResult(studentID, programID) values(29,1);insert into @tResult(studentID, programID) values(38,1);insert into @tResult(studentID, programID) values(37,4);insert into @tResult(studentID, programID) values(39,3);--and so on--The application result data stored in @tResult
The scenario as following,1. Student (studentID) will apply any Program (programID)2. The successful of the application result based on quota balance in @t1. Quota balance = (Application result data in @tResult) - quota3. If the program is out of quota, the system will select another program randomly till successful. Another word, FIFO4. If all the program and all the quota is filled, there's no chances for student to apply any programThe transaction scenario as following1. Student with studentID=89 apply the programID=4. Let's say, the program still have the quota, so T-SQL execution as followinginsert into @tResult(studentID, programID) values(89,4);
2. Student with studentID=74 apply the programID=3. Let's say, the program is out the quota, so T-SQL have to choose another programID. May be, T-SQL execution as followinginsert into @tResult@tResult(studentID, programID) values(74,1); -- the programID choose randomly. Old programID=3. New programID=1
3. Student with studentID=102 apply the programID=1. Let's say, all the program and all the quota is FILLED. There's no T-SQL will be executed in @tResult. At this level, message will prompt to user 'Sorry, there's no program available at this moment'I can't imagine How my SQL statement looks like. I hope someone can help me to imagine the logic and built the T-SQLReally need help