www.Orasql.Com
 
用户名: 密码:  验证码: 注册|找回密码 oracleawen's blog  
 
         当前位置 >> 技术论坛  发表帖子     论坛版块     查找帖子
plsql表和记录类型-技术论坛||Oracle高手之路—Orasql.Com!
            plsql表和记录类型
 
发贴人:oracle_awen
oracleawen@gmail.com448315233127.0.0.1
用户级别:管理员
发贴总数:1245
注册时间:2011-05-05 09:23:03
2011-05-30 11:05:15
--用下标给PL/SQL表中的记录赋值 declare type emptabletype is table of varchar2(10) index by binary_integer; emp_name emptabletype; begin emp_name(-2):='tom'; emp_name(3):='mike'; dbms_output.put_line(emp_name(-2)); dbms_output.put_line(emp_name(3)); end; ------------------------------------------------------------------------------------------------------ --用FOR循环给PL/SQL表赋值 declare type stutabletype is table of varchar2(10) index by binary_integer; stu stutabletype; begin stu(1):='mike'; stu(2):='tom'; for i in 1..10 loop stu(i):='N'||i; dbms_output.put_line('第'||i||'记录的值是'||stu(i)); end loop; end; ------------------------------------------------------------------------------------------------- --创建一张表,用FOR循环插入数据 create table student(stuid number,stuname varchar2(10),sex char(6)) declare begin for i in 1..10 loop insert into student values(i,'S'||i,'boy'); end loop; end; declare type stutabletype is table of varchar2(10) index by binary_integer; stu stutabletype; cursor c is select stuname from student; e student.stuname%type; i int:=0; begin open c; loop fetch c into e; exit when c%notfound; stu(i):=e; dbms_output.put_line(stu(i)); i:=i+1; end loop; i:=0; loop exit when i=stu.count; insert into student2(stuname) values(stu(i)); i:=i+1; end loop; end; ------------------------------------------------------------------------------------------------------- declare type stutabletype is table of student%rowtype index by binary_integer; stu stutabletype; stu_id student.stuid%type; stu_name student.stuname%type; stu_sex student.sex%type; cursor c is select stuid,stuname,sex from student; i int:=1; begin open c; loop fetch c into stu_id,stu_name,stu_sex; exit when c%notfound; stu(i).stuid:=stu_id; stu(i).stuname:=stu_name; stu(i).sex:=stu_sex; i:=i+1; end loop; i:=1; loop exit when i=stu.count; insert into student2 values(stu(i).stuid,stu(i).stuname,stu(i).sex); i:=i+1; end loop; end; --批量绑定 declare type stutabletype is table of student%rowtype index by binary_integer; stu stutabletype; i int:=1; begin select stuid,stuname,sex bulk collect into stu from student; loop exit when i=stu.count; insert into student2 values(stu(i).stuid,stu(i).stuname,stu(i).sex); i:=i+1; end loop; end; declare type area is table of number index by varchar2(10); a area; begin a('北京'):=1; a('上海'):=2; a('广州'):=3; dbms_output.put_line('第一个元素索引是'||a.first); dbms_output.put_line('最后一个元素索引是'||a.last); dbms_output.put_line('第一个元素是'||a(a.first)); dbms_output.put_line('最后一个元素是'||a(a.last)); end;;

      
 
  
 Orasql.Com|E-Mail:oracleawen@gmail.com|Phone:15271942915|Copyright © 2011|All Rights Reserved|