Is there any way around to make control statements work in apex?
SET SERVEROUTPUT ON;
DECLARE
v_num NUMBER := 12;
v_word VARCHAR := 'hello';
BEGIN
IF v_num < 10 THEN
DBMS.OUTPUT.PUT_LINE('IF condition executed.');
ELSIF v_word = 'hello' THEN
DBMS.OUTPUT.PUT_LINE('ELSIF condition executed.');
ELSE v_word = 'world' THEN
DBMS_OUTPUT.PUT_LINE('ELSE condition executed.')
END IF;
DBMS_OUTPUT.PUT_LINE('Outside the control statement.');
END;
This is the error I received every time I use control statements:
CodePudding user response:
There are many little things wrong with your code. Start with the smallest possible piece, check it works and then add code - best way to start writing code in any language.
Issue 1: set serveroutput on
is not needed in sql workshop. The server output is enabled by default and cannot be toggled. Try the following code:
BEGIN
dbms_output.put_line('hello');
END;
and observe it works. Then run this code
set serveroutput on
and... notice that you get the same error you're reporting !
Issue 2: typos typos typos. Each commented line below causes an error
DECLARE
v_num NUMBER := 12;
--for VARCHAR, you need to declare the precision
--no error but datatype VARCHAR should NOT be used. Instead use VARCHAR2
--v_word VARCHAR := 'hello';
v_word VARCHAR2(100) := 'hello';
BEGIN
IF v_num < 10 THEN
-- typo: there is no "DBMS.OUTPUT"
--DBMS.OUTPUT.PUT_LINE('IF condition executed.');
DBMS_OUTPUT.PUT_LINE('IF condition executed.');
ELSIF v_word = 'hello' THEN
-- typo: there is no "DBMS.OUTPUT"
--DBMS.OUTPUT.PUT_LINE('ELSIF condition executed.');
DBMS_OUTPUT.PUT_LINE('ELSIF condition executed.');
--No condition needed after the ELSE keyword.
--ELSE v_word = 'world' THEN
ELSE
DBMS_OUTPUT.PUT_LINE('ELSE condition executed.');
END IF;
DBMS_OUTPUT.PUT_LINE('Outside the control statement.');
END;