Oracle游标未按预期工作

时间:2010-12-03 07:52:37

标签: oracle stored-procedures plsql oracle10g cursor

这是程序,

CREATE OR REPLACE PROCEDURE provsnXmlCmprsn (
            encyNo SAS_PRO_CTL.AGENCYNO%TYPE, period SAS_PRO_CTL.PERIODE%TYPE) IS

xmlContent SAS_PRO_XML.XMLCONTENT%TYPE;
sasProvisionId SAS_PRO_CTL.SASPROVISIONID%TYPE;

CURSOR crsrXml IS
SELECT XMLCONTENT, c.SASPROVISIONID FROM SAS_PRO_XML x, SAS_PRO_CTL c
  WHERE x.SASPROVISIONID = c.SASPROVISIONID AND c.PERIODE = period 
                                    AND c.AGENCYNO = agencyNo ORDER BY XMLLINENO;

BEGIN
DBMS_OUTPUT.put_line('Params: ' || agencyNo || ', ' || period);

OPEN crsrXml;
LOOP
  FETCH crsrXml INTO xmlContent, sasProvisionId;
  EXIT WHEN crsrXml%NOTFOUND;
    DBMS_OUTPUT.put_line('XML Content Length: ' || LENGTH(xmlContent));
END LOOP;
CLOSE crsrXml;

END provsnXmlCmprsn;

根据条件和参数值,cursor中的查询检索5行,而预期1行。当独立运行时,相同的查询会产生1行。令人惊讶的是,无论条件cursor是否通过,c.PERIODE = period AND c.AGENCYNO = agencyNo中的查询总是返回5行。这显然意味着这个查询,

SELECT XMLCONTENT, c.SASPROVISIONID FROM SAS_PRO_XML x, SAS_PRO_CTL c
  WHERE x.SASPROVISIONID = c.SASPROVISIONID AND c.PERIODE = period 
                                    AND c.AGENCYNO = agencyNo ORDER BY XMLLINENO;

和此查询,

SELECT XMLCONTENT, c.SASPROVISIONID FROM SAS_PRO_XML x, SAS_PRO_CTL c
  WHERE x.SASPROVISIONID = c.SASPROVISIONID ORDER BY XMLLINENO;

cursor内的行为相同。这个AND c.PERIODE = period AND c.AGENCYNO = agencyNo部分根本没有被考虑过。 知道出了什么问题吗?

1 个答案:

答案 0 :(得分:5)

您的一个参数与列名称相同:AGENCYNO。由于范围界定的工作方式,评估结果为1=1。这就是为什么提供参数唯一名称的好习惯,例如通过在p_前加上它们。

你应该找到

AND c.PERIODE = p_period AND c.AGENCYNO = p_agencyNo

返回所需的一行。严格来说,您无需将period的名称更改为p_period,因为它已与periode区分开来。但是,一致性是软件工程的一种优点。