




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Oracle 数据库基础实验4 PL/SQL 块结构,条件、循环语句的使用实验学时】2学时实验目的】1. 学习匿名PL/SQL块的基本结构。2. 了解PL/SQL的基本数据类型。3. 掌握PL/SQL中变量和常量的定义方法。4. 学会在PL/SQL程序中使用复合数据类型:记录和集合。5. 学会使用selectinto语句返回一行数据。6. 练习在PL/SQL中使用DML语句和动态SQL语句。7. 熟练掌握PL/SQL中的选择和循环语句的使用。实验内容】1. 定义一个 PL/SQL 块,向屏幕输出 hello world !1 declare2 begin3 dbms_output.put_li
2、ne(hello world!);4* end;SQL / hello world!PL/SQL 过程已成功完成。2. 定义一个 PL/SQL 块,将输入的字符串中大小写相互转换后向屏 幕输出。例如,输入abDCe,输出ABdcEdeclarestr1 varchar2(20):=&str1;str2 varchar2(2);str3 varchar2(20);len number(10);leng number(10);beginlen:=1;leng:=length(str1);while len=a and str2=A and str2=Z thenstr3:=str3|lower(s
3、tr2);end if;len:=len+1;end loop;dbms_output.put_line(str3); end;1declare2str1 varchar2(20):=&str1;3str2 varchar2(2);4str3 varchar2(20);5len number(10);6leng number(10);7begin8len:=1;9leng:=length(str1);10while len=a and str2=A and str2 /输入 str1 的值 : dfaERERTdfdEdf原值2: str1 varchar2(20):=&str1;新值2: s
4、tr1 varchar2(20):=dfaERERTdfdEdf;DFAerertDFDeDFPL/SQL 过程已成功完成。3. 定义一个 PL/SQL 块,完成如下功能:输入一个 3 位数,输出其 各个数位上的数字。declareabc number(20):=&abc;a number(4);b number(4);c number(4);begin a:=floor(abc/100); b:=mod(floor(abc/10),10); c:=mod(abc,10);dbms_output.put_line(a);dbms_output.put_line(b); dbms_output.
5、put_line(c);end;1 declare2 abc number(20):=&abc;3 a number(4);4 b number(4);5 c number(4);6 begin7 a:=floor(abc/100);8 b:=mod(floor(abc/10),10);9 c:=mod(abc,10);10 dbms_output.put_line(a);11 dbms_output.put_line(b);12 dbms_output.put_line(c);13* end;SQL /输入 abc 的值 : 346原值2: abc number(20):=&abc;新值2:
6、 abc number(20):=346;346PL/SQL 过程已成功完成。4. 编写 PL/SQL 程序,程序的功能是:输入员工号,输出该员工经 理的姓名。1 declare2 v_empno emp.empno%type:=&v_emp;3 v_ename emp.ename%type;4 begin5 select ename6 into v_ename7 from emp8 where empno in(select mgr from emp where empno=v_empno);9 dbms_output.put_line(v_empno| manager is |v_enam
7、e);10* end;SQL /输入 v_emp 的值 : 7369原值2:v_empno emp.empno%type:=&v_emp;新值2:v_empno emp.empno%type:=7369;7369 manager is FORDPL/SQL 过程已成功完成。SQL /输入 v_emp 的值 : 7566原值2:v_empno emp.empno%type:=&v_emp;新值2:v_empno emp.empno%type:=7566;7566 manager is KINGPL/SQL 过程已成功完成。5. 编写PL/SQL程序,根据输入的员工号,若职务是 CLERK提高工资
8、 1%。工作为 CLERK 1 declare2 v_empno emp.empno%type:=&v_emp;3 v_sal emp.sal%type;4 begin5 select sal into v_sal from emp where empno=v_empno;6 dbms_output.put_line(old sal is|v_sal);7 update emp8 set sal=sal*1.019 where job=CLERK and empno=v_empno;10 select sal into v_sal from emp where empno=v_empno;11
9、 dbms_output.put_line(new sal is|v_sal);12* end;13 /输入 v_emp 的值 : 7934原值2: v_empno emp.empno%type:=&v_emp;新值2: v_empno emp.empno%type:=7934;old sal is1300new sal is1313PL/SQL 过程已成功完成。 工作不是 CLERK 1 declare2 v_empno emp.empno%type:=&v_emp;3 v_sal emp.sal%type;4 begin5 select sal into v_sal from emp wh
10、ere empno=v_empno;6 dbms_output.put_line(old sal is|v_sal);7 update emp8 set sal=sal*1.019 where job=CLERK and empno=v_empno;10 select sal into v_sal from emp where empno=v_empno;11 dbms_output.put_line(new sal is|v_sal);end;12* end;13 /输入 v_emp 的值 : 7566原值 2:v_empno emp.empno%type:=&v_emp; 新值 2: v_
11、empno emp.empno%type:=7566; old sal is2975new sal is2975PL/SQL 过程已成功完成。6. 编写 PL/SQL 程序,程序的功能是:输入员工号,如果该员工工 龄在 20 年之上,工资提高 10%。1 declare2 v_empno emp.empno%type:=&v_emp;3 v_sal emp.sal%type;4 v_age number(4,2);5 begin6 select sal into v_sal from emp where empno=v_empno;7 dbms_output.put_line(old sal
12、is|v_sal);8 update emp9 set sal=sal*1.110 where empno=v_empno and (sysdate-hiredate)/36520;11 select sal,(sysdate-hiredate)/365 age into v_sal,v_age from empwhere empno=v_empno;12 dbms_output.put_line(work time is |v_age| new sal is|v_sal);13* end;SQL /输入 v_emp 的值 : 7844原值2: v_empno emp.empno%type:=
13、&v_emp;新值2: v_empno emp.empno%type:=7844;old sal is1500work time is 29.74 new sal is1650PL/SQL 过程已成功完成。7. 编写 PL/SQL 程序,根据输入的员工号,输出该员工所在的部门 名。1 declare2 v_empno emp.empno%type:=&v_emp;3 v_dname dept.dname%type;4 begin5 select dname6 into v_dname7 from dept8 where deptno in(select deptno from emp wher
14、e empno=v_empno);9 dbms_output.put_line(v_empno| is in |v_dname);10* end;SQL /输入 v_emp 的值 : 7369原值2:v_empno emp.empno%type:=&v_emp;新值2:v_empno emp.empno%type:=7369;7369 is in RESEARCHPL/SQL 过程已成功完成。8. 使用 index by 表复合数据类型,提取员工表 emp 中的员工姓名 ename和员工职位job。1 declare2 cursor c is select * from emp;3 type
15、table_emp_type is table of emp%rowtype index by binary_integer;4 table_emp table_emp_type;5 i binary_integer:=1;6 num number;7 begin8 select count(empno)9 into num10 from emp;11 dbms_output.put_line(num);12 open c;13 for i in 1.num loop14 fetch c into table_emp(i);15 dbms_output.put_line(table_emp(i
16、).ename|,|table_emp(i).job);16 end loop;17* end;18 /14SMITH,CLERK ALLEN,SALESMAN WARD,SALESMAN JONES,MANAGER MARTIN,SALESMAN BLAKE,MANAGER CLARK,MANAGER SCOTT,ANALYST KING,PRESIDENT TURNER,SALESMAN ADAMS,CLERK JAMES,CLERK FORD,ANALYST MILLER,CLERKPL/SQL 过程已成功完成。9. 用动态 SQL 语句创建一个表格。 (表结构自行设计)1 declar
17、e2 table_name varchar2(20):=&table_name;3 line1 varchar2(10):=&line1;4 datatype1 varchar2(20):=number;5 line2 varchar2(20):=&line2;6 datatype2 varchar2(20):=varchar2(3);7 str_sql varchar2(500);8 begin9 str_sql:=create table| |table_name|(|line1| |datatype1|,|line2| |dataty10 dbms_output.put_line(str_sql);11 execute immediate str_sql;12* end;SQL /输入 table_name 的值 : person原值2:table_name varchar2(20):=&table_name;新值2:table_name varchar2(20):=person;输入 line1 的值 : name原值3:line1 varchar2(10):=&line1;新值3:line
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论