2017-03-22 81 views
1
DECLARE 
    TEAM_ID NUMBER := &INPUT; 
    CURSOR C_WORKER IS 
    SELECT FIRST_NAME, LAST_NAME 
    FROM EMPLOYEES 
    WHERE DEPARTMENT_ID = TEAM_ID; 
    V_LAST_NAME EMPLOYEES.LAST_NAME%TYPE; 
    V_FIRST_NAME EMPLOYEES.FIRST_NAME%TYPE; 
BEGIN 
    OPEN C_WORKER; 
    LOOP 
     FETCH C_WORKER INTO V_LAST_NAME, V_FIRST_NAME; 
     EXIT WHEN C_WORKER%NOTFOUND; 
     DBMS_OUTPUT.PUT_LINE(V_LAST_NAME || ' ' || V_FIRST_NAME); 
    END LOOP; 
    CLOSE C_WORKER; 
END; 

我如何更改此代碼來檢查TEAM_ID(&輸入)是一個數字? 如果是 - 打開光標,如果沒有,打印「請寫一個數字」。PLSQL - 如何檢查和輸入是一個數字

最小值是1,max是最大數量TEAM_ID?或者它只是一個數字?

+1

你會接受什麼樣的數字,並以哪種方式?例如,具有千位分隔符的數字是否有效?和負面? ...請嘗試更好地定義您需要獲得一些幫助的檢查 – Aleksej

+1

@Aleksej我編輯帖子。好? – Starvill

+0

「1,000」是否應該算作數字還不清楚。 –

回答

1

要處理替換變量,您需要將它們包裝在引號中,並將它們視爲字符串。

例如,這可能是一個辦法做到像你需要:

declare 
    -- define a varchar2 variable to host your variable; notice the quotes 
    vStringInput  varchar2(10) := '&input'; 
    vNumInput   number; 
    vVal    number; 
    -- define a parametric cursor, to avoid references to variables 
    cursor cur(num number) is select num from dual; 
begin 
    -- try to convert the string to a number 
    begin 
     vNumInput := to_number(vStringInput); 
    exception 
     when others then 
      vNumInput := null; 
    end; 
    -- 
    -- check the values, to understand if it is a number (vNumInput NULL or NOT NULL) 
    -- and, in case it's a number, if it suits your criteria 
    case 
     when vNumInput is null then 
      dbms_output.put_line('not a number'); 
     when vNumInput < 1 then 
      dbms_output.put_line('less than 1'); 
     -- whatever check you need on the numeric value 
     else 
      -- if the value is ok, open the cursor 
      open cur(vNumInput); 
      loop 
       fetch cur into vVal;     
       exit when cur%NOTFOUND; 
       dbms_output.put_line('value from cursor: ' || vVal); 
      end loop;    
    end case;  
end; 
/
+0

它的作品,非常感謝你! – Starvill

相關問題