--匿名块set serveroutput on;begin dbms_output.put_line('this is an anonymous block');end;/ --不考虑无数据异常declare v_sname varchar2(200);begin select name into v_sname from students t where t.student_id=10381; dbms_output.put_line('name is'||v_sname);end;/--捕获空异常declare v_sname varchar2(200);begin select name into v_sname from students t where t.student_id=10381; dbms_output.put_line('name is'||v_sname); exception when no_data_found then dbms_output.put_line('no data found'); end;/--注释的例子,此例子包含CASE语法,%数据类型,&表示输入declare v_id teachers.teacher_id%type; v_job_title teachers.title%type;begin v_id:=&teacher_id; select title into v_job_title from teachers t where t.teacher_id=v_id; --将教师标号为V——id的教师职称赋值给变量 case when v_job_title='教授' then update teachers set wage=1.1*wage where teacher_id=v_id; when v_job_title='高级工程师' or v_job_title='副教授' then update teachers set wage=1.05*wage where teacher_id=v_id; else update teachers set wage=wage+100 where teacher_id=v_id; end case;end;/--注释的例子,此例子包含CASE语法,%数据类型,异常处理declare v_id teachers.teacher_id%type; v_job_title teachers.title%type;begin v_id:=&teacher_id; select title into v_job_title from teachers t where t.teacher_id=v_id; --将教师标号为V——id的教师职称赋值给变量 exception when no_data_found then dbms_output.put_line('no teachear be found'); case when v_job_title='教授' then update teachers set wage=1.1*wage where teacher_id=v_id; when v_job_title='高级工程师' or v_job_title='副教授' then update teachers set wage=1.05*wage where teacher_id=v_id; else update teachers set wage=wage+100 where teacher_id=v_id; end case;end;/--假如只有1个值,那么可以这样写,假如比较的条件比较多,那么换上面的写法declare v_id teachers.teacher_id%type; v_job_title teachers.title%type;begin v_id:=&teacher_id; select title into v_job_title from teachers t where t.teacher_id=v_id; --将教师标号为V——id的教师职称赋值给变量 exception when no_data_found then dbms_output.put_line('no teachear be found'); case v_job_title when '教授' then update teachers set wage=1.1*wage where teacher_id=v_id; when '高级工程师' then update teachers set wage=1.05*wage where teacher_id=v_id; else update teachers set wage=wage+100 where teacher_id=v_id; end case;end;/--在PLSQL中执行select语句,select * from departments;declare v_id departments.department_id%type; v_name departments.department_name%type; v_address departments.address%type; begin select * into v_id,v_name,v_address from departments where department_id=&department_id; dbms_output.put_line('系部名称:'||v_name); dbms_output.put_line('系部地址:'||v_address);end;/--行类型的数据类型 ,异常处理应该写到select into 之后?还是输出之后?还是只要有异常处理,写到哪里都没有关系,oracle会自动需找异常处理的代码?--假如有些异常不可预料,怎么写?declare v_student students%rowtype; begin select * into v_student from students where student_id =&student_id; dbms_output.put_line('name sex birthday'); dbms_output.put_line(v_student.name || v_student.sex||v_student.dob); exception when no_data_found then dbms_output.put_line('no data found'); /*在表中执行select into 这条语句,如果返回值为空,那么会抛出no_data_found异常, 如果返回的结果过多,那么则会抛出too_many_row异常*/ end; /--假如没有找到数据,什么都不做declare v_id teachers.teacher_id%type; v_title teachers.title%type;begin v_id:=&teacher_id; select title into v_title from teachers where teacher_id=v_id; exception when no_data_found then null; if v_title='' then update teachers set wage=1.1*wage where teacher_id=v_id; else update teachers set wage=wage+100 where teacher_id=v_id; end if;end;---多次elsif的例子declare v_id teachers.teacher_id%type; v_title teachers.title%type;begin v_id:=&teacher_id; select title into v_title from teachers where teacher_id=v_id; exception when no_data_found then null; if v_title='' then update teachers set wage=1.1*wage where teacher_id=v_id; elsif v_title='aaa' then update teachers set wage=wage+100 where teacher_id=v_id; elsif v_title='ccc' then null; else null; end if;end;--loop循环的例子create table total(n int ,reault int);declare v_i int:=1; v_sum int:=0;begin loop v_sum:=v_i+1; insert into total values (v_i,v_sum); exit when v_sum=10; v_i:=v_i+1; end loop;end;/select * from total;--while 循环的例子declare v_i INTEGER:=1; v_sum INTEGER:=0;begin while v_i<=10 loop v_sum:=v_i+1; insert into total values(v_i,v_Sum); v_i:=v_i+1; end loop; commit;end;/select * from total;--for循环的测试例子declare v_i int:=1; v_sum int :=1;begin for v_i in 1..100 loop v_sum:=v_i*1; insert into total values (v_i,v_sum); end loop; commit;end;/select * from total;?
详细解决方案
PLSQL开发基础-构造控制
热度:66 发布时间:2016-05-05 14:07:10.0
相关解决方案
- plsql 里怎么判断密码长度大于等于6
- java-plsql 有关问题
- PlSql 如何查询一张表使得orderID相同就合并那几行数据的某一行数据
- PLSQL 撤销有关问题
- win7x64上安装oraclex64版本后,plsql Developer无法登录的有关问题
- 如何用 PLSQL Developer 创建新的oracle数据库
- PLSQL 批量安插更新
- plsql Developer工具使用异常
- plsql 与 sqlplus 查询结果不一致,该怎么处理
- plsql oracle脚本,导出数据条目不一样?解决思路
- plsql 是不是有辦法將 Schema 的 Table 清單 拉出來成一個視窗
- PLSQL 新计算机装了 11g, 开启后找不到可以连接的资料库清单><
- 为啥监听没有启动,plsql dev客户端依然可以连接数据库呢
- 为何监听没有启动,plsql dev客户端依然可以连接数据库呢
- PLSQL 登录oracle数据库出现 “无法解析指定的连接标识符”异常
- plsql 客户端连接远程 oracle 服务查询卡住,该怎么处理
- plsql,该如何处理
- PLSQL develop连不上数据库解决思路
- Win7 64 安装oracle11g plsql 配置完后查询数据中文显示乱码,该怎么处理
- 求大神 PLSQL 使用,该怎么处理
- 求大神 PLSQL 使用,该如何解决
- plsql developer 到底如何显示dbms_output
- plsql 8.0怎么在windows server 2008下连64bit 的Oracle 11g client
- plsql 联接本地oracle连接不上 昨天还好使
- PLSQL 查询乱码有关问题
- plsql 没法弹出数据表对应的字段
- PLSQL Developer连接64位oracle 11.2.0.1.0遇到难以想象的有关问题
- plsql deveploer软件奇怪有关问题
- PLSQL developer从ORACLE中导出的TSV资料中有空行
- plsql 无监听程序,该如何解决