2012-08-27 43 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