在sql存储过程中的if else条件

时间:2013-11-07 10:35:16

标签: sql oracle

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

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;

但是这显示了编译时错误。任何人都可以提出问题所在吗?

6 个答案:

答案 0 :(得分: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;

答案 1 :(得分:2)

使用ELSE IF

ELSIF不存在,//之前删除dbms_output.putline('this is if block')

答案 2 :(得分:2)

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;

答案 3 :(得分: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;

答案 4 :(得分:0)

使用ELSIF代替ELSE IF。通过使用ELSE IF,您将打开一个新的嵌套IF - 块,而您尚未关闭。

答案 5 :(得分:0)

一些错误:  1.打开记录集???为了什么??  2. ELSIF,没有ELSE IF 3.在哪里;

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;