T-SQL编程实现采用游标方式输出各课程的课号、课名和平均分。
时间: 2024-10-17 14:06:43 浏览: 23
实验九T-SQL编程、存储过程、游标.docx
在T-SQL(Transact-SQL,用于Microsoft SQL Server等数据库的语言)中,你可以使用游标(Cursor)来逐行处理数据并输出特定字段。假设你有一个名为`Courses`的表,其中包含`CourseID`, `CourseName`, 和 `AverageGrade`字段,下面是创建游标并遍历数据的一个示例:
```sql
DECLARE @courseCursor CURSOR FOR
SELECT CourseID, CourseName, AverageGrade
FROM Courses
OPEN @courseCursor
FETCH NEXT FROM @courseCursor INTO @currentCourseID, @currentCourseName, @currentAverageGrade
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @currentCourseID AS '课号', @currentCourseName AS '课名', @currentAverageGrade AS '平均分'
FETCH NEXT FROM @courseCursor INTO @currentCourseID, @currentCourseName, @currentAverageGrade
END
CLOSE @courseCursor
DEALLOCATE @courseCursor
--
阅读全文