2012-08-27 47 views
2

我想知道是否有任何方式在PL/SQL过程的声明内有一个if语句。例如:PL/SQL如果里面声明

procedure testing (no IN NUMBER, name IN VARCHAR2) IS 
    if no = 0 then 
     cursor c is 
      select * from table where name = name; 
    else 
     cursor c is 
      select * from table where name = name; 
    end if; 
BEGIN 
    --work with cursor c 
END testing; 

这或多或少是它的意图。

+0

不......你可以嵌套PL/SQL块,所以在开始时有一个声明。 – Ben

回答

1

不,你只能在声明部分变量声明和初始化。

您可以使用游标变量(REF CURSOR型)并打开可执行块光标:

procedure testing (no IN NUMBER, name IN VARCHAR2) IS 
    TYPE YOUR_CURSOR_TYPE IS REF CURSOR; 
    c YOUR_CURSOR_TYPE; 
BEGIN 
    --work with cursor c 
    if no = 0 then 
     OPEN c FOR 
      select * from table where name = name; 
    else 
     open c FOR 
      select * from table where name = name; 
    end if; 

    ... do something with c 
END testing; 

根据您select子句中的列,你必须决定是否使用强类型或弱类型游标变量。

Cursor Variables