I get the following error, even though I have checked the code carefully. I don't know what I'm missing.
LINE/COL ERROR
31/1 PLS-00103: Encountered the symbol "END" when expecting one of the following:
CODE:
CREATE OR REPLACE PROCEDURE sp_ssjm_newworkorder
( workorderno IN NUMBER,
company IN CHAR,
attention IN CHAR,
datedue IN DATE,
loggedby IN CHAR
)
AS id NUMBER;
today DATE:=SYSDATE;
BEGIN
SELECT client_id --grab client_id
INTO id
FROM ssjm_client
WHERE ssjm_client.name=company;
IF id IS NULL THEN --check if client exists by checking if client_id is there
dbms_output.put_line('Please create client first');
GOTO the_end;
ELSE
INSERT INTO ssjm_workorder VALUES(workorderno,workorderno,company,loggedby,attention,'Received',today,datedue,id);
END IF;
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20999,'An error occured in' ||
SQLCODE || '-ERROR-' || SQLERRM);
<<the_end>>
END sp_ssjm_newworkorder;
There are several spots in your code needed attention:
<<the_end>> should be placed before the EXCEPTION section.NULL operator should be used. To that end your code should look like this:
IF id IS NULL THEN --check if client exists by checking if client_id is there
dbms_output.put_line('Please create client first');
GOTO the_end;
ELSE
INSERT INTO ssjm_workorder
VALUES(workorderno,workorderno,company,loggedby
,attention,'Received',today,datedue,id);
END IF;
<<the_end>>
NULL;
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20999,'An error occured in' ||
SQLCODE || '-ERROR-' || SQLERRM);
END sp_ssjm_newworkorder;
By all means try to avoid unconditional branching. Using GOTO operator is very, very not good practice. It kills readability, code like that hard to debug. It will cause you and everybody who will look at that code after you a headache. Moreover if the query
SELECT client_id --grab client_id
INTO id
FROM ssjm_client
WHERE ssjm_client.name=company;
returns no rows the exception NO_DATA_FOUND will be immediately raised and execution of the code halts. So IF id IS NULL THEN condition will never be evaluated. You may rewrite your code by removing that condition and adding NO_DATA_FOUND exception handler in the EXCEPTION section of your code. And of course as @Rob van Wijk correctly pointed out in the comment
but code can be cleaned up further. today variable can be removed and the WHEN OTHERS should definitely be removed. As it is now, it just transforms an error to a longer error message without more detail and most importantly: it disguises the line number where the real error took place.
there is no need of today variable, SYSDATE can be used directly in the values clause of the insert statement, and WHEN OTHERS can be removed as well.
CREATE OR REPLACE PROCEDURE sp_ssjm_newworkorder
(
workorderno IN NUMBER,
company IN CHAR,
attention IN CHAR,
datedue IN DATE,
loggedby IN CHAR
)
AS
id NUMBER;
BEGIN
SELECT client_id --grab client_id
INTO id
FROM ssjm_client
WHERE ssjm_client.name=company;
INSERT INTO ssjm_workorder
VALUES(workorderno,workorderno,company,loggedby
,attention,'Received',SYSDATE,datedue,id);
EXCEPTION
when NO_DATA_FOUND
then dbms_output.put_line('Please create client first');
END sp_ssjm_newworkorder;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With