每次我运行它并添加单词"黄色"或"红色"或"蓝色"时,都会出现相同的错误。
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
请帮忙!:)
试试这样,
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;
&color1
是一个替换变量,sqlplus 基本上用用户输入内容替换了&color1
- 这里必须是单引号文字。只要不是,您的blue
输入就会被视为未定义的变量名称。
因此,您有两种选择 - 要么在 sqlplus 中输入带引号的 'blue'
、'yellow'
等,要么在代码中将&color1
替换为 '&color1'
。
试试这个
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;
/