实验5 SQL - PL编程基础(3)

2019-08-03 12:39

v_salary NUMBER (10,2) );

rsm_emp remp;

BEGIN

SELECT empno,ename,sal INTO rsm_emp.v_empno,rsm_emp.v_ename,rsm_emp.v_salary FROM emp

WHERE sal =(SELECT MAX(sal) FROM emp); --求出工资最高的雇员

DBMS_OUTPUT.PUT_LINE ('Highest Paid Employee is '||rsm_emp.v_ename); DBMS_OUTPUT.PUT_LINE ('Id is '||rsm_emp.v_empno ||'Salary ' ||

to_char(rsm_emp.v_salary,'999,999.99'));

END;

运行结果为:

(3)--使用记录类型3:

set serveroutput on; DECLARE

TYPE remp IS RECORD( v_empno number(10), v_ename VARCHAR2 (20), v_salary NUMBER (10,2) );

rsm_emp remp; BEGIN

SELECT empno,ename,sal INTO rsm_emp FROM emp

WHERE sal =(SELECT MAX(sal) FROM emp); --求出工资最高的雇员

DBMS_OUTPUT.PUT_LINE ('Highest Paid Employee is '||rsm_emp.v_ename); DBMS_OUTPUT.PUT_LINE ('Id is '||rsm_emp.v_empno ||'Salary ' ||

to_char(rsm_emp.v_salary,'999,999.99'));

END;

运行结果为:

第 11 页 共 24 页

2.记录表类型

阅读以下程序,理解其功能,给出运行结果。

--一维表类型(相当于基本数据类型元素组成的一维数组) declare

type tabletype1 is table of varchar2(4) index by binary_integer;

--type tabletype2 is table of employees.last_name%type index by binary_integer; table1 tabletype1; --table2 tabletype2; begin

table1(1):='大学'; table1(2):='大专'; --table2(1):=88; --table2(2):=55;

dbms_output.put_line(table1(1)||table1(2)); --dbms_output.put_line(table1(2)||table2(2)); end; /

运行结果为:

/*

在定义好的表类型变量里,可以使用count、delete、first、last、next、exists和prior

等属性进行操作,使用方法为“表变量名.属性”,返回的是数字。

*/

--使用表类型变量属性

SET SERVEROUTPUT ON; Declare type tabletype1 is table of varchar2(9) index by binary_integer; table1 tabletype1; i binary_integer :=11; begin table1(1):='成都市'; table1(5):='北京市'; table1(3):='青岛市';

第 12 页 共 24 页

table1(11):='青岛市'; dbms_output.put_line('总记录数:'||to_char(table1.count)); dbms_output.put_line('第一条记录:'||table1.first); dbms_output.put_line('最后条记录:'||table1.last); dbms_output.put_line('第3条的前一条记录:'||table1.prior(3)); dbms_output.put_line('第5条的后一条记录:'||table1.next(5)); if(table1.exists(i)) then dbms_output.put_line('该条记录值为:' || table1(i)); else dbms_output.put_line('该条记录不存在!'); end if; table1.delete(11); --table1.delete(12); dbms_output.put_line('总记录数:'||to_char(table1.count)); dbms_output.put_line('第一条记录:'||table1.first); dbms_output.put_line('最后条记录:'||table1.last); dbms_output.put_line('第3条的前一条记录:'||table1.prior(3)); dbms_output.put_line('第5条的后一条记录:'||table1.next(5)); end; /

运行结果为:

5.5. 复合变量的使用

1. 完整定义一个记录类型的变量,了解其简单应用。

阅读以下程序,理解其功能,给出运行结果。 Declare

Type myrecord Is Record( r_recordnumber Number(4), r_currentdate Date );

v_myrecord myrecord; Begin

第 13 页 共 24 页

Select *

Into v_myrecord From testtable

Where recordnumber=80;

dbms_output.put_line('用记录类型的变量取出来的值为:'|| v_myrecord.r_recordnumber|| v_myrecord.r_currentdate); End;

给出运行结果:

2. 简单的%Rowtype定义变量的实例

阅读以下程序,理解其功能,给出运行结果。

Declare

v_myrow testtable%Rowtype; Begin

Select *

Into v_myrow From testtable

Where recordnumber=90;

dbms_output.put_line('用rowtype查询的结果是:

'||v_myrow.recordnumber||v_myrow.currentdate);

--dbms_output.put_line('用rowtype查询的结果是:

'||v_myrow.recordnumber||v_myrow.currentdate||myrow.Rowid);

--上句操作说明了行类型的变量中不会把oracle表中的rowid的值带进来 End;

给出运行结果:

3.定义与使用一维表变量

阅读以下程序,理解其功能,给出运行结果。

--维表变量,这种变量看起来有些类似于C语言中的一维数组 Declare

Type mytbtype1 Is Table Of Varchar2(4) Index By Binary_Integer;

Type mytbtype2 Is Table Of testtable.recordnumber%Type Index By Binary_Integer; tb1 mytbtype1; tb2 mytbtype2; Begin

tb1(1):='大学'; tb1(2):='大专'; tb2(1):=90; tb2(2):=70;

dbms_output.put_line(tb1(1)||tb2(1)); dbms_output.put_line(tb1(2)||tb2(2));

第 14 页 共 24 页

End;

给出运行结果:

4.定义与使用多维表变量

阅读以下程序,理解其功能,给出运行结果。

定义一个多维表变量,这就像一个二维数组,当然这个二维的数组的下标就有些区别于在编程语言中熟悉的二维数组,可以理解为一维存储的列名,而另一维则是存储与一维列名相对应的数据。

(1) 多维表变量简单应用

Declare

--这里区别于一维表变量的定义

Type multbtype Is Table Of testtable%Rowtype Index By Binary_Integer; multb multbtype; Begin

Select * Into multb(12) From testtable

Where recordnumber=88;

dbms_output.put_line('multb(12).recordnumber='|| multb(12).recordnumber|| ' multb(12).currentedate'|| multb(12).currentdate ); End;

给出运行结果:

(2) oracle中的‘数组’与其他编程语言中的数组的区别:

Declare

Type mytabletype Is Table Of Varchar2(9) Index By Binary_Integer; tb mytabletype; Begin

tb(1):='成都市'; tb(2):='太原市'; tb(3):='北京市';

dbms_output.put_line('记录总数:'||to_char(tb.Count));

dbms_output.put_line('第一条记录为:'||tb.First||'其值为:'||tb(tb.First)); dbms_output.put_line('最后条记录为:'||tb.Last||'其值为:'||tb(tb.Last)); dbms_output.put_line('第二条的前一条记录为:'||tb.Prior(2)||'其值为:'||tb(tb.Prior(2)));

第 15 页 共 24 页


实验5 SQL - PL编程基础(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:社区调解案例

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: