--while Cycle
--Features:
--1. No true/false, write conditional expressions
--2. can also be nested
--3. can also break,continue
--4. No {}, need to use begin. End
-If the number of people who fail the office is more than half (the exam problem is difficult), give everyone 2 points, the cycle plus, until a few people fail to pass.
Go
declare @subjectname nvarchar = ' Office '--account name
Declare @subjectId int = (select Subjectid from Subject where [email protected])--Account ID
Declare @classid int--the class ID that the specified account belongs to
Set @classid = (select ClassID from Subject where [email protected]); --Query the class ID to which the specified account belongs
declare @totalNum INT--Total People
Select @totalNum =count (*) from Student where [email protected] Get the total number of required parameters for the specified account exam
declare @unpassNum INT--The number of people who have not passed the specified account
Select @unpassNum = (select COUNT (*) from Result where [email protected] and studentresult<60)--query does not pass the number of people
--loop plus
while (@unpassNum > @totalNum/2)
Begin
-- Perform an add-on Operation
Update Result set studentresult+=2 where [email protected] and studentresult<=98
--again counting the number of failed trips Select @unpassNum = (select COUNT (*) from Result where [email protected] and studentresult<60)
End
GO
--------------------------------------------------
------------------------------------------------------
Go
declare @subjectname nvarchar () = ' Office '--account name
declare @subjectId int = (select Subjectid from Subject where [email protected])--Account ID
Declare @classid int--the class ID that the specified account belongs to
Set @classid = (select ClassID from Subject where [email protected]); --Query the class ID that the specified account belongs to
DECLARE @totalNum INT--Total number of people
Select @totalNum =count (*) from Student where [email protected] Get the total number of subjects for which you want to specify a parameter exam
DECLARE @unpassNum INT--The number of people who have not passed the specified account
--select @unpassNum = (select COUNT (*) from Result where [email protected] and studentresult<60)--Query failed
--Cyclic plus points
while (1=1)
Begin
if (@totalNum/2< (select COUNT (*) from Result where [email protected] and studentresult<60))
--Perform a plus split operation
Update Result set studentresult+=2 where [email protected] and studentresult<=98
Else
Break
End
A while loop inside SQL