每次运行此操作并添加“黄色”或“红色”或“蓝色”字样时,都会出现相同的错误。
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;
/
ERROR:
ORA-06550: line 2, column 7:
PLS-00201: identifier 'BLUE' must be declared
ORA-06550: line 2, column 4:
PL/SQL: Statement ignored
请帮忙! :)
答案 0 :(得分: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;
答案 1 :(得分:1)
&color1
是替换变量,sqlplus基本上用用户输入内容替换&color1
- 这里必须是单引号文字。只要不是,您的blue
输入就会被视为变量名,但未定义。
因此,您有两种选择 - 在sqlplus中输入引用的'blue'
,'yellow'
等,或者在代码中将&color1
替换为'&color1'
。
答案 2 :(得分: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;
/