0

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

-- 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 语句将其添加到表中?

4

1 回答 1

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;

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

于 2012-08-13T01:20:50.650 回答