庆祝s

或多或少
整理的Oracle精辟问答题(6)
1.游标的机制是什么?
  ////游标是构建在PL?SQL中, 用来查询数据,获取记录集合的子针。      它可以让开发者一次访问集合中的某一行。

2.游标有哪种类型?分别是什么含义?
  ////1. 隐式游标
      2. 显示游标
      3. 循环游标
      4. REF游标
3.请问
  update student set mark=100 where stuid='0001';
  存在游标吗?
  如果存在游标,游标名是什么?
  ////存在 游标名是SQL的隐式游标

4.请问,当我删除一个表的记录时,如delete from studentinfo where flag='1';时,我如何获得我删除了多少条记录?
  ///// begin
         delete from studentinfo where flag = '1';
         dbms_output.put_line('更新了'||SQL%Rowcount||'行')
        end;

5.如何为访问studentinfo表下的记录来定义显式游标呢?请创建游标来显示此表条件满足clsid='c'下记录的所有成绩mark信息?

    select * from studentinfo
    create table studentinfo
    (
      s_id int,
      s_name varchar2(50),
      s_clsid varchar2(20)
    )

   declare
   
    clsid studentinfo.s_clsid%type;
    cursor cur_test is select s_clsid from studentinfo where s_clsid = 'c';
   begin
    open cur_test;
    loop
     fetch cur_test into clsid;
     exit when cur_test%notfound;
     dbms_output.put_line(cur_test%rowcount || '.班级号码'|| clsid);
    end loop;
    close cur_test;
   end;
6.在游标的循环过程中,如何判断已到记录尾?
  ////exit when 游标名%notfound
7.在将来的哪些地方,我们有可能要用到游标的那些属性呢?考虑之后写下来
  %found  如果执行最后一条FETCH语句成功返回行,则%found的值为True,
  %notfound 如果执行最后一条 FETCH语句未能提取行时,则 %notfound 的值为True
  %isopen 如果游标已经打开,则返回true 否则返回false;
  %rowcount 返回到目前为止游标提取的行数。 在第一次获取之前,为零.当fetch语句返回一行时,则该数加1.
 
8.请再一遍操作游标的全部过程
  declare
   clsid studentinfo.s_clsid%type;
   cursor cur_test is select s_clsid from studentinfo where s_clsid = 'c';
   begin
    open cur_test;
     loop
      fetch cur_test into clsid;
      exit when cur_test%notfound;
      dbms_output.put_line(cur_test%rowcount||'班级号码'||clsid);
     end loop;
    close cur_test;
   end;

9.请写一个游标程序来更新学生表的数据studentmark(stuid varchar2(5),clsid varchar2(5),testdate date,mark number).
  打开游标后,如果成clsid为sql,且testdate是在一年之前的日期的话,我们就将mark更改为90,请在表中插入部分数据后进行,最后显示出一共更新了多少行数据?
 
  create table studentmark(stuid varchar2(5),clsid varchar2(5),testdate date,mark number)
  select * from studentmark
  insert into studentmark values ('01','张三',sysdate,65.5)
  insert into studentmark values ('02','sql',trunc(to_date('2007-03-28','yyyy-mm-dd')),65.5);
  insert into studentmark values ('03','sql',trunc(to_date('2007-03-28','yyyy-mm-dd')),70);
  insert into studentmark values ('04','sql',trunc(to_date('2007-03-28','yyyy-mm-dd')),87);
  insert into studentmark values ('05','sql',trunc(to_date('2007-03-28','yyyy-mm-dd')),67);
 
  declare
   classid studentmark.clsid%type;
   testdate studentmark.testdate%type;
   cursor cur_test is select clsid from studentmark where months_between(sysdate,testdate) > 12 for update ;
   begin
    open cur_test;
     loop
      fetch cur_test into classid;
      exit when cur_test%notfound;
      update studentmark set mark = 90 where current of cur_test;
      dbms_output.put_line('更新了'||cur_test%rowcount);
     end loop;
    close cur_test;
   end;
   select * from studentmark
10.修改并重写第9题的游标,我希望游标是根据我的clsid输入值确定的(由外部输入),逐行循环每条记录,
   如果testdate是在一年之前的日期的话,我们就将mark更改为90,请在表中插入部分数据后进行,
   最后显示出一共更新了多少行数据?
   ////如果没有cid呢?
   
   declare
   classid studentmark.clsid%type;
   testdate studentmark.testdate%type;
   cursor cur_test is select clsid from studentmark where months_between(sysdate,testdate) > 12 and clsid = '&cid'for update ;
   begin
    open cur_test;
     loop
      fetch cur_test into classid;
      exit when cur_test%notfound;
      update studentmark set mark = 90 where current of cur_test;
     
     end loop;
     dbms_output.put_line('更新了'||cur_test%rowcount);
    close cur_test;
   end;
11.游标必须显式的找开,提取和关闭吗?
   如果不是,在什么情况下是这样?
  --在显式游标下

12.循环游标中的记录变量需要定义吗?它是何种类型?它在循环体之外还可用吗?(对比for循环)
不需要 如下面的 e 就没有定义

  declare
   cursor cur_test is
   select * from studentmark;
   begin
    for e in cur_test
    loop
     dbms_output.put_line('编号: '|| e.stuid);
    end loop;
   end;

13.何为强类型ref游标,何为弱类型ref游标?

没有return 语句的Ref游标 是弱类型的,允许使用动态SQL
有return语句的REF游标 是强类型的,不允许使用动态SQL   

14.游标变量是如何定义的?请定义一个游标变量curStudent?
declare
type refcur_test is ref cursor;
return refcur_test;  

15.请写出从声明,定义到打开游标的全过程,来打开表student下的记录。
///////////////////////?????????????????????????????
  declare
  type refcur_test is ref cursor;
  refcur refcur_test;
  stuinfo student%rowtype;
  begin
    open refcur for select * from student;
     while refcur%found
     loop
      dbms_output.put_line(stuinfo.stuid);
      fetch refcur into stuinfo;
     end loop;
    close refcur;
  end;
  commit;
  select * from student

16.使用游标变量时有哪些限制。
  //////////////
 -- 声明游标 和 打开游标变量 提取 关闭游标变量

17.有表teacher(name varchar2(20),phone varchar2(12),address varchar2(20))
     表student(name varchar2(20),phone varchar2(12),address varchar2(20))
   表建好后,分别插入部分记录
   现在要求根据输入值来确定显示哪个表的记录1显示teacher表,2显示student表,显示内容是name和address的组合,如***住在.....地方.
   请通过游标显示表中的所有记录
create table teacher(name varchar2(20),phone varchar2(12),address varchar2(20))
create table student(name varchar2(20),phone varchar2(12),address varchar2(20))
insert into teacher values ('刘老师','123456','石家庄老虎区');
insert into student values ('王雪松','11111122','北京海淀');
select * from teacher
select * from student
accept tab prompt 'asdfdsfas';


declare
type refcur_test is ref cursor;
refcur refcur_test;
qname varchar(20);
qaddress varchar(20);
selection varchar2(2) := '&tab';

begin
 if selection = '1' then
  open refcur for select name ,address from teacher;
 elsif selection = '2' then
  open refcur for select name , address from student;
 else
  dbms_output.put_line('请输入老师信息(1)或同学信息(2)');
  return;
  end if;
  loop
  fetch refcur into qname,qaddress;
  exit when refcur%notfound;
  dbms_output.put_line('名字是'||qname||'地址是'||qaddress);
  end loop;
end;

18.修改上题,在获得显示的是teacher和student表之后,再要求用户输入显示何人信息,此时再通过游标显示姓名相符之人的地址信息。(动态SQL语句的游标)
?????????????????????????????????????????????

declare
type refcur_test is ref cursor;
refcur refcur_test;
qname varchar(20);
qaddress varchar(20);
selection varchar2(2) := '&tab';

begin
 if selection = '1' then
  open refcur for select name ,address from teacher;
 elsif selection = '2' then
  open refcur for select name , address from student;
 else
  dbms_output.put_line('请输入老师信息(1)或同学信息(2)');
  return;
  end if;
  loop
  fetch refcur into qname,qaddress;
  exit when refcur%notfound;
  dbms_output.put_line('名字是'||qname||'地址是'||qaddress);
  end loop;
end;




19.游标变量属于哪种类型的游标?
///ref游标  

20.显式游标是在哪部分声明的?
  declare

posted on 2008-04-30 20:15  庆祝  阅读(484)  评论(0)    收藏  举报