0

我正在尝试编译存储过程:

create 
procedure checkFroud2(code IN varchar2, p_recordset OUT SYS_REFCURSOR)
AS
BEGIN
OPEN p_recordset FOR

if code='C' then 
select * from emp
//dbms_output.putline('this is if block')
else if code='F' then
dbms_output.putline('this is else block')
else
dbms_output.putline('last else')
end if;
end checkFroud2;

但这显示了编译时错误。有人可以建议问题出在哪里吗?

4

6 回答 6

4
CREATE
PROCEDURE checkFroud2(
          code IN VARCHAR2,
          p_recordset OUT SYS_REFCURSOR)
AS
BEGIN
     IF code='C' THEN
     dbms_output.put_line('this is if block');
     OPEN p_recordset FOR        
     SELECT * FROM emp;
     ELSIF code='F' THEN

     --you can open p_recordset with dummy as
     /*open p_recordset for select * from dual where 1 = 0; */
          dbms_output.put_line('this is else block');

     ELSE
     /*open p_recordset for select * from dual where 1 = 0; */
          dbms_output.put_line('last else');
     END IF;
END checkFroud2;
/

var o refcursor;
BEGIN
CHECKfroud2
('C',:o);

END;
/

PRINT O;
于 2013-11-07T10:54:14.563 回答
3
The correct code is as follows:

create procedure checkFroud2(code IN varchar2, p_recordset OUT SYS_REFCURSOR)
AS
BEGIN
OPEN p_recordset FOR

if code='C' then 
select * from emp
//dbms_output.putline('this is if block');
elsif code='F' then
dbms_output.putline('this is else block');
else
dbms_output.putline('last else');
end if;
end checkFroud2;
于 2013-11-07T10:40:51.590 回答
2

ELSE IF doesn't exist use ELSIF, also remove // before dbms_output.putline('this is if block').

于 2013-11-07T10:37:44.900 回答
1

这是 oracle 11g 中的一个示例

CREATE OR REPLACE PROCEDURE PROC_EMP_CHECK 
(
  EMPNO1 IN NUMBER 
, EMPNO2 IN NUMBER 
) 
AS 
EMPONE_NOT_EXISTS EXCEPTION;
EMPTWO_NOT_EXISTS EXCEPTION;
BOTHEMP_NOT_EXISTS EXCEPTION;
EMPCOUNT1 NUMBER;
EMPCOUNT2 NUMBER;
BEGIN

  SELECT COUNT(1) INTO EMPCOUNT1 FROM EMPLOYEES WHERE EMPLOYEE_ID=EMPNO1;
  SELECT COUNT(1) INTO EMPCOUNT2 FROM EMPLOYEES WHERE EMPLOYEE_ID=EMPNO2;
  BEGIN
  IF( EMPCOUNT1=0 AND EMPCOUNT2=0)THEN 
    RAISE BOTHEMP_NOT_EXISTS;
  ELSIF ( EMPCOUNT1=0) THEN 
    RAISE EMPONE_NOT_EXISTS;
   ELSIF ( EMPCOUNT2=0) THEN 
    RAISE BOTHEMP_NOT_EXISTS;
    dbms_output.put_line('ELSE BLOCK');
   END IF; 
  END;

  EXCEPTION
  WHEN EMPONE_NOT_EXISTS THEN 
  dbms_output.put_line('EMP One not exit');
  WHEN EMPTWO_NOT_EXISTS THEN 
  dbms_output.put_line('EMP two not exit');
  WHEN BOTHEMP_NOT_EXISTS THEN 
  dbms_output.put_line('both not exit');
  WHEN OTHERS THEN 
  dbms_output.put_line(SQLERRM);
END PROC_EMP_CHECK;
于 2015-12-09T16:01:16.567 回答
0

几个错误: 1. 打开记录集??? 为了什么??2. ELSIF and no ELSE IF 3. where is ;

CREATE PROCEDURE CHECKFROUD2 ( CODE        IN   VARCHAR2,
                         P_RECORDSET         OUT SYS_REFCURSOR )
AS
BEGIN
    OPEN P_RECORDSET FOR SELECT * FROM DUAL;

    IF CODE = 'C'
    THEN
        SELECT * FROM EMP;
    ELSIF CODE = 'F'
    THEN
        DBMS_OUTPUT.PUTLINE ( 'this is else block' );
    ELSE
        DBMS_OUTPUT.PUTLINE ( 'last else' );
    END IF;
END CHECKFROUD2;
于 2013-11-07T10:40:27.740 回答
0

Use ELSIF instead of ELSE IF. By using ELSE IF you're opening up a new nested IF-block, which you're not closing.

于 2013-11-07T10:38:06.913 回答