PLSQL learning by self
- 格式:doc
- 大小:119.00 KB
- 文档页数:15
//author 满晨晨//time 2009 4 20上午字符型charvarchar2long 2000个非汉字字符1000汉字ncharnvarchar2数值型number方案schema用户名:表空间(表索引视图)lob 大对象blob 4g初始化init emp_blobcdml存二进制clob 4g存字符初始化init emp_clobcdml{insert delete update}使用%type定义变量定义记录类型变量使用&rowtype定义变量定义一维表类型变量定义多维表类型变量delcarev_empno EMP.empno%TYPE;v_ename EMP.ename%TYPE;beginselect sex into v_sexfrom t_demo;end;file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.20.txt2009-4-27 9:11:38 file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.21.txt//author 满晨晨//time 2009 4 21上午sql>conn scott/tiger/@oraclesql>ed 0421sqlupper()全规范为大写lower()全规范为小写set serveroutput ondeclaretype deptrec//部门表is record 声明记录类型部分字段(v_deptno dept.deptno%type,v_dname dept.dname%type);deptrec1 deptrec;beginselect deptno,dname into deptrec1 from dept where deptno=30;dbms_output.put_line(deptrec1.v_dname);dbms_output.put_line(deptrec1.v_deptno);end;create table t_empasselect * from scott.empwhere 1=1 全部复制过来%rowtype可以使变量获得字段的数据类型使用%rowtype可以使变量获得整个记录的数据类型比较两者不同:变量名数据表名.列名%type,变量名数据表名%rowtpye定义名字为mytable的复合类型变量,与testtable数据表结构相同set serveroutput ondeclaremytable testtable%rowtype;beginselect * into mytable from tempuser.testtablewhere recordnumber=88;doms_output_put.line(mytable.currentdate);end;表类型变量用来表示一维或多维数组一维表类型type 表类型is table of 类型index by binary_integer;表变量名表类型;//使用表类型变量类型可以使前面的类型的定义,index by binary_integer子句代表以符号整数为索引,这一访问表类型变量中的数据方法就是file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.21.txt(第1/4 页)2009-4-27 9:11:39file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.21.txt‘表变量名(索引符号整数)’sqlplusw /nogondeclaretype tabletype1 is table of varchar2(8) index by binary_integer;type tabletpye2 is table of tempuser.testtable.recordnumber%tpye index by binary_integer; table1 tabletype1;table2 tabletype2;begintable1(1):='大学';table1(2):='大专';table2(1):=88;table2(2):=55;dbms_output.put_line(table1(1)||table2(1));dbms_output.put_line(table1(2)||table2(2));end;||使连接字符串的运算符控制结构条件结构if switch循环结构loop exit for while循环结构goto null作业count first next lasy priror 函数<1000 显示工资太低>1000 <3000 显示工资中等>3000 显示工资较高实践从客户端输入一字符如果字符为A 输出信息‘A的标准是90分以上’如果字符为B 输出信息‘B的标准是80分至90分之间’如果字符为C输出信息‘C的标准是60之80分之间’否则输出信息不及格declarev_empno emp.empno%type:=&empno;v_ename emp.ename%type;beginselect empno, ename into v_empno,v_ename from emp where empno=v_empno; dbms_output.put_line(v_ename);exceptionwhen others thendbms_output.put_line(' your empno is null,ename is null');end;declarev_grade char(1):=upper(&p_grade);v_result varchar2(20);file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.21.txt(第2/4 页)2009-4-27 9:11:39 file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.21.txtbeginv_result:=case v_gradewhen 'A' then '90 分以上'when 'B' then '80分到90分之间'when 'C' then '60分到80分之间'else '不及格'end;dbms_output.put_line('grade'||v_grade||'result'||v_result);end;declarev_name emp.ename%type;v_sal emp.sal%tpye:=&sal;//显示输入sal的值&为输入接入点赋给v_salv_char varchar2(30);select ename,sal into v_name,v_sal from emp where sal=v_sal;if v_sal<1000 thenv_char:='sal is low';elsif v_sal>=1000 and v_sal<=3000 thenv_char:='sal is normal';else v_sal>3000 thenv_char:='sal is high';else v_char:='lost data';end if;dbms_output.put_line('name'||v_name||'sal'||v_char);end;if thenelseelsif thenend ifcase selectorwhen thenwhen thenend case;loop执行语句exit when 退出条件end loopfor count in 1..10000 loop执行语句file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.21.txt(第3/4 页)2009-4-27 9:11:39 file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.21.txtend loop;file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.21.txt(第4/4 页)2009-4-27 9:11:39 file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.22.txt//author 满晨晨//time 2009 4 22上午conn scott/tiger@STUF1DB查询表结构和表空间SHOW USERDESC USER_TABLES查询表的结构不显示内容select 查询表的内容SELECT TABLE_NAME,TABLESPACE_NAME FROM USER_TABLES table 不能用or replace 只能先删除再给重新建个表视图view1 查询速度快只做一次编译2 调用简单虚表不占物理内存空间视图名称以vi开头create view vi_depttempasselect e.empno,,d.dname from emo e join dept de.deptno=d.deptno;建立了view 虚表只是建立查询view虚表只存在内存里一关机就没了select* from vi_deptemp 查询具体内容更新视图update vi_demptemp set ename='张学友'where empno='7788'commit 提交对视图有影响对实际数据也更改delete from vi_的皮特名片where empno=7788commit 提交对视图有影响对实际数据也更改复制别人的表create table t_empasselect * from scott.emp where 1=1 或者1!=1create table t_deptasselect * from scott.deptfile:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.22.txt(第1/5 页)2009-4-27 9:11:40file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.22.txtupdate 更新视图时候视图来源多个表时候无法更新必须使用触发器instead of 类型触发器而视图只有一个单表时候可以更新select job,sum(sal) as totalsal from t_emp group by job having job='clerk'视图查询时候聚合函数要给它重新命名;wheregroup byhavingorder bygroup by having 分组查询是将查询结果按照字段分组HAVING相当与WHERE的限制条件查询select empno,ename,job,salfrom scott.empgroup by job,empno,ename,sal ---按字段分组having sal<=2000 --分组后是否符合条件where 检查每条记录是否符合条件having检查分组后的各组是否满足条件having 只能配合group by 使用在使用join ,group by时候要使用视图结构触发器使特定事件出现的时候自动执行的代码块类似于存储过程但是用户不能直接调用他们当insert emp时候记录此操作过程到emp_log中业务逻辑自动执行功能1 允许/限制对表的修改2 自动生成派生列比如自增字段3 提供审计和日志记录4防止无效的事务处理5 启用复杂的业务逻辑触发器不会通知用户便改变了用户的输入值】语法create or replace trigger name tri_前缀_emp表明_insert事件tri_emp_insertafter /before /instead of(视图时候多表) 执行时机insert/update/delete 表之前之后替换视图什么事件on 表明视图名declarebeginfile:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.22.txt(第2/5 页)2009-4-27 9:11:40file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.22.txtdml语句要commit,ddl语句end 触发器名称(也可以不写)create table t_emp_log(who varchar2(10) not null,action varchar2(10) not null,actime date);create or replace trigger tri_emp_insertbefore inserton t_empbegininsert into t_emp_log(who,action,actime)values(user,'insert',sysdate);end;raise_application_error(-20001,'你不能访问或修改这个表')ora-20001:你不能访问或修改这个表往里面插入东西如果是你这个用户的话可以操作如果不是弹出一个错误create or replace trigger tri_emp_insert1before inserton t_empbeginif user!='scott' thenraise_application_error(-20001,'你不能访问或修改这个表');end if;删除两条记录file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.22.txt(第3/5 页)2009-4-27 9:11:40file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.22.txt删除t_emp表中的数据同时把删除的数据保存到新的表a_emp中去create table a_empasselect * from t_emp where 1!=1;CREATE OR REPLACE TRIGGER TRI_t_empbefore delete ON T_empREFERENCING NEW AS NEWROWFOR EACH ROWdeclareBEGININSERT INTO a_empVALUEE(:NEWROW.empno,:NEWROW.ename,:NEWROW.job,:NEWROW.MGR,:NEWROW.HI REDATE,:NEWROW.Sal,:M,:NEWROW.deptno);END TRI_t_emp;触发器;statement trrow trend;delete 删除段drop删除表视图触发器行级触发器new -insert oid 无效old-delete new 无效dne rowfile:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.22.txt(第4/5 页)2009-4-27 9:11:40file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.22.txtnew old -updatefor each row 循环遍历每一行带业务逻辑的触发器如插入T_DEMO3数据同时插入T_DEMO2,条件是工资要大于4500rmb的记录CREATE OR REPLACE TRIGGER TRI_TDEMO3AFTER INSERT ON T_DEMO3REFERENCING NEW AS NEWROWFOR EACH ROWWHEN(NEWROW.SALARY>=4500)BEGININSERT INTO T_DEMO2VALUEE(:NEWROW.ID,:NEWROW.TYPE,:,:NEWROW.SALARY,:NEWROW. C_DATE);END TRI_TDEMO3;file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.22.txt(第5/5 页)2009-4-27 9:11:40file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.23.txt//author 满晨晨//time 2009 4 23上午系级trigger监听到系统发生事件之后自动执行的代码块所以是after!!!!deleting 当向一个表执行delete动作时出发deleting事件inserting 当向一个表执行insert动作时出发inserting事件updating 当向一个表执行update动作时出发updating事件都是返回true或者falseif deleting thenelsif inserting thenelsif updating thenelse raise_application_errorcreate table t_emp_log2(who varchar2(10) not null,action varchar2(10) not null,actime date);create or replace trigger tri_emp2after insert or update or deleteon t_empbeginif updating theninsert into t_emp_log2(who,action,actime)values(user,'update',sysdate);elsif deleting theninsert into t_emp_log2(who,action,actime)values(user,'delete',sysdate);elsif inserting theninsert into t_emp_log2(who,action,actime)values(user,'insert',sysdate);end if;end;对操作行为进行表述时候不涉及数据正常写假如对操作行为更改的数据进行数据操作时候referencing new as newrowfor each row行级触发器修改的是行的所有列的数据的时候referencing new as newrowfor each row列级触发器修改的是某个列的数据的时候file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.23.txt(第1/3 页)2009-4-27 9:11:40file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.23.txtafter insert or update of enameon t_empreferencing new as newrowfor each rowcreate or replace trigger tri_emp2after insert or updateon t_empbeginif updating theninsert into t_emp_log2(who,action,actime)values(user,'update',sysdate);elsif deleting theninsert into t_emp_log2(who,action,actime)values(user,'delete',sysdate);elsif inserting theninsert into t_emp_log2(who,action,actime)values(user,'insert',sysdate);end if;end;更新的数据保存到新表中用newrow把删除的数据保存到列触发器只能针对update 因为insert 插入数据的时候肯定是更改了行的所有列create or replace trigger tri_emp4after update of enameon t_empreferencing new as newrowfor each rowbeginif updating theninsert into t_emp_log2(who,action,actime)values(user,'update',sysdate);INSERT INTO temp_new(ename,empno)VALUEE(:newrow.ename,:newrow.empno);elsif inserting theninsert into t_emp_log2(who,action,actime)values(user,'insert',sysdate);INSERT INTO temp_new(ename,empno)VALUEE(:newrow.ename,:newrow.empno);end if;end;行级触发器new保存更改后的数据old 保存删除之前的数据update delete insert列级触发器只能update更改某列视图如果是多表的话不能用update更新触发器instead只能用于更新视图时候用file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.23.txt(第2/3 页)2009-4-27 9:11:40file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.23.txt当update更新视图时候同时要更新基表(如果视图时单表的时候用update直接就可以更新更新视图同时也更新基表可当视图时多表的时候用update就不可以直接更新了必须用触发器)/**create view t1_t2asselect e.empno,e.ename,e.job,d.dname from t_emp e,t_dept d where d.deptno=e.deptno;**/create or replace view t1_t2asselect e.empno,e.ename,e.job,d.dname from t_emp e,t_dept d where d.deptno=e.deptno;create or replace trigger t1_t2instead of update on t1_t2referencing new as newrowfor each rowbeginupdate t_emp e set empno=:newrow.empno,ename=:newrow.ename,job=:newrow.job where empno=:newrow.empno;/**where指的是假如视图更新一个人的名字了那么也要更改基表怎么更改了不加where全部都改了加了where 假如视图更改的是1号为刘德华基表也要变成刘德华那么更改到基表用where判断基表号为1号更新ename**/update t_dept d set dname=:newrow.dname where deptno=(select deptno from t_emp where empno=:newrow.empno);end;old 和newcreate or replace trigger tir_t_emp2before update on t_empreferencing new as newrow old as oldrowfor each rowbeginINSERT INTO emp_NEW(EMPNO,ENAME,JOB,DEPTNO) update set对某一个数据进行更新更新之前的数据存在一个表里更新后的数据也放在一个表里values(:NEWROW.empno,:NEWROW.ename,:NEWROW.job,:NEWROW.deptno);更新后的数据保存在一个新的表中INSERT INTO emp_OLD(EMPNO,ENAME,JOB,DEPTNO)values(:OLDROW.empno,:OLDROW.ename,:OLDrow.job,:OLDROW.deptno);更新前的数据保存在一个新的表中end tir_t_emp2;file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.23.txt(第3/3 页)2009-4-27 9:11:40file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.24.txt//author 满晨晨//time 2009 4 24上午clob初始化为empty-clob()blob初始化为empty-clob()sequence序列序列号每次读取它会自动增加一般用在需要序列号排序的地方create sequence 序列名称increment by 增量start with 起始数字nomaxvalue/maxvalue 数字nocycle/cyclecache 数字;nomaxvaue 只能搭配nocycle 用maxvaue cycle 或者nocycle搭配用序列命名规则seq_数据库名称create sequence seq_dbstart withincrement by 1maxvalue 10nocyclecache 2;dual是经常用来系统测试的表序列名字.currval nextvalcreate sequence seq_1start with 1increment by 1maxvalue 100cycle/nocyclecache 2;创建序列1到100 然后再回到1到100declaresname number(3):=1;beginloopinsert into emp(empno,ename,hiredate,deptno)values(seq_1.nextval,'aa'||sname,sysdate,30); sname:=sname+1;exit when sname>1000;file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.24.txt(第1/4 页)2009-4-27 9:11:41file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.24.txtend loop;end;无法例程化石因为到了最大值了改改序列的循环maxvalue同一个语句多此调用nextvalue是同样的数值代理主键统一为ID没有实际含义只起到唯一标志一条记录的作用主键因为一般不能更改起到唯一标志的作用可是有些主键可以被更改如学号等等作主键代理主键就来作为唯一标志一定要选没有实际含义id varchar2(32) 一般用户max地址系统时间来定义存储过程把beginend之间的业务逻辑存下来好直接被引用procedurecreate or replace procedure 名称(para1 in/out datetype, in传入的参数(insert时候)在过程中不能被修改out 返回值在过程中可以被修改para1 in/out datetype)as /isbegindoend 过程名称;exec 存储过程的名字:调用存储过程存储过程不能用declare声明变量要想用声明变量()如上面的file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.24.txt(第2/4 页)2009-4-27 9:11:41file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.24.txtcreate or replace procedure p_b(v_id in varchar2,v_name in varchar2,v_psw in varchar2,v_address in varchar)asbegininsert into t_user(id,name,psw,address)values(v_id,v_name,v_psw,v_address);end;插入数据对内的所以用in如果对外输出的话变量用outexec p_b(354,546,'456sd','srf5');procedure变量中不能声明变量类型的大小create or replace procedure p_c(v_id in number(12))错误的这是自动扩展的12不能用定义大小也不能给它赋值in是给它赋值的出来的时候exec的时候给它赋值的create or replace procedure p_c(v_id in number:=1,v_name in number:=1,v_psw in number:=1,v_address in number:=1)asfile:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.24.txt(第3/4 页)2009-4-27 9:11:41file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.24.txtbeginloopinsert into t_user(id,name,psw,address)values('id'||v_id,'name'||v_name,'psw'||v_psw,'address'||v_address);v_id:=v_id+1;v_name:=v_name+1;v_psw:=v_psw+1;v_address:=v_address+1;exit when v_id>200,v_name>200,v_psw>200,v_address>200;end loopend;删除存储过程drop procedure 过程名();file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.24.txt(第4/4 页)2009-4-27 9:11:41file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.25.txt//author 满晨晨//time 2009 4 25上午游标存储过程视图触发器数据库实际应用中非常重要的四部分cursor 是系统为用户开设的一个数据缓冲区存放sql语句的执行结果每个游标都有自己的名字用户可以用sql语句逐以从游标中获取记录并赋给主变量交给主语言进一步处理主语言是面向记录的一组主变量一次只能存放一条记录仅用主变量并不能完全满足sql语句向应用程序输出数据的要求嵌入式sql引入了游标的概念用来协调这两种不同的处理方式cursor :resultsset 结果集recordposi 记录位置本质上游标实际上时一种从包括多条数据记录的结果集中每次提取一条记录的机制游标总是与一条sql选择语句相关联因为游标由结果集(可以使零条或者一条或者由相关的选择语句检索出的多条记录)和结果集中指向特定记录的游标位置组成当决定对结果集进行处理时必须声明一个指向该结果集的游标游标分类显示游标用户自己声明操作隐式游标oracle为所有操作语言自动声明和操作的一种游标显示游标声明cursor 游标名(变量名数据类型,...)return 返回类型selectstatement;打开open 游标名(实参列表)提取fetch 游标名into(variable_list,record_variable)关闭close%isopen%found %notfound%rowcount 返回当前位置为止游标读取的记录行数set serveroutput on;declaret_empno emp.empno%type;cursor mycursor isselect e.empno,e.ename from emp e where empno>t_empno;cursored mycursor%rowtype;beginfile:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.25.txt(第1/5 页)2009-4-27 9:11:42file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.25.txtt_empno:=10;open mycursor;if mycursor%isopen thenfetch mycursor into cursored;dbms_output.put_line(to_char(cursored.ename));elsedbms_output.put_line('no open!!');end if;close mycursor;end;set serveroutput on;declareeno emp.empno%type;e_name emp.ename%type;cursor mycurs1(var_name varchar2) isselect e.empno,e.ename from emp e where e.ename like var_name//like '%'||var_name||'%'; beginif mycurs1%isopen=false thenopen mycurs1('%A%');end if;fetch mysurs1 into eno,e_ename;while mysurs1%found loopdbms_output.put_line(eno||':'||e_name);if mycurs1%rowcount=4 thenexit;end if;fetch mycurs1 into eno,e_ename;end loop;close mycurs1;end;set serveroutput on;declaret_empno t_emp.empno%type;cursor mycursor isselect e.empno,e.ename from t_emp e where empno>t_empno;cursored mycursor%rowtype;begint_empno:=1000;open mycursor;if mycursor%isopen thenfetch mycursor into cursored;file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.25.txt(第2/5 页)2009-4-27 9:11:42file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.25.txtdbms_output.put_line(to_char(cursored.ename));elsedbms_output.put_line('no open!!');end if;while mycursor%foundloopdbms_output.put_line(t_empno||':'||to_char(cursored.ename));if mycursor%rowcount=4 thenexit;end if;fetch mycursor into t_empno,cursored.ename;end loop;close mycursor;end;set serveroutput on;declarecursor mycurs1 isselect e.empno,e.ename,e.deptno from t_emp e where e.deptno=20;beginfor employee in mycurs1loopif mycurs1%isopen thendbms_output.put_line('学号:'||employee.empno||'姓名:'||employee.ename||'部门:'||employee.deptno); end if;end loop;end;/**for游标**/创建包包一般是过程和函数的集合,对过程和函数进行更好的封装,一般不针对字段包的构成包括包头和包体包头仅仅是针对包中过程或者函数进行说明而没有实现create or replace package pack_t1is procedure sayhello(vname ,vachars);声明包的一个过程;end;file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.25.txt(第3/5 页)2009-4-27 9:11:42file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.25.txt包体是对包头中定义的过程函数的具体实现create or replace package body pack_t1isprocedure sayhello(vname varchar2);声明包的一个过程isdbms_output.put_line('hello'||vname);end;end;set serveroutput on;declaret_empno emp.empno%type;cursor mycursor isselect e.empno,e.ename from emp e where empno>t_empno;cursored mycursor%rowtype;begint_empno:=10;open mycursor;if mycursor%isopen thenfetch mycursor into cursored;dbms_output.put_line(to_char(cursored.ename));elsedbms_output.put_line('no open!!');end if;close mycursor;end;set serveroutput on;declareeno emp.empno%type;e_name emp.ename%type;cursor mycurs1(var_name varchar2) isselect e.empno,e.ename from emp e where e.ename like var_name//like '%'||var_name||'%'; beginif mycurs1%isopen=false thenopen mycurs1('%A%');end if;fetch mysurs1 into eno,e_ename;while mysurs1%found loopdbms_output.put_line(eno||':'||e_name);if mycurs1%rowcount=4 thenexit;file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.25.txt(第4/5 页)2009-4-27 9:11:42file:///E|/满晨晨的文档/培训/ORACLE/plsql/课堂记录/SQL课堂记录2009.4.25.txtend if;fetch mycurs1 into eno,e_ename;end loop;close mycurs1;end;set serveroutput on;declaret_empno t_emp.empno%type;cursor mycursor isselect e.empno,e.ename from t_emp e where empno>t_empno;cursored mycursor%rowtype;begint_empno:=1000;open mycursor;if mycursor%isopen thenfetch mycursor into cursored;dbms_output.put_line(to_char(cursored.ename));elsedbms_output.put_line('no open!!');end if;while mycursor%founddbms_output.put_line(t_empno||':'||to_char(cursored.ename)); if mycursor%rowcount=4 thenexit;end if;fetch mycursor into t_empno,cursored.ename;end loop;close mycursor;end;。