0% found this document useful (0 votes)
34 views9 pages

Store 2

The document discusses using cursors in SQL stored procedures to loop through records and display student and subject information. It provides examples of procedures that use cursors to print student lists with details, subject lists for individual students, and a student list with average marks. The final example procedure loops through students, calls another procedure to calculate average marks, and displays the results.

Uploaded by

HuyCường
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views9 pages

Store 2

The document discusses using cursors in SQL stored procedures to loop through records and display student and subject information. It provides examples of procedures that use cursors to print student lists with details, subject lists for individual students, and a student list with average marks. The final example procedure loops through students, calls another procedure to calculate average marks, and displays the results.

Uploaded by

HuyCường
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Session 3: Cursor

Cursor is a data type containing a table from SELECT ---


FROM---WHERE---
Example:
a. Print the information of @ClassID as follows:
Class ID:…. BeginYear:……EndYear:…..
1. Student Name:….. Student ID:….. Birthday:….
2. Student Name:….. Student ID:….. Birthday:….
3. Student Name:….. Student ID:….. Birthday:….
….
Total:…. Students.
create proc StudentList
@classID varchar(50)
as
begin
declare @beginyear int
declare @endyear int
select @beginyear=beginyear,
@endyear=endyear
from class
where id=@classID
print 'ClassID: ' + @classID + '
BeginYear' + cast(@beginyear as varchar(10))
+ ' EndYear' + cast(@endyear as varchar(10))

declare c cursor for


(select ID, name, birthday from student
where classID=@classID)

open c

declare @ID varchar(50)


declare @birthday datetime
declare @name nvarchar(50)

fetch next from c into @ID, @name,


@birthday
declare @i int
set @i=1
while(@@fetch_status=0)
begin
print cast(@i as varchar(10)) + '.
Name:'+ @name +' ID:' + @ID +' Birthday:'
+cast(@birthday as varchar(50))
fetch next from c into @ID, @name,
@birthday
set @i=@i+1
end
set @i=@i-1
print 'Total:' + cast(@i as varchar(20))
close c
deallocate c
end
go
exec StudentList 'LH000001 '
b. Print the subjects that @StudentID have passed as
follows:
1. Subject Name:….. Credit:….. Mark:….
2. Subject Name:….. Credit:….. Mark:….
3. Subject Name:….. Credit:….. Mark:….

create proc SubjectList


@StudentID varchar(50)
as
begin
declare d cursor for
(select sub.name, sub.credits, r.mark
from result r, subject sub
where r.subjectID=sub.ID
and r.studentID=@studentID and r.mark>=5
and r.times >= all ( select r1.times from
result r1 where r1.subjectID=r.subjectID and
r1.
studentID=r.studentID))
open d
declare @name nvarchar(50)
declare @credit int
declare @mark float
declare @i int
set @i=1
fetch next from d into
@name,@credit,@mark
while(@@fetch_status=0)
begin
print cast(@i as varchar(3)) + ' Sub
Name:' + @name +' Credit:'+ cast(@credit as
varchar (50)) + ' Mark:' +cast(@mark as
varchar(4))
set @i=@i+1
fetch next from d into
@name,@credit,@mark
end
close d
deallocate d
end
exec SubjectList 'HV000001 '
c. Print the information of @ClassID as follows:
Class ID:…. BeginYear:……EndYear:…..
1. Student Name:….. Student ID:….. Birthday:….
-Passed Subjects:
SubjectName:…. Mark:…..

2. Student Name:….. Student ID:….. Birthday:….
-Passed Subjects:
SubjectName:…. Mark:…..

Total:…. Students.
create proc SubjectList1
@StudentID varchar(50)
as
begin
declare d cursor for
(select sub.name, r.mark
from result r, subject sub
where r.subjectID=sub.ID
and r.studentID=@studentID and r.mark>=5
and r.times >= all ( select r1.times from
result r1 where r1.subjectID=r.subjectID and
r1.
studentID=r.studentID))
open d
declare @name nvarchar(50)
declare @mark float
fetch next from d into @name,@mark
while(@@fetch_status=0)
begin
print' Sub Name:' + @name +' Mark:'
+cast(@mark as varchar(4))
fetch next from d into @name, @mark
end
close d
deallocate d
end

create proc StudentList1


@classID varchar(50)
as
begin
declare @beginyear int
declare @endyear int
select @beginyear=beginyear,
@endyear=endyear
from class
where id=@classID
print 'ClassID: ' + @classID + '
BeginYear' + cast(@beginyear as varchar(10))
+ ' EndYear' + cast(@endyear as varchar(10))

declare c cursor for


(select ID, name, birthday from student
where classID=@classID)

open c

declare @ID varchar(50)


declare @birthday datetime
declare @name nvarchar(50)

fetch next from c into @ID, @name,


@birthday
declare @i int
set @i=1
while(@@fetch_status=0)
begin
print cast(@i as varchar(10)) + '.
Name:'+ @name +' ID:' + @ID +' Birthday:'
+cast(@birthday as varchar(50))
print '-Passed Subjects:'
exec SubjectList1 @ID

fetch next from c into @ID, @name,


@birthday
set @i=@i+1
end
set @i=@i-1
print 'Total:' + cast(@i as varchar(20))
close c
deallocate c
end
go
exec StudentList1 'LH000001 '

Exercise:

Print the students of ClassID


ManagerID:.... ManagerName:
1. StudentID:....
StudentName:...... Average Mark:....
2. StudentID:....
StudentName:...... Average Mark:....
3. StudentID:....
StudentName:...... Average Mark:....

create proc GetAverage


@StudentID nvarchar(30), @mark float
output
as
begin

select
@mark=sum(r.mark*sub.credits)/sum(sub.credits
)
from student s, result r, subject sub
where s.ID=r.studentID and r.mark is not
null
and r.subjectID=sub.ID and
s.ID=@StudentID and r.times > = all
(select r1.times from result r1
where r1.studentID=s.ID and
r1.subjectID=sub.ID
)

end
go

create proc ABC @ClassID varchar(20)


as
begin
declare @managerID varchar(20)
declare @managerName nvarchar(50)
select @managerID=c.ManagerID,
@managerName=t.Name
from class c, teacher t where
c.ID=@classID
and c.ManagerID=t.ID
print 'ClassID: ' + @ClassID+' ManagerID:
'+
@managerID + ' ManagerName: '+
@managerName
declare c cursor for
select s.ID, s.name from student s where
s.classID=@ClassID
open c
declare @ID varchar(20)
declare @name nvarchar(20)
fetch next from c into @ID,@name
declare @i int
set @i=1
WHILE @@FETCH_STATUS = 0
begin
Declare @avermark float
exec GetAverage @ID, @avermark output
print cast(@i as varchar(4))+'.' + @ID
+ '
' +@name + ' ' +cast (@avermark as
varchar(20))
set @i=@i+1
fetch next from c into @ID,@name
end
close c
deallocate c
end
go
exec ABC 'LH000001 '
select * from student

You might also like