2017-03-22 2 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이고 최대 값은 TEAM_ID입니까? 아니면 그냥 숫자입니까?

+1

당신이 숫자의 어떤 종류를 받아 들일 것, 그리고 형식 :

는 예를 들어, 당신이 필요 같은 것을 할 수있는 방법이 될 수 있을까? 예를 들어 천 단위 구분 기호가 유효한 숫자입니까? 그리고 부정적인? ... 좀 더 잘 할 수 있도록 수표를 정의 해주세요. – Aleksej

+1

@Aleksej i는 게시물을 편집합니다. 좋은? – 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