It seems ok but am getting exception please correct me.
declare
var_number number;
begin
var_number := 10;
if var_number > 100 then
dbms_output.put_line(var_number||' is greater than 100');
elseif var_number < 100 then
dbms_output.put_line(var_number||' is less than 100');
else
dbms_output.put_line(var_number||' is equal to 100');
end if;
end;
Exception :
ORA-06550: line 8, column 8:
PLS-00103: Encountered the symbol "VAR_NUMBER" when expecting one of the following:
:= . ( @ % ;
ORA-06550: line 13, column 4:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
if
This question is related to
sql
oracle
plsql
syntax-error
begin
var_number := 10;
if var_number > 100 then
dbms_output.put_line(var_number||' is greater than 100');
else if var_number < 100 then
dbms_output.put_line(var_number||' is less than 100');
else
dbms_output.put_line(var_number||' is equal to 100');
end if;
end if;
The IF statement has these forms in PL/SQL
:
IF THEN
IF THEN ELSE
IF THEN ELSIF
You have used elseif
which in terms of PL/SQL is wrong. That need to be replaced with ELSIF
.
DECLARE
mark NUMBER :=50;
BEGIN
mark :=& mark;
IF (mark BETWEEN 85 AND 100) THEN
dbms_output.put_line('mark is A ');
elsif (mark BETWEEN 50 AND 65) THEN
dbms_output.put_line('mark is D ');
elsif (mark BETWEEN 66 AND 75) THEN
dbms_output.put_line('mark is C ');
elsif (mark BETWEEN 76 AND 84) THEN
dbms_output.put_line('mark is B');
ELSE
dbms_output.put_line('mark is F');
END IF;
END;
/
The IF statement has these forms in PL/SQL
:
IF THEN
IF THEN ELSE
IF THEN ELSIF
You have used elseif
which in terms of PL/SQL is wrong. That need to be replaced with ELSIF
.
So your code should appear like this.
declare
var_number number;
begin
var_number := 10;
if var_number > 100 then
dbms_output.put_line(var_number ||' is greater than 100');
--elseif should be replaced with elsif
elsif var_number < 100 then
dbms_output.put_line(var_number ||' is less than 100');
else
dbms_output.put_line(var_number ||' is equal to 100');
end if;
end;
The problem is that the else and if are two operators here. Since you open a new 'if' you need a corresponding 'end if'.
Thus:
declare
mark number :=50;
begin
mark :=& mark;
if (mark between 85 and 100) then
dbms_output.put_line('mark is A ');
else
if (mark between 50 and 65) then
dbms_output.put_line('mark is D ');
else
if (mark between 66 and 75) then
dbms_output.put_line('mark is C ');
else
if (mark between 76 and 84) then
dbms_output.put_line('mark is B');
else
dbms_output.put_line('mark is F');
end if;
end if;
end if;
end if;
end;
/
Alternatively you can use elsif:
declare
mark number :=50;
begin
mark :=& mark;
if (mark between 85 and 100)
then
dbms_output.put_line('mark is A ');
elsif (mark between 50 and 65) then
dbms_output.put_line('mark is D ');
elsif (mark between 66 and 75) then
dbms_output.put_line('mark is C ');
elsif (mark between 76 and 84) then
dbms_output.put_line('mark is B');
else
dbms_output.put_line('mark is F');
end if;
end;
/
Source: Stackoverflow.com