1

每次我运行它并添加“黄色”或“红色”或“蓝色”作为输入时,都会出现相同的错误。

set serveroutput on
undefine color1
undefine color2
accept color1 prompt 'Type the 1st primary color: '
accept color2 prompt 'Type the 2nd primary color: '
begin
   if &color1 = &color2 then
      dbms_output.put_line(&color1||' plus '||&color2||' then '||&color1);
   elsif (&color1 = 'red' and &color2 = 'blue') or (&color2 = 'red' and &color1 = 'blue') then
      dbms_output.put_line(&color1||' plus '||&color2||' is purple');
   elsif (&color1 = 'red' and &color2 = 'yellow') or (&color2 = 'red' and &color1 = 'yellow') then
      dbms_output.put_line(&color1||' plus '||&color2||' is orange');
   else
      dbms_output.put_line(&color1||' plus '||&color2||' is green');
   end if;
end;
/   

错误:

ORA-06550: line 2, column 7:
PLS-00201: identifier 'BLUE' must be declared
ORA-06550: line 2, column 4:
PL/SQL: Statement ignored

请帮忙!:)

4

3 回答 3

1

像这样试试

set serveroutput on
undefine color1
undefine color2
accept color1 prompt 'Type the 1st primary color: '
accept color2 prompt 'Type the 2nd primary color: '
BEGIN
   IF '&color1' = '&color2' THEN
      dbms_output.put_line('&color1'||' plus '||'&color2'||' then '||'&color1');
   elsif ('&color1' LIKE 'red' AND '&color2' LIKE 'blue') OR ('&color2' LIKE 'red' AND '&color1' LIKE 'blue') THEN
      dbms_output.put_line('&color1'||' plus '||'&color2'||' is purple');
   elsif ('&color1' LIKE 'red' AND '&color2' LIKE 'yellow') OR ('&color2' LIKE 'red' AND '&color1' LIKE 'yellow') THEN
      dbms_output.put_line('&color1'||' plus '||'&color2'||' is orange');
   ELSE
      dbms_output.put_line('&color1'||' plus '||'&color2'||' is green');
   end if;
END;
/   

或者最好将 color1 & color2 存储在这样的局部变量中,

SET serveroutput ON
undefine color1
undefine color2
accept color1 prompt 'Type the 1st primary color: '
accept color2 prompt 'Type the 2nd primary color: '
DECLARE 
   color_1 VARCHAR2(10) := '&color1';
   color_2 VARCHAR2(10) := '&color2';
BEGIN
   IF color_1 = color_2 THEN
      dbms_output.put_line(color_1||' plus '||color_2||' then '||color_1);
   elsif (color_1 LIKE 'red' AND color_2 LIKE 'blue') OR (color_2 LIKE 'red' AND color_1 LIKE 'blue') THEN
      dbms_output.put_line(color_1||' plus '||color_2||' is purple');
   elsif (color_1 LIKE 'red' AND color_2 LIKE 'yellow') OR (color_2 LIKE 'red' AND color_1 LIKE 'yellow') THEN
      dbms_output.put_line(color_1||' plus '||color_2||' is orange');
   ELSE
      dbms_output.put_line(color_1||' plus '||color_2||' is green');
   END IF;
end;
于 2013-09-10T03:27:34.087 回答
1

&color1是一个替换变量,sqlplus 基本上替换&color1为用户输入内容——这里必须是单引号文字。只要不是,您的blue输入就会被视为未定义的变量名。

因此,您有两个选择 -在 sqlplus 中输入quoted 等,或者在您的代码中'blue'替换为。'yellow'&color1'&color1'

于 2013-09-10T03:39:15.610 回答
0

尝试这个

 set serveroutput on
    undefine color1
    undefine color2
    accept color1 prompt 'Type the 1st primary color: '
    accept color2 prompt 'Type the 2nd primary color: '
    begin
       if '&color1' = '&color2' then
          dbms_output.put_line('&color1'||' plus '||'&color2'||' then '||'&color1');
       elsif ('&color1' like 'red' and '&color2' like 'blue') or ('&color2' like 'red' and '&color1' like 'blue') then
          dbms_output.put_line('&color1'||' plus '||'&color2'||' is purple');
       elsif ('&color1' like 'red' and '&color2' like 'yellow') or ('&color2' like 'red' and '&color1' like 'yellow') then
          dbms_output.put_line('&color1'||' plus '||'&color2'||' is orange');
       else
          dbms_output.put_line('&color1'||' plus '||'&color2'||' is green');
       end if;
    end;
    /  
于 2013-09-10T03:45:28.420 回答