2012-08-13 5 views
0

사용자로부터 입력을받을 PL/SQL 스크립트를 수행하고 있고, if 문을 통해 데이터를 1에 추가해야 하는지를 알아냅니다 또는 2 개의 테이블. 이 작업을 수행하기 위해 IF 문과 WHILE 루프를 사용하고 있지만 제대로 작동하지 않습니다. 또한이 스크립트는 main menu의 일부입니다. 모든 입력은 그것을 인정한다.IF 문을 사용하여 PL/SQL에서 사용자 입력을 필터링하는 데 도움이 필요합니다.

-- q3.sql 
-- Question name goes here!! 

VARIABLE g_options VARCHAR2(1000); 

PROMPT 'Vehicle Inventory Record' 
ACCEPT p_Options PROMPT 'Does this car has any Optional Equipment or Accessories?(YES or NO)'; 
-- Option table 
ACCEPT p_OCode PROMPT 'Enter the code for the option picked (Ex. CD2):' 
ACCEPT p_ODescription PROMPT 'Enter the description for the option picked:' 
ACCEPT p_OPrice PROMPT 'Enter the price for the option picked:' 
-- Car table 
ACCEPT p_SerialNo PROMPT 'What is the serial number of the car?' 
ACCEPT p_Make PROMPT 'What is the make of the car?' 
ACCEPT p_Model PROMPT 'What is the model of the car?' 
ACCEPT p_Year PROMPT 'What is the color of the car?' 
ACCEPT p_Trim PROMPT 'What is the trim of the car?' 
ACCEPT p_PurchFrom PROMPT 'Where was the car purchased from?' 
ACCEPT p_PurchInvNo PROMPT 'What was the invoice number of the purcahse?' 
ACCEPT p_PurchDate PROMPT 'When was the car purcahsed?(ex. 13-FEB-06)' 
ACCEPT p_PurchCost PROMPT 'How much did he car cost?' 
ACCEPT p_ListPrice PROMPT 'What was the list price of the car?' 


DECLARE 
    n_numb number := 1; 

BEGIN 
    --WHILE n_numb < 2 LOOP 

    IF '&p_Options' = 'YES' THEN 
    :g_options := 'Input will be added into the option table AND car table'; 
    -- Insert statement goes here 
    n_numb := 2; 

    ELSIF '&p_Options' = 'NO' THEN 
    :g_options := 'Input will only be added to the car table'; 
    -- Insert statement goes here 
    n_numb := 2; 

    ELSE 
    :g_options := ' The correct input for the first prompt was "YES" or "NO", you entered something else' 
    --:g_options := :g_options || ' The script will now end, please run it again' 
    n_numb := 0; 

    END IF; 
    --END LOOP; 
END; 
/

나는 루프를 주석하고 n_numb 변수, 그리고이 오류 얻을 :

END IF; 
    * 
ERROR at line 22: 
ORA-06550: line 22, column 7: 
PLS-00103: Encountered the symbol "END" when expecting one of the following: 
. (* @ % & = - + ; </> at in is mod remainder not rem 
<an exponent (**)> <> or != or ~= >= <= <> and or like 
between || multiset member SUBMULTISET_ 
The symbol ";" was substituted for "END" to continue. 

UPDATE 1 : 나는 문장의 끝에 세미콜론을 추가,하지만 나는 p_Options을 선언해야한다는 오류가 발생했습니다. 방금 p_Options를 '& p_Options'으로 변경했으며 스크립트가 성공적으로 실행되었지만 출력이 표시되지 않는다고 말합니다. Like : '스크립트가 종료됩니다. 다시 실행하십시오.'.

이것이 전부입니다.

old 7:  IF '&p_Options' = 'YES' THEN 
new 7:  IF '' = 'YES' THEN 
old 12:  ELSIF '&p_Options' = 'NO' THEN 
new 12:  ELSIF '' = 'NO' THEN 

PL/SQL procedure successfully completed. 

업데이트 2 : 나는 일할 수있는 if 문을 얻었다. 그러나 입력으로 YES 또는 NO 이외의 다른 것을 입력하면 루프가 작동하지 않습니다. 나는 기본적으로 돌아 오지 않는다. 나는 계속해서 영원히 들어가야한다. : 사용자가/입력 s의 것을 나는 정보를 추출하려면 어떻게

다른 주 ----------에 S

------------ INSERT 문을 사용하여 테이블에 추가 하시겠습니까?

+0

Miguel, 정확하게 작동하지 않는 것을 정확히 지정할 수 있습니까? 또한, n_numb이 2로 설정 되었기 때문에 루프가 시작되는 방법을 볼 수 없으며 n_numb이 2보다 작 으면 루프 만 실행됩니다. 확실히 루프가 시작되지 않습니다. 아기가 발을 딛는 것도 좋습니다. 한 명만 받아 들여 진술을 받아들이고 다른 모든 의견을 주석으로 달아 보는 것이 어떻겠습니까? 작동하는지 확인한 다음 다음 작업을 수행하십시오. – DavidHyogo

+0

나는 그걸 시험해 보았는데, 이제 그 오류가 나옵니다. –

+0

이 줄의 끝에서';'을 잊었습니다 : ': g_options : = '첫 번째 프롬프트의 올바른 입력 내용은 "예"또는 "아니오"입니다. – Nagh

답변

0

Alittle은 약간의 디버깅을 추가하여 스크립트를 수정했습니다. 이 그대로 작동하는 것 같군 :

SET VERIFY OFF 
SET FEED OFF 
SET SERVEROUTPUT ON 
SET SERVEROUTPUT ON 
VARIABLE g_options VARCHAR2(1000); 
PROMPT 'Vehicle Inventory Record' 
ACCEPT p_Options PROMPT 'Does this car has any Optional Equipment or Accessories?(YES or NO): '; 
/* 
-- Option table 
ACCEPT p_OCode PROMPT 'Enter the code for the option picked (Ex. CD2):' 
ACCEPT p_ODescription PROMPT 'Enter the description for the option picked:' 
ACCEPT p_OPrice PROMPT 'Enter the price for the option picked:' 
-- Car table 
ACCEPT p_SerialNo PROMPT 'What is the serial number of the car?' 
ACCEPT p_Make PROMPT 'What is the make of the car?' 
ACCEPT p_Model PROMPT 'What is the model of the car?' 
ACCEPT p_Year PROMPT 'What is the color of the car?' 
ACCEPT p_Trim PROMPT 'What is the trim of the car?' 
ACCEPT p_PurchFrom PROMPT 'Where was the car purchased from?' 
ACCEPT p_PurchInvNo PROMPT 'What was the invoice number of the purcahse?' 
ACCEPT p_PurchDate PROMPT 'When was the car purcahsed?(ex. 13-FEB-06)' 
ACCEPT p_PurchCost PROMPT 'How much did he car cost?' 
ACCEPT p_ListPrice PROMPT 'What was the list price of the car?' 
*/ 

DECLARE 
    n_numb number := 1; 

BEGIN 
    --WHILE n_numb < 2 LOOP 
    IF '&p_Options' = 'YES' THEN 
    :g_options := 'Input will be added into the option table AND car table'; 
    -- Insert statement goes here 
    dbms_output.put_line('insert with YES'); 
    n_numb := 2; 

    ELSIF '&p_Options' = 'NO' THEN 
    :g_options := 'Input will only be added to the car table'; 
    -- Insert statement goes here 
    dbms_output.put_line('insert with NO'); 
    n_numb := 2; 

    ELSE 
    :g_options := ' The correct input for the first prompt was "YES" or "NO", you entered something else'; 
    --:g_options := :g_options || ' The script will now end, please run it again' 
    dbms_output.put_line('ELSE'); 
    n_numb := 0; 

    END IF; 
    --END LOOP; 
END; 

이 더 문제가있는 경우, 자세한 내용과 질문을 업데이트합니다.

+0

귀하의 코드를 사용하고 if 문이 작동합니다. 하지만 입력으로 YES 또는 NO 이외의 다른 것을 입력하면 루프가 작동하지 않습니다. 나는 기본적으로 돌아 오지 않는다. 나는 계속해서 영원히 들어갈 수있다. –

+1

왜이 루프가 필요한가요, 거기에 무엇을하려고합니까? – Nagh

관련 문제