Function - oracle (PLS-00103: Encountered the symbol "") - sql

I'm trying to create a simple oracle function which loops over some records and then inserts records for each of those ..
CREATE OR REPLACE FUNCTION addNewRolesToAllGDP
return NUMBER
is dummy number;
BEGIN
FOR applicationId IN (SELECT APPID
FROM GRPAPPLICATIONINSTANCES
where GRPAPPID = (select GRPAPPID
from GRPAPPLICATIONS
where GRPNAME = 'DIGITAL_OFFICE')
AND APPID in (select APPID from APPLICATIONS where REGEXP_LIKE(APPNAME, '[[:digit:]]')))
LOOP
INSERT INTO ROLES (ROLID, ROLNAME, APPID)
VALUES (SEQROLES.nextval,
'INVENTORY_REQUESTER',
applicationId);
INSERT INTO ROLES (ROLID, ROLNAME, APPID)
VALUES (SEQROLES.nextval,
'INVENTORY_OWNER',
applicationId);
INSERT INTO ROLES (ROLID, ROLNAME, APPID)
VALUES (SEQROLES.nextval,
'INVENTORY_ADMIN',
applicationId);
END LOOP;
RETURN 1;
END;
alter function addNewRolesToAllGDP compile;
This statements gives me the following in USER_ERRORS:
PLS-00103: Encountered the symbol "" when expecting one of the following: ( return compress compiled wrapped

I'm not sure if this is the problem, but I notice that you do not have a slash following the END statement of the function. In Oracle tools, without that slash, the statement does not actually get terminated, and the ALTER command is included as part of the same statement. This can cause weird syntax errors.
If this is the problem, fundamentally this question is a duplicate of oracle SQL plus how to end command in SQL file?.
Edited to add As others have said in comments, the same code seems to compile fine for me when I cut-and-paste it from your post. Based on the error and the position where it is reported, my best guess is that at the end of the first line your original source has some invisible character that is causing the parser error.

Used to run this in Intellij which gave me the stated behaviour. After running it from sqlDeveloper it compiled fine.

I got a similar error message for a missing comma in the procedure definition and got compiled after adding the comma.
Procedure definition before fix
PROCEDURE CREATE_WO(p_wo_type IN NUMBER,
p_id IN NUMBER,
p_do_commit IN VARCHAR2 DEFAULT 'Y'
p_return_value OUT VARCHAR2)
IS
BEGIN
...
END;
Error Message
Cause: java.sql.SQLException: ORA-06550: line 8, column 9:
PLS-00103: Encountered the symbol "" when expecting one of the following:
) , * & = - + < / > at in is mod remainder not rem =>
<an exponent (**)> <> or != or ~= >= <= <> and or like like2
like4 likec between || multiset member submultiset
Procedure definition after fix, note the comma at the end of p_do_commit
PROCEDURE CREATE_WO(p_wo_type IN NUMBER,
p_id IN NUMBER,
p_do_commit IN VARCHAR2 DEFAULT 'Y',
p_return_value OUT VARCHAR2)
IS
BEGIN
...
END;

Related

Is there anyway to test the function in SQL?

I have following problem. I've made an function which checks dates, warehouse if it's full, etc..
Here is the code and I've added comments so it is more clear and understanble:
SET SERVEROUTPUT ON
CREATE OR REPLACE
PROCEDURE Anlieferung (P_Artikelbezeichnung VARCHAR, P_Datum date, P_Stueck INT) AS
_inputQuantity int;
_currentQuantity int;
_inputItem int;
_articleNummber int;
_quanitityToInput int:=P_Stueck;
_maximumLnr int;
BEGIN
FOR v_rec IN (SELECT * FROM lager) LOOP -- implicit cursor
_currentQuantity:=0;
SELECT artikel.anr -- Adding our article nummber in our variable called "_articleNummber"
INTO _articleNummber
FROM artikel
WHERE artikel.bezeichnung = P_Artikelbezeichnung;
FOR buchung IN(SELECT * FROM lagerbuchung lag --Checking the lnr and the date
WHERE lag.lnr = v_rec.lnr AND P_Datum >= lag.datum)
LOOP
--Menge in _currentQuantity einfügen
_currentQuantity:=_currentQuantity + buchung.stueck; --Adding our current quantity in our variable called "_currentQuantity"
END LOOP;
IF(v_rec.stueckkap > _currentQuantity OR _quanitityToInput >0) --Checking our capacity
THEN
_inputItem:=v_rec.stueckkap - _currentQuantity;
IF(_inputItem <= _quanitityToInput)
THEN
_inputQuantity:= _inputItem;
_quanitityToInput:=_quanitityToInput - _inputItem;
ELSE
_inputQuantity:= _quanitityToInput;
_quanitityToInput:=0;
END IF;
SELECT COALESCE(MAX(lfndnr),0) -- If we have null in our table, we will also get null in return
INTO _maximumLnr
FROM lagerbuchung;
_maximumLnr:=_maximumLnr +1;
--Inserting our new values in our table
INSERT INTO lagerbuchung (lfndnr, datum, stueck, anr, lnr) VALUES (_maximumLnr, P_Datum, _inputQuantity, _articleNummber, v_rec.lnr);
END IF;
END LOOP;
END Anlieferung;
/
SELECT l.LNR, SUM(lb.STUECK) FROM LAGER l JOIN LAGERBUCHUNG lb on l.lnr = lb.lnr group by l.lnr order by l.lnr;
EXEC Anlieferung('Apfel', '27.11.2015', 160);
SELECT l.LNR, SUM(lb.STUECK) FROM LAGER l JOIN LAGERBUCHUNG lb on l.lnr = lb.lnr group by l.lnr order by l.lnr;
So basically if I run only the function I don't get any error. But when I run it with the test date I get following error:
Fehler beim Start in Zeile : 166 in Befehl -
EXEC Anlieferung('Apfel', '27.11.2015', 160)
Fehlerbericht -
ORA-06550: line 1, column 7:
PLS-00905: object IF4EBIHORACM.ANLIEFERUNG is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
I've tried to debbug it and also input different test dates but still, same error.
After you compile the procedure either run:
SHOW ERRORS
or (assuming you are compiling it as the owning user and not as a DBA user; if you are not the owning user then use ALL_ERRORS):
SELECT * FROM USER_ERRORS;
Either will give you a list of the errors in the procedure and you can then debug it and fix the errors so the procedure works.
Update
From the comment:
2/1 PLS-00103: Encountered the symbol "_" when expecting one of the following: begin function pragma procedure subtype type <an identifier> <a double-quoted delimited-identifier> current cursor delete exists prior external language
Could it be because of my variables? They start with underscore
From the Database Object Names and Qualifiers documentation:
Database Object Naming Rules
Every database object has a name. In a SQL statement, you represent the name of an object with a quoted identifier or a nonquoted identifier.
A quoted identifier begins and ends with double quotation marks ("). If you name a schema object using a quoted identifier, then you must use the double quotation marks whenever you refer to that object.
A nonquoted identifier is not surrounded by any punctuation.
...
Nonquoted identifiers must begin with an alphabetic character from your database character set. Quoted identifiers can begin with any character.
You have many identifiers for variables that begins with an underscore. This tells you that the solution is to change the variable names to start with an alphabetic character (or to use a quoted identifier and surround the identifier with double quotes; but don't use quoted identifier, just use a letter as a prefix before the underscore):
CREATE OR REPLACE PROCEDURE Anlieferung (
P_Artikelbezeichnung VARCHAR,
P_Datum date,
P_Stueck INT
)
AS
v_inputQuantity int;
v_currentQuantity int;
v_inputItem int;
v_articleNummber int;
v_quanitityToInput int:=P_Stueck;
v_maximumLnr int;
BEGIN
...
Also, don't use:
SELECT COALESCE(MAX(lfndnr),0) -- If we have null in our table, we will also get null in return
INTO _maximumLnr
FROM lagerbuchung;
_maximumLnr:=_maximumLnr +1;
As it may generate duplicate values if the procedure is called twice at the same time.
Instead, create a sequence for the column and get the next value from the sequence.
CREATE SEQUENCE lagerbuchung__lfndnr__seq START WITH 213 INCREMENT BY 1;
(Start with whatever is 1 more than your current maximum value.)
Then in your procedure use:
INSERT INTO lagerbuchung (
lfndnr,
datum,
stueck,
anr,
ln
) VALUES (
lagerbuchung__lfndnr__seq.NEXTVAL,
P_Datum,
v_inputQuantity,
v_articleNummber,
v_rec.lnr
);
The message
object IF4EBIHORACM.ANLIEFERUNG is invalid
means there are errors in the ANLIEFERUNG routine. Check the source code and fix errors in there

How to check if an sequence is above a certain number and if not change it in Postgres

I have a problem where my SQL sequence has to be above a certain number to fix a unique constraint error. Now I started to write an if-statement that checks for a certain number and if it's below it should be increased to a certain number. The statement is for Postgres.
I got the separate parts running but the connection over if is throwing an error and I don't know why.
First for selecting the current number:
SELECT nextval('mySequence')
Then to update the number:
SELECT setval('mySequence', targetNumber, true)
The full statement looks something like this in my tries:
IF (SELECT nextval('mySequence') < targetNumber)
THEN (SELECT setval('mySequence', targetNumber, true))
END IF;
and the error is
ERROR: syntax error at »IF«
Can someone explain to me what I did wrong there because the error message isn't giving me much to work with? I would appreciate your help.
Try this:
SELECT setval('mySequence', targetNumber, true)
WHERE (SELECT nextval('mySequence') < targetNumber) is true;
You can use postgres functions if you want to use IF statement.
You can try something like this:
CREATE SEQUENCE seq_test_id_seq;
CREATE TABLE seq_test(
id integer NOT NULL DEFAULT nextval('seq_test_id_seq'),
name VARCHAR
);
CREATE OR REPLACE FUNCTION seq_test_function(target_number bigint)
RETURNS void
LANGUAGE 'plpgsql'
VOLATILE
PARALLEL UNSAFE
COST 100
AS $BODY$
DECLARE
seq_val INTEGER;
BEGIN
SELECT nextval('seq_test_id_seq') INTO seq_val;
RAISE NOTICE 'NEXT SEQUENCE [%]', seq_val;
IF (seq_val < target_number) THEN
SELECT setval('seq_test_id_seq', target_number, true) INTO seq_val;
RAISE NOTICE 'SEQUENCE VALUE MODIFIED [%]', seq_val;
END IF;
END;
$BODY$;
Then call the procedure:
select seq_test_function(10);

Trying to insert data into various tables via stored procedure, what am i doing wrong?

I am trying to get a JAVA program (Pega to be exact) to call the procedure to load XML data by calling a stored procedure, but it is not working, what am i doing wrong? I know it has something to do with my variable definitions but i am not sure how i would be able to specify that they will be provided parameters by the java file. below is my stored procedure. Thanks in advance
The error messages I'm getting are:
Error(2,16): PLS-00103: Encountered the symbol ";" when expecting one of the following: := . ) , # % default character
and
Error(16,1): PLS-00103: Encountered the symbol ")" when expecting one of the following: begin function pragma procedure subtype type current cursor delete exists prior
Create or Replace Procedure Cascade_Load (
Value_ID Number,
pValue_ID Number,
pCalculation_ID Number,
Calculation_ID Number,
Calculation_Value_ID Number,
p_Entity_Address_ID Varchar2(50),
New_Value_ID Number,
New_Calculation_ID Number,
New_Calculation_Value_ID Number
) AS
BEGIN
IF code is not null
THEN
INSERT INTO Value (Value_ID, energy_product_id, data_source_id, unit_cd,
value_tx, padd_cd, supply_type_id, country_cd, state_cd, county_cd,
entity_address_id, series_id, energy_process_cd, result_type_cd,
geo_area_cd, create_dt, create_user_id)
VALUES (
Value_ID,
Get_energy_product_id(:NEW.Product_Name_Cd),
Get_Data_Source_Id(:NEW.Data_Source_Tx),
:NEW.UNIT_CD ,
:NEW.Value_Tx,
:NEW.PADD_CD,
Get_Supply_Type_Id(:NEW.Supply_Type_Tx),
:NEW.COUNTRY_CD,
Get_State_CD(Get_entity_Id(p_Entity_Address_ID)),
'NA',
Get_Entity_Address_ID(Get_Entity_ID(p_Entity_Address_ID)),
0,
:NEW.Energy_Process_CD,
:NEW.Result_Type_CD,
:NEW.Geo_Area_Cd,
Sysdate,
'15'
);
Commit;
END IF;
END;
#icerabbit
Am not sure if it is a syntax error for having semicolons to separate parameters instead of comas. If this was the case You would've got an error message while compiling itself.
What is that code in the body of the SP, is that a variable which is not defined?
If Value is a table name, try enclosing it in the braces.
If you are seeing an error message, please post that too, it'll give some idea for debugging further.
Thank you
:NEW and :OLD variables are used just in TRIGGERS.
You can't BIND then inside a stored procedure.
Replace the semicolon with comma in your stored procedure's parameters

Create a trigger

Create a trigger called NewEntry, that will not allow a result to be inserted into the student exam table if it is less than zero. In your query display a suitable message if the result is less than zero and supply code to test the trigger.
This is what I have done, but I keep getting
warning : trrigger created with compilation errors.
This is my code, please help!
CREATE OR REPLACE TRIGGER NewEntry
AFTER INSERT OR UPDATE ON Assignment2.Student_Exam
FOR EACH ROW
DECLARE
Results NUMBER;
BEGIN
IF(:NEW.Results < '0')
THEN
RAISE_APPLICATION_ERROR
(-20700,'Student's result cannot be less-than ZERO..Enter valid Results':);
ENDIF;
END;
/
END IF, not ENDIF. Also as Jeremy C mentions above you need to create the trigger as BEFORE INSERT OR UPDATE
you listen the other answers but one more thing ,
you must focus on :NEW.Results this is the thing.
There are two errors: Firstly it is 'END IF' not ENDIF, secondly you have apostrophe's in your message.
The correct code is:
CREATE OR REPLACE TRIGGER NewEntry
AFTER INSERT OR UPDATE ON Assignment2.Student_Exam
FOR EACH ROW
DECLARE
Results NUMBER;
BEGIN
IF(:NEW.Results < '0')
THEN
RAISE_APPLICATION_ERROR
(-20700, 'students result cannot be less-than ZERO..Enter valid Results:');
END IF;
END;
/
As others have said you could have de-bugged this yourself by typing show errors; This would have given you the answer e.g.
SQL> show errors;
Errors for TRIGGER NEWENTRY:
LINE/COL ERROR
-------------------------------------------------------------------------
7/18 PLS-00103: Encountered the symbol "S" when expecting one of the
following:
) , * & = - + < / > at in is mod remainder not rem =>
<an exponent (**)> <> or != or ~= >= <= <> and or like like2
like4 likec between || multiset member submultiset
edit: In reply to Ersin - If it was New.Results then you would get a bind error e.g.
SQL> show errors;
Errors for TRIGGER NEWENTRY:
LINE/COL ERROR
-------- -----------------------------------------------------------------
4/4 PLS-00049: bad bind variable 'NEW.RESULTS'
SQL>
This will be a result of 'RESULTS' not being a column in Student_Exam, which I presume does exist given the rest of the statement.

PLS-00103 error with anonymous procedure

I got an error
PLS-00103: Encountered the symbol "end-of-file" when expecting one of
the following: [...]
when running an anonymous procedure (with Oracle):
BEGIN
DECLARE
seq number(12);
pk number(12);
BEGIN
loop
select mod_sdemol.nextval into seq from dual;
select idn_demol into pk from demol where demol.idn_demol=seq;
exit when pk is null;
end loop;
INSERT INTO "T_MOD"."DEMOL" (IDN_DEMOL, COD_MOL, PATH, IND_BLOK) VALUES (seq, '13000501', 'V', 'S');
END;
What I am trying to do is iterate through a sequence to prevent conflicts with existing data.
According to the answers in this question, a PL/SQL procedure should do something with selected data, but all my SELECTs have INTOs.
What am I doing wrong or what am I missing?
You don't need the first BEGIN. Each BEGIN keyword must match an END keyword.