Several usages of SQL server cursor

The use of the cursor is divided into 5 steps:
1. Declare the cursor
2. Open the cursor
3. Read the data in the cursor
4. Close the cursor
5. Release the cursor

Error-prone places:
1. Write begin and end in while
2. Use set or select when updating the value

1. Count the number of students who did not choose courses

Involved tables: student, sc

--1、声明游标
declare num_cursor cursor
for
	select sno
	from student
for read only
--2、打开游标
open num_cursor
--声明变量
declare @sno char(20), @num int
select @num = 0
--3、读取游标中的数据
fetch next from num_cursor
into @sno
while @@FETCH_STATUS = 0
begin
	if not exists(select* from sc where Sno = @sno)
		select @num  = @num + 1
	fetch next from num_cursor
	into @sno
end
--打印未选课的学生人数
select @num
--4、关闭游标
close num_cursor
--5、释放游标
deallocate num_cursor

effect:
Insert picture description here

2. Print out the number of elective courses for each subject

Involved tables: course, score

--1、声明游标
declare cno_cursor cursor
for 
	select cno
	from course
for read only
--2、打开游标
open cno_cursor
--声明变量
declare @cno char(20), @num int
select @num = 0
--3、读取游标中的数据
fetch next from cno_cursor
into @cno
while @@FETCH_STATUS = 0
begin
	select @num = COUNT(*) from Score where cno = @cno
	--打印当前科目的人数
	print convert(char(5), @cno) + ':' + convert(char(1), @num)
	fetch next from cno_cursor
	into @cno
end
--4、关闭游标
close cno_cursor
--5、释放游标
deallocate cno_cursor

effect:
Insert picture description here

3. Calculate and count the number of people at each level based on student performance

Involved tables: sc

--声明游标
declare grade_cursor cursor
for
	select grade
	from sc
for read only
--打开游标
open grade_cursor

declare @grade int, @a int, @b int, @c int, @d int, @e int
select @a = 0, @b = 0, @c = 0, @d = 0, @e = 0

--读取游标中的数据
fetch next from grade_cursor
into @grade

while @@FETCH_STATUS = 0
begin
	if @grade is null or @grade < 60
		select @e = @e + 1
	else
		if @grade < 70
			select @d = @d + 1
		else
			if @grade < 80
				select @c = @c + 1
			else
				if @grade < 90
					select @b = @b + 1
				else
					select @a = @a + 1
	fetch next from grade_cursor
	into @grade
end

select @a, @b, @c, @d, @e

--关闭游标
close grade_cursor
--释放游标
deallocate grade_cursor

Insert picture description here

4. Calculate the personal income tax based on the employee's salary, 3000 yuan is the threshold, and the part exceeding 3000 yuan is levied personal income tax at a rate of 10%

Involved table: salary_table

--声明游标
declare salary_cursor cursor
for
	select cno, salary
	from salary_table
for read only
--打开游标
open salary_cursor

declare @cno char(10), @salary int, @tax int

--读取游标中的数据
fetch next from salary_cursor
into @cno, @salary

while @@FETCH_STATUS = 0
begin
	if @salary <= 3000
		select @tax = 0
	else
		select @tax = (@salary - 3000) * 0.1
	update salary_table set tax = @tax where cno = @cno
	
	fetch next from salary_cursor
	into @cno, @salary
end

--关闭游标
close salary_cursor
--释放游标
deallocate salary_cursor

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43772166/article/details/107157131