早教吧 育儿知识 作业答案 考试题库 百科 知识分享

SQL填空题求解DECLARE@COUNOVARCHAR(3),@COUNAMEVARCHAR(20)————————————FORSELECTCOUNO,COUNAMEFROMCOURSEORDERBYCOUNO————————————FETCH————FROMCRSCOURSEINTO@COUNO,@COUNAMEWHILE@@

题目详情
SQL填空题求解
DECLARE @COUNO VARCHAR(3),@COUNAME VARCHAR(20)
————————————
FOR SELECT COUNO,COUNAME FROM COURSE ORDER BY COUNO
————————————
FETCH ———— FROM CRSCOURSE INTO @COUNO,@COUNAME
WHILE @@FETCH_STATUS=0
BEGIN
PRINT '课程号:'+ @COUNO +'课程名称:' + @CouName
FETCH ———— FROM CRSCOURSE INTO @COUNO,@COUNAME
END
————————
DEALLOCATE CRSCOURSSE
在横线上填写.
题目是声明一个CRSCOURSE的游标,并利用游标遍历,显示整个结果,且从结果集最后一条记录显示到第一条记录
然后有一答案
1.Declare crscourse cursor
2.open crscourse
3.NEXT
4.NEXT
5.close crscourse
这个答案不知道有没有达到【从结果集最后一条记录显示到第一条记录】这个条件
▼优质解答
答案和解析
完全到达游标标准写法!

USE AdventureWorks
GO
-- Declare the variables to store the values returned by FETCH.
DECLARE @LastName varchar(50), @FirstName varchar(50)

DECLARE contact_cursor CURSOR FOR
SELECT LastName, FirstName FROM Person.Contact
WHERE LastName LIKE 'B%'
ORDER BY LastName, FirstName

OPEN contact_cursor

-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- in the SELECT statement. 

FETCH NEXT FROM contact_cursor
INTO @LastName, @FirstName

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN

   -- Concatenate and display the current values in the variables.
   PRINT 'Contact Name: ' + @FirstName + ' ' +  @LastName

   -- This is executed as long as the previous fetch succeeds.
   FETCH NEXT FROM contact_cursor
   INTO @LastName, @FirstName
END

CLOSE contact_cursor
DEALLOCATE contact_cursor
GO