我需要按价格对产品名称进行分类。以下是我到目前为止所做的。
我需要以cursor c1(Price_A Number) is select Product_A, Status_A from Product
完成最后一个吗?
老实说,我根本无法得到它。请问有人可以帮我解决这个问题吗?
这是我到目前为止所写的内容:
Declare
Product_A Product.product_Name%type;
Price_A Product.Price%type;
Status_A Varchar(20);
cursor c1(Price_A Number)
is select Product_A, Status_A from Product
;
begin
'nothing
end;
答案 0 :(得分:0)
以下是如何使用游标中的显式游标和SQL大小写来完成它:
Declare
Product_A Product.product_Name%type;
Price_A Product.Price%type;
Status_A Varchar(20);
cursor c1 is
select product_name, price
, case
when price < 5 then 'low'
when price between 5 and 20 then 'medium'
else 'high' -- price >20
end status
from product;
begin
open c1;
loop
fetch c1 into Product_A, Price_A, Status_A;
exit when c1%NOTFOUND;
dbms_output.put_line(rpad(Product_A, 15)|| rpad(Price_A, 5)|| Status_A);
end loop;
end;
/
但是使用隐式游标更简单:不需要声明:
begin
for x in (
select product_name, price
, case
when price < 5 then 'low'
when price between 5 and 20 then 'medium'
else 'high' -- price >20
end status
from product) loop
dbms_output.put_line(rpad(x.Product_Name, 15)|| rpad(x.price, 5)|| x.status);
end loop;
end;
/
create table product (product_Name varchar2(15), price number);
insert into product
select 'a', 3 from dual
union all select 'b', 13 from dual
union all select 'bb', 20 from dual
union all select 'c', 130 from dual
;