我试图在oracle中执行以下查询,我收到了ORA-01422错误。以下是我用来完成结果的查询。此查询是Package的一部分。
Select DISTINCT TO_CHAR(RHR.d_end_of_month_s,'MM/dd/yyyy') "Disbursal Date",
RST.st_nam_s "State Name", MLO_COUNTY.loc_nam_s "County Name",RHR.d_pmtdu_s,
MLO_CITY.loc_nam_s "City Name", RHR.recip_s "Original GeoCode",
RHR.lse_s "Lease Number",RHR.unit_lse_s "Asset Number",
REQ.bk_acct_s "Contract Type", REQ.eqt_stat_s "Asset Status",
TO_CHAR(REQ.d_disp_s,'MM/dd/yyyy') "Asset Disposition Date",
RHR.p_st_rat_d "State Rate",(RHR.p_ct_rat_d-RHR.p_ct_trnst_rat_d) "County Rate",
RHR.p_ct_trnst_rat_d "County Transit Rate",
RHR.p_cy_rat_d -RHR.p_cy_trnst_rat_d) "City Rate",
RHR.p_cy_trnst_rat_d "City Transit rate",
RHR.p_st_rat_d +RHR.p_ct_rat_d+RHR.p_cy_rat_d) "Combined Rate",
RHR.nontax_gross_d+RHR.gross_d) "Gross Invoice Billed",
CASE WHEN trim(RHR.lse_s)='UPF STAX' THEN
mpkg_lpkreport.GROSSTAX(trim(RHR.UNIT_LSE_S),RHR.d_end_of_month_s,
trim(RHR.LSE_S),RHR.p_ct_rat_d) ELSE RHR.gross_d END "Taxable Invoice Billed",
mpkg_lpkreport.NCALACCUREDTAXVALUE(RHR.UNIT_LSE_S,0,TO_CHAR(RHR.d_end_of_month_s,'MM/dd/yyyy'),
RHR.LSE_S,TO_CHAR(RHR.d_pmtdu_s,'MM/dd/yyyy'),RHR.p_ct_rat_d) "Accrued State Tax",
mpkg_lpkreport.NCALACCUREDTAXVALUE(RHR.UNIT_LSE_S,1,TO_CHAR(RHR.d_end_of_month_s,'MM/dd/yyyy'),
RHR.LSE_S,TO_CHAR(RHR.d_pmtdu_s,'MM/dd/yyyy'),RHR.p_ct_rat_d ) "Accrued County Tax",
mpkg_lpkreport.NCALACCUREDTAXVALUE(RHR.UNIT_LSE_S,2,TO_CHAR(RHR.d_end_of_month_s,'MM/dd/yyyy'),
RHR.LSE_S,TO_CHAR(RHR.d_pmtdu_s,'MM/dd/yyyy'),RHR.p_ct_rat_d) "Accrued City Tax",
mpkg_lpkreport.NCALACCUREDDISTRICTTAX(RHR.UNIT_LSE_S,1,TO_CHAR(RHR.d_end_of_month_s,'MM/dd/yyyy'),
RHR.LSE_S,TO_CHAR(RHR.d_pmtdu_s,'MM/dd/yyyy')) "Accrued County District Tax",
mpkg_lpkreport.NCALACCUREDDISTRICTTAX(RHR.UNIT_LSE_S,2,TO_CHAR(RHR.d_end_of_month_s,'MM/dd/yyyy'),
RHR.LSE_S,TO_CHAR(RHR.d_pmtdu_s,'MM/dd/yyyy')) "Accrued City District Tax",
TO_CHAR(RLS.d_com_s,'MM/dd/yyyy') "Lease commencement date",
REQ.txp_s "Tax payment code",
replace((State_Geocodes.geo_st_l || substr(RHR.recip_s,3,length(recip_s))),'','')
"Translated GeoCode"
from RHR
Inner join REQ on REQ.unit_s =RHR.unit_lse_s
and rhr.use_tax_c = case when REQ.txp_s in ('CNTY','CNCI' ) then '1' when REQ.txp_s = 'CITY' then
'2' else '0' end
Inner join RLS on RLS.lse_s = REQ.lse_s
Inner join RST on REQ.st_s =RST.st_s
Inner join MLO_COUNTY on MLO_COUNTY.ct_s =REQ.ct_s and MLO_COUNTY.st_s =REQ.st_s
Inner join MLO_CITY on MLO_CITY.cy_s =REQ.cy_s AND MLO_CITY.ct_s =REQ.ct_s
AND MLO_CITY.st_s =REQ.st_s
Inner join State_Geocodes on State_Geocodes.ST_S=substr(RHR.recip_s,1,2)
Where 1=1 and rhr.por_s =' 1' and rhr.d_end_of_month_s = '30-NOV-2017';
查询中是否有任何遗漏?
以下是查询mpkg_lpkreport.GROSSTAX中使用的函数。它显示了此函数中的错误,我不确定这是什么问题。
FUNCTION GROSSTAX(sunit_lse_s varchar2,
sd_end_of_month_s date, slse_s varchar2, rat_d FLOAT) return FLOAT
AS
n_grossd FLOAT;
BEGIN
BEGIN
select nvl(gross_d,0) into n_grossd from RHR
where trim(RHR.lse_s)=slse_s and trim(RHR.unit_lse_s)=sunit_lse_s
and RHR.use_tax_c IN ('0') and RHR.d_end_of_month_s=sd_end_of_month_s
and RHR.P_CT_RAT_D=rat_d;
EXCEPTION
WHEN NO_DATA_FOUND THEN
n_grossd := 0;
END;
IF n_grossd = 0 THEN
BEGIN
select nvl(gross_d,0) into n_grossd from RHR
where trim(RHR.lse_s)=slse_s and trim(RHR.unit_lse_s)=sunit_lse_s
and RHR.use_tax_c IN ('1') and RHR.d_end_of_month_s=sd_end_of_month_s
and RHR.P_CT_RAT_D=rat_d;
EXCEPTION
WHEN NO_DATA_FOUND THEN
n_grossd := 0;
END;
END IF;
IF n_grossd = 0 THEN
BEGIN
select nvl(gross_d,0) into n_grossd from RHR
where trim(RHR.lse_s)=slse_s and trim(RHR.unit_lse_s)=sunit_lse_s
and RHR.use_tax_c IN ('2') and RHR.d_end_of_month_s=sd_end_of_month_s
and RHR.P_CT_RAT_D=rat_d ;
EXCEPTION
WHEN NO_DATA_FOUND THEN
n_grossd := 0;
END;
END IF;
return n_grossd;
END GROSSTAX;
答案 0 :(得分:0)
请尝试调试您逐行查询该功能的所有调用的注释。 例如,您的函数中的这段代码可以返回> 1 recs 选择nvl(gross_d,0) 进入n_grossd 来自RHR 修剪(RHR.lse_s)= slse_s 和trim(RHR.unit_lse_s)= sunit_lse_s 和RHR.use_tax_c IN('0') 和RHR.d_end_of_month_s = sd_end_of_month_s 和RHR.P_CT_RAT_D = rat_d;
答案 1 :(得分:0)
问题是,GROSSTAX
函数中的一个查询返回了多行,但由于您正在执行SELECT...INTO
这些查询,因此最多必须返回一行。解决此问题的一种方法是重写GROSSTAX
:
FUNCTION GROSSTAX(sunit_lse_s IN varchar2,
sd_end_of_month_s IN date,
slse_s IN varchar2,
rat_d IN FLOAT)
return FLOAT AS
BEGIN
FOR aRow IN (select GROSS_D
from RHR
where trim(RHR.lse_s) = slse_s and
trim(RHR.unit_lse_s)=sunit_lse_s and
RHR.use_tax_c IN ('0', '1', '2') and
RHR.d_end_of_month_s = sd_end_of_month_s and
RHR.P_CT_RAT_D=rat_d
ORDER BY RHR.USE_TAX_C ASC)
LOOP
RETURN NVL(aRow.GROSS_D, 0);
END LOOP;
RETURN 0;
END GROSSTAX;
在这里,我将三个查询压缩为一个,因为看起来它们只是与他们检查的RHR.USE_TAX_C
的值不同。查询的结果按RHR.USE_TAX_C的升序排序,以便它将返回与存在的USE_TAX_C的最小值相关联的GROSS_D值,所有其他比较都相同。仅使用查询返回的第一行;一旦光标返回第一行,函数就会返回。
祝你好运。