实验八
实验名称:游标的建立与使用(2课时)
一、实验目的
理解游标的概念、定义方法和使用方法。
二、实验环境
采用Client/Server模式,学生为客户端,是MS SQL SERVER 2000的中文客户端。
登录用户名是:学号;密码为:****** 。
用户名和密码以任课老师给出为准。
三、实验示例
1、利用游标选取业务部门的员工编号和姓名,并执行游标。
DECLARE cur_emp SCROLL cursor FOR
SELECT emp_no,emo_name
FROM employee
WHERE dept=’业务’
ORDER BY emp_no
执行:
OPEN cur_emp
SELECT ”cursor打开状态”=@@ERROR
SELECT ”cursor内数据条数”=@@CURSOR_ROWS
FETCH NEXT FROM cur_emp
SELECT ”cursor读取状态”=@@ FETCH _ST ATUS
四、实验内容与步骤
1、利用游标查找所有女业务员的基本情况。
declare cur_emp scroll cursor for
select*
from employee
where sex='F'and title='职员'
order by emp_no
open cur_emp
fetch next from cur_emp
while(@@FETCH_STATUS<>-1)
begin
fetch next from cur_emp
end
close cur_emp
deallocate cur_emp
2、创建一游标,逐行显示表customer.的记录,并且用WHILE结构来测试游标的函数
@@FETCH_STA TUS的返回值
declare cur_cus_new scroll cursor for
select*
from customer
order by cust_id
open cur_cus_new
select "cursor打开状态"=@@ERROR
select "cursor内数据条数"=@@CURSOR_ROWS
while(@@FETCH_STATUS=0)
begin
fetch next from cur_cus_new
select "fecth_status值"=@@FETCH_STATUS
end
select "cursor读取状态"=cursor_status('variable','@cur_cus_new')
五、实验报告。