2012-08-13 38 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. 

更新1:我加分号在语句的结束,后来我有一个错误说p_Options必须声明。我只是将p_Options更改为'& p_Options',并且它表示脚本成功运行,但是我没有看到任何输出。如:'脚本现在将结束,请重新运行'

这就是我看到的!

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

------------在不同的音符----------

如何提取信息的用户的输入/并使用INSERT语句将其添加到表中?

+0

Miguel,你能指定到底哪些工作不正常吗?另外,我看不到循环是如何开始的,因为n_numb被设置为2,并且只有当n_numb小于2时循环才会运行。当然循环永远不会启动。这也值得建议你采取婴儿步骤。为什么不只是做一个接受的声明,并评论所有其他人。看看是否有用,然后做下一个,等等。 – DavidHyogo 2012-08-13 00:35:20

+0

我试过了,现在我得到了那个错误。 – 2012-08-13 00:43:42

+0

你在这行末尾忘了';': ':g_options:='第一个提示的正确输入是“YES”或“NO”,你输入了其他的东西'' – Nagh 2012-08-13 00:46:57

回答

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以外的其他东西作为输入,循环将不起作用。我基本上没有回报,我可以继续按下进入永久。 – 2012-08-13 01:44:49

+1

为什么你需要这个循环,你试图在那里存档? – Nagh 2012-08-13 02:24:04