Oracle 输出与PL/SQL中的预期输出不匹配
我的问题是:创建一个存储过程,它将不同的过滤器作为输入,比如价格范围, 类别、产品评级、卖家评级、缺货,并显示带有 应用过滤器后的所有详细信息 我已经用pl/SQLOracle编写了以下代码Oracle 输出与PL/SQL中的预期输出不匹配,oracle,plsql,Oracle,Plsql,我的问题是:创建一个存储过程,它将不同的过滤器作为输入,比如价格范围, 类别、产品评级、卖家评级、缺货,并显示带有 应用过滤器后的所有详细信息 我已经用pl/SQLOracle编写了以下代码 create or replace procedure filter_query(l in number,r in number,catg in varchar,prat in float,srat in float,oos in number) as begin if(oos=1) then
create or replace procedure filter_query(l in number,r in number,catg in varchar,prat in float,srat in float,oos in number)
as
begin
if(oos=1)
then
declare
pname product.p_name%type;
cursor ptr is
select p_name from product where ((amount between l and r) and (ct_id in (select ct_id from category where categ=catg)) and (rating=prat) and (sl_id in (select sl_id from seller where rating=srat)) and (qt_rem=0)) ;
begin
open ptr;
loop
fetch ptr into pname;
exit when ptr%notfound;
dbms_output.put_line(pname);
end loop;
close ptr;
end;
if(oos=0)
then
declare
pname product.p_name%type;
cursor ptr is
select p_name from product where ((amount between l and r) and (ct_id in (select ct_id from category where categ=catg)) and (rating=prat) and (sl_id in (select sl_id from seller where rating=srat)) and (qt_rem>0)) ;
begin
open ptr;
loop
fetch ptr into pname;
exit when ptr%notfound;
dbms_output.put_line(pname);
end loop;
close ptr;
end;
end if;
end if;
end;
/
我的主要剧本是:
declare
sname varchar(20);
begin
filter_query(500,1000,'Home Decor',3.6,3.3,0);
end;
/
产品表
卖方表
类别表
输出应为“白灯”,因为其数量在500到1000之间,额定值=3.6,卖方额定值“5S”也为3.3,类别为“室内装饰”。但它什么也没表现出来为什么?请帮忙。我无法找出错误第一个问题是开头的
if(oos=1)
包含了整个过程,因此如果oos
不是1
,则处理结束。如果您格式化了代码(这是我们在编程中这样做的原因之一),或者通过调试器运行代码,这会更清楚。一些不必要的开始
/结束
块和重复也会使结构变得复杂,这使得查看结构(或将来维护结构)变得更加困难。固定版本为:
create table CATEGORY (
CT_ID char(2)
,CATEG varchar2(20)
)
/
insert into CATEGORY values ('2C','Footwear')
/
insert into CATEGORY values ('1C','Books')
/
insert into CATEGORY values ('3C','Home Decor')
/
insert into CATEGORY values ('4C','Accessories')
/
create table SELLER (
SL_ID char(2)
,SL_NAME char(5)
,RATING number(2, 1)
)
/
insert into SELLER values ('2S','Priya', 2.5)
/
insert into SELLER values ('4S','Vicky', 4.7)
/
insert into SELLER values ('5S','Sneha', 3.3)
/
insert into SELLER values ('1S','Abhay', 3.5)
/
create table PRODUCT (
P_ID char(2)
,P_NAME varchar2(35)
,AMOUNT number(4)
,QT_REM number(1)
,CT_ID char(2)
,SL_ID char(2)
,RATING number(2, 1)
)
/
insert into PRODUCT
values ('1P'
,'The programming language of oracle'
,350
,4
,'1C'
,'1S'
,3.5)
/
insert into PRODUCT
values ('2P'
,'Nike White shoes'
,7000
,2
,'2C'
,'3S'
,3.25)
/
insert into PRODUCT
values ('3P'
,'White Lamp'
,800
,3
,'3C'
,'5S'
,3.6)
/
create or replace procedure FILTER_QUERY(l in number
,r in number
,catg in varchar
,prat in float
,srat in float
,oos in number)
as
pname product.p_name%type;
cursor ptr is
select p_name
from product
where amount between l and r
and ct_id in (select ct_id from category where categ=catg)
and rating=prat
and sl_id in (select sl_id from seller where rating=srat)
and qt_rem=0;
cursor ptr2 is
select p_name
from product
where amount between l and r
and ct_id in (select ct_id from category where categ=catg)
and rating=prat
and sl_id in (select sl_id from seller where rating=srat)
and qt_rem>0;
begin
if oos=1 then
open ptr;
loop
fetch ptr into pname;
exit when ptr%notfound;
dbms_output.put_line(pname);
end loop;
close ptr;
elsif oos=0 then
open ptr2;
loop
fetch ptr2 into pname;
exit when ptr2%notfound;
dbms_output.put_line(pname);
end loop;
close ptr2;
end if;
end;
/
set serveroutput on
declare
sname varchar2(35);
begin
filter_query(500,1000,'Home Decor',3.6,3.3,0);
end;
/
White Lamp
PL/SQL procedure successfully completed.
create or replace procedure filter_query
( l in number
, r in number
, catg in varchar2
, prat in float
, srat in float
, oos in number )
as
pname product.p_name%type;
begin
if oos = 1 then
declare
cursor ptr is
select p_name
from product
where amount between l and r
and ct_id in (select ct_id from category where categ = catg)
and rating = prat
and sl_id in (select sl_id from seller where rating = srat)
and qt_rem = 0;
begin
open ptr;
loop
fetch ptr into pname;
exit when ptr%notfound;
dbms_output.put_line(pname);
end loop;
close ptr;
end;
elsif oos = 0 then
declare
cursor ptr is
select p_name
from product
where amount between l and r
and ct_id in (select ct_id from category where categ = catg)
and rating = prat
and sl_id in (select sl_id from seller where rating = srat)
and qt_rem > 0;
begin
open ptr;
loop
fetch ptr into pname;
exit when ptr%notfound;
dbms_output.put_line(pname);
end loop;
close ptr;
end;
end if;
end filter_query;
但是,除了最终条件外,这两个块是相同的,因此您可以通过让一个块拾取两个光标中的一个来简化它:
create or replace procedure filter_query
( l in number
, r in number
, catg in varchar2
, prat in float
, srat in float
, oos in number )
as
pname product.p_name%type;
ptr sys_refcursor;
begin
if oos = 1 then
open ptr for
select p_name
from product
where amount between l and r
and ct_id in (select ct_id from category where categ = catg)
and rating = prat
and sl_id in (select sl_id from seller where rating = srat)
and qt_rem = 0;
elsif oos = 0 then
open ptr for
select p_name
from product
where amount between l and r
and ct_id in (select ct_id from category where categ = catg)
and rating = prat
and sl_id in (select sl_id from seller where rating = srat)
and qt_rem > 0;
end if;
loop
fetch ptr into pname;
exit when ptr%notfound;
dbms_output.put_line(pname);
end loop;
close ptr;
end filter_query;
或者,使用一个游标并在SQL中应用筛选逻辑:
create or replace procedure filter_query
( l in number
, r in number
, catg in varchar2
, prat in float
, srat in float
, oos in number )
as
begin
for prod in (
select p_name
from product
where amount between l and r
and ct_id in (select ct_id from category where categ = catg)
and rating = prat
and sl_id in (select sl_id from seller where rating = srat)
and ( ( oos = 1 and qt_rem = 0 )
or ( oos = 0 and qt_rem > 0 ) )
)
loop
dbms_output.put_line(prod.p_name);
end loop;
end filter_query;
不要发布表格的图像,将DDL和DML作为文本发布。请回复…@SITANSHUYADAV-但你明白为什么答案有效而你的答案无效吗;你从中学到了什么?