将productinfo表中的数据根据不同的产品类型分类,把数据输出到屏幕
1.创建productinfo表
create table productinfo
(
product_id varchar2(64),
product_name varchar2(20),
product_price number(8,2),
quantity number(10),
category varchar2(64),
desperation varchar2(1000),
origin varchar2(10)
);
2.录入数据
– Add comments to the columns
comment on column productinfo.product_id
is ‘id’;
comment on column productinfo.product_name
is ‘商品名称’;
comment on column productinfo.product_price
is ‘商品价格’;
comment on column productinfo.quantity
is ‘商品数量’;
comment on column productinfo.category
is ‘商品分类’;
comment on column productinfo.desperation
is ‘商品描述’;
comment on column productinfo.origin
is ‘商品产地’;
– Create/Recreate primary, unique and foreign key constraints
alter table productinfo
add constraint key_product_id primary key (product_id);
insert into productinfo
(product_id,product_name,product_price,quantity,category,desperation,origin)
select ‘001’,‘平凡的世界’,50,5000,‘001’,null,null from dual
union all
select ‘002’,‘荆棘鸟’,60,4500,‘001’,null,null from dual
union all
select ‘003’,‘华为笔记本V1’,5400,300,‘002’,null,null from dual
union all
select ‘004’,‘小米音响’,1000,200,‘003’,null,null from dual;
select * from productinfo;
3.创建category表
create table category
(
category_id varchar2(64),
category_name varchar2(20)
);
4.录入数据
– Add comments to the columns
comment on column category.category_id
is ‘id’;
comment on column category.category_name
is ‘分类名称’;
– Create/Recreate primary, unique and foreign key constraints
alter table category
add constraint key_category_id primary key (category_id);
insert into category(category_id,category_name)
select ‘001’,‘书籍’ from dual
union all
select ‘002’,‘笔记本’ from dual
union all
select ‘003’,‘音响’ from dual
union all
select ‘004’,‘教学用品’ from dual;
select * from category;
5.无参存储过程
create or replace procedure pro_cag_prod
is
v_categoryName category.category_name%type;
v_categoryId productinfo.category%type;
v_prod productinfo%rowtype;
–创建游标
cursor cur_category
is
select category from productinfo group by category;
cursor cur_prod(categoryId productinfo.category%type)
is
select * from productinfo where category=categoryId;
begin
open cur_category; --打开游标
loop
fetch cur_category into v_categoryId;
if cur_category% found then
dbms_output.put_line (’-------------’);
select category_name into v_categoryName
from category where category_id=v_categoryId;
if sql%found then
dbms_output.put_line(v_categoryName);
else
dbms_output.put_line(‘不知道什么分类’);
end if;
open cur_prod(v_categoryId);
loop
fetch cur_prod into v_prod;
if cur_prod%found then
dbms_output.put_line(‘产品名称:’||v_prod.product_name||’ 产品价格:’|| v_prod.product_price);
else exit;
end if;
end loop;
close cur_prod;
else exit;
end if;
end loop;
close cur_category;–关闭游标
end;
6.结果图
begin
pro_cag_prod;
end;