需要帮助使用IF语句过滤PL / SQL中的用户输入

时间:2012-08-13 00:28:31

标签: sql oracle plsql

我正在做一个PL / SQL脚本,它将从用户那里获得输入,然后如果必须将数据添加到1或2个表中,它将通过if语句找到。我正在使用IF语句和WHILE循环来完成此任务,但它无法正常工作。这个脚本也是main menu的一部分。任何输入都是值得赞赏的。

-- 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改为'&amp; 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语句提取用户输入的信息并将其添加到表中?

1 个答案:

答案 0 :(得分:0)

通过添加一些调试来修改脚本。 它似乎按原样运作:

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;

如果您还有其他问题,请使用更多详细信息更新您的问题。