0
CREATE OR REPLACE PROCEDURE ABC ( REG_NO   IN  CO_ENROLMENT.S_REGNO%TYPE,
                                  TERM     IN  COURSEOFFERING.CO_TERMNUMBER%TYPE,
                                  YEAR     IN  COURSEOFFERING.CO_YEAR%TYPE,
                                  CO_TITLE IN  COURSE.C_TITLE%TYPE,
                                  EN_DATE  OUT CO_ENROLMENT.COE_ENROLDATE%TYPE,
                                  COM_ST   OUT CO_ENROLMENT.COE_COMPLETIONSTATUS%TYPE)

AS 
BEGIN
   SELECT M.COE_COMPLETIONSTATUS, M.COE_ENROLDATE 
     INTO COM_ST, EN_DATE
     FROM COURSEOFFERING O
    INNER JOIN COURSE C
            ON C.C_ID = O.C_ID
    INNER JOIN CO_ENROLMENT M
            ON M.CO_ID = O.CO_ID
    WHERE M.S_REGNO LIKE REG_NO AND
          O.CO_TERMNUMBER LIKE TERM AND
          O.CO_YEAR LIKE YEAR AND
          C.C_TITLE LIKE CO_TITLE;
END ABC;

我已经写下了上面的程序。下面是调用上述程序的 PL/SQL 块,

DECLARE
  COMPL_STATUS   CO_ENROLMENT.COE_COMPLETIONSTATUS%TYPE;
  ENROL_DATE     CO_ENROLMENT.COE_ENROLDATE%TYPE;
BEGIN
  ABC (44444444, 2009, 2, 'PLSQL Programming', 
       EN_DATE => enrol_date, COM_ST =>compl_status);
  DBMS_OUTPUT.PUT_LINE ('STUDENT COMPLETION STATUS AND ENROLMENT DATE IS ' 
                         || ENROL_DATE
                         || '    ' 
                         || compl_status );
END;

由于找不到数据,它返回错误,但是当我单独运行查询时,我得到了输出。我无法弄清楚出了什么问题。我是否正确编写了程序块并且在 PL/SQL 块中传递的参数是否正确?

4

4 回答 4

1

在过程中,您有一年排名第三,但您称它为第二名。以及如何声明类型 (COURSEOFFERING.CO_TERMNUMBER%TYPE, COURSEOFFERING.CO_TERMNUMBER%TYPE)?

于 2012-10-31T06:56:51.120 回答
1

select您的过程中的语句ABC没有返回任何行时,NO_DATA_FOUND将引发异常并且您的存储过程的执行将被暂停。为避免此类行为,您需要EXCEPTION在存储过程中添加部分以捕获异常并做出适当反应。为此,存储过程的执行部分可能如下所示:

BEGIN
   SELECT M.COE_COMPLETIONSTATUS, M.COE_ENROLDATE 
     INTO COM_ST, EN_DATE
     FROM COURSEOFFERING O
    INNER JOIN COURSE C
            ON C.C_ID = O.C_ID
    INNER JOIN CO_ENROLMENT M
            ON M.CO_ID = O.CO_ID
    WHERE M.S_REGNO LIKE REG_NO AND
          O.CO_TERMNUMBER LIKE TERM AND
          O.CO_YEAR LIKE YEAR AND
          C.C_TITLE LIKE CO_TITLE;
EXCEPTION
  WHEN NO_DATA_FOUND
  THEN DBMS_OUTPUT.PUT_LINE('No data found') -- for example
END ABC

或者,如果您想继续,即使select语句引发异常,您也可以select用嵌套BEGIN .. END块将您的语句括起来。

BEGIN
   -- some code before
   BEGIN    
     SELECT M.COE_COMPLETIONSTATUS, M.COE_ENROLDATE 
       INTO COM_ST, EN_DATE
       FROM COURSEOFFERING O
      INNER JOIN COURSE C
              ON C.C_ID = O.C_ID
      INNER JOIN CO_ENROLMENT M
              ON M.CO_ID = O.CO_ID
      WHERE M.S_REGNO LIKE REG_NO AND
            O.CO_TERMNUMBER LIKE TERM AND
            O.CO_YEAR LIKE YEAR AND
            C.C_TITLE LIKE CO_TITLE;
   EXCEPTION
     WHEN NO_DATA_FOUND
     THEN DBMS_OUTPUT.PUT_LINE('No data found') -- for example
  END;
  -- some code after
END ABC
于 2012-10-31T06:59:39.280 回答
1

您的过程查询中可能缺少通配符。

16:02:06 SYSTEM@dwh-prod> select * from dual where 'abc' like 'ab'; 

no rows selected                                                    


16:02:18 SYSTEM@dwh-prod> select * from dual where 'abc' like 'ab%';

D                                                                   
-                                                                   
X                                                                   

没有它们,C.C_TITLE LIKE CO_TITLE等于C.C_TITLE = CO_TITLE,这不一定是您想要的。

试试C.C_TITLE LIKE '%'||CO_TITLE||'%'等等。

于 2012-10-31T08:03:52.713 回答
0

您错误地调用了存储过程。您必须以正确的顺序指定参数,或使用命名参数表示法,即

ABC (44444444, 2, 2009, 'PLSQL Programming', enrol_date, compl_status);

或者

ABC (REG_NO => 44444444, YEAR => 2009, TERM => 2,
   CO_TITLE => 'PLSQL Programming', 
   EN_DATE => enrol_date, COM_ST =>compl_status);

您可以看到,如果您使用命名参数表示法,您可以以不同的顺序指定参数(如果参数具有 DEFAULT 值,则根本不指定)。

于 2012-10-31T08:09:11.470 回答