实验3 游标和视图.doc
文本预览下载声明
实验三 使用游标和视图
【开发语言及实现平台或实验环境】
Oracle 11g 快捷版
PL/SQL developer
【实验目的和要求】
掌握游标创建和使用步骤。
掌握使用游标提取数据集数据的主要方法。
掌握视图的基本原理。
掌握视图创建、修改、删除的方法。
掌握在PL/SQL developer中创建表、视图的方法
【实验内容及步骤】
DECLARE CURSOR 游标名
[ (参数列表) ]
IS
SELECT语句;
(2)打开游标。
OPEN 游标名 [ (参数列表) ];
(3)读取数据。
FETCH 游标名 INTO 变量列表;
(4)关闭游标。
CLOSE 游标名;
例1:创建一个名为pdct_name的游标,游标以productinfo表为查询表,并查询相关数据。
declare
Cursor pdct_cur
Is select * from productinfo;
cur_prodrcd productinfo%rowtype; --声明变量cur_prodrcd
Begin
Open pdct_cur;
Fetch pdct_cur into cur_prodrcd;
dbms_output.put_line(cur_prodrcd.productid||‘-’||cur_prodrcd.productname||’-’||cur_prodrcd.productprice);
Close pdct_cur;
end;
例2、通常显式游标只会返回一条结果,如果需要返回多条结果就要遍历结果集,使用Loop语句。
declare
Cursor pdct_loop_cur Is select productid, productname, productprice from productinfo where productprice2500;
cur_productid productinfo.productid%type;
cur_productname productinfo.productname%type;
cur_productprice productinfo.productprice%type;
Begin
Open pdct_loop_cur;
loop
Fetch pdct_loop_cur into cur_productid, cur_productname,cur_productprice;
exit when pdct_loop_cur%notfound;
dbms_output.put_line(‘产品ID:’||cur_productid||’产品名称:’||cur_productname||’产品价格:’||cur_productprice);
end loop;
Close pdct_loop_cur;
end;
例3、使用cursor for loop提取数据
declare
cursor cf1 is select productname, productprice from productinfo
where productprice1200;
begin
for curcf1 in cf1
loop
dbms_putout.put_line(名称:||curcf1.productname||产品价格:||curcf1.productprice);
end loop;
end;
2、视图的使用
CREATE [OR REPLACE] [[NO] FORCE] VIEW
[schema.]view
[(alias, ...) inline_constraint(s)]
[out_of_line_constraint(s)]
AS subquery
[
WITH{READ ONLY| CHECK OPTION[CONSTRAINT constraint]}
];
例1:以productinfo为基表创建一个简单视图:展示5行产地是“中国”的数据。
create or replace view simple_productinfo_vie
显示全部