A while loop inside SQL

Source: Internet
Author: User

--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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.