When drawing questions, it is easy to encounter such a situation: 50 questions have a total of 100, that is, 2 points for each question. If you fill in one by one manually, you will not be exhausted. I ran into a problem like this. So I wrote this simple stored procedure to do this. After the execution, the scores of most of the questions are settled, and a small part of the hand tuning + minor tuning is OK.
Simple ideas and business:
1. Calculate the average score (integer) according to the total score of the test paper and the total number of questions;
2. Get the total score of all questions;
3. Subtract the total score of the question from the total score of the test paper, and add the difference to the last question.
The implementation process is as follows:
ALTER PROCEDURE avgScore @id int -- test paper serial number AS BEGIN SET NOCOUNT ON; --1. The serial number of the verification test paper cannot be empty if(@id is null or @id<=0) begin raiseerror('must provide test paper information',16,1); return ; end --2. Get the score of the current test paper declare @field6 int;--The total score of the test declare @count int;--The total number of questions in the current test paper declare @avg int;--average declare @sum int;--Total score, the sum of the scores of each topic, possibly <= total score select @field6=field6 from table88 where field1=@id; select @count = count(field1) from table89 where field2=@id; if(@field6>0 and @count>0)-- both have values begin set @avg = @field6/@count;--get the average -- Divide the score into each topic update table89 set field11=@avg where field2=@id; set @sum = @avg*@count; set @sum = @field6-@sum;--get the difference between the two --Update the score of the last question, that is, give all the remaining scores to the last question update table89 set field11=isnull(field11,0)+@sum where field1=(select top 1 field1 from table89 where field2=@id order by field1 desc); end END