executeSqlScript fails with Spring for PL/SQL block - sql

Im trying to populate my database using the builtin function executeSqlScript from AbstractTransactionalJUnit4SpringContextTests using the following external SQL file.
declare
id number;
begin
insert into table1 (field1) values ('V1') returning account__id into id;
insert into table2 (my_id, field2) VALUES (id, 'Value3');
end;
However im getting the following error. Im not sure what im allowed to do in a SQL file I would like to execute using executeSqlScript.
org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement at line 1 of resource class path resource [testdata.sql]: declare id number; nested exception is java.sql.SQLException: ORA-06550: line 1, column 17:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
:= . ( # % ; not null range default character
Caused by: java.sql.SQLException: ORA-06550: line 1, column 17:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
:= . ( # % ; not null range default character
So my questions are:
What am I allowed to express in the SQL file for executeSqlScript?
What is the cause of the error im reciving?

It appears that you are attempting to use features of PL/SQL in your script.
The executeSqlScript(..) methods in AbstractTransactionalJUnit4SpringContextTests internally delegate to ScriptUtils.executeSqlScript(..) behind the scenes, and ScriptUtils only supports pure SQL scripts.
So you'll likely need to switch to simple SQL statements and find a different mechanism for retrieving the value of the account__id from table1.
Another option (which I have not tried) would be to change the statement separator to something other than ";" (e.g., "end;"), but you cannot do that via AbstractTransactionalJUnit4SpringContextTests.executeSqlScript. Instead, you'd need to invoke ScriptUtils.executeSqlScript(..) or (perhaps preferably) use a ResourceDatabasePopulator.

Related

Why am I receiving the PSL-00103 Error in Oracle SQL?

I am trying to make a procedure that would insert a row into a table in Oracle SQL. However, I can't figure out a solid reason on why this issue exists when I write any type of procedure.
I have tried changing the syntax around a couple of times, but I still don't know how to remedy the issue.
Errors given:
Error at line 1: PLS-00103: Encountered the symbol ")" when expecting one of the following: in out table ... columns long double ref char time timestamp interval date binary national character nchar
Error at line 5: PL/SQL: SQL Statement ignored Error at line 6: PL/SQL: ORA-00984: column not allowed here
Code:
create or replace procedure insert_category(
category_name_param in categories.category_name%type)
as
begin
insert into categories (category_id, category_name)
values (category_id, category_name_param);
end;
It seems that procedure lacks in yet another parameter; see if this helps (I presumed that such a column exists in the table; can't be sure as you didn't post table description):
create or replace procedure insert_category(
category_name_param in categories.category_name%type,
category_id_param in categories.category_id%type --> this
)
as
begin
insert into categories
(category_id,
category_name)
values
(category_id_param, --> this
category_name_param);
end;

PLS-00103: Encountered the symbol "CREATE" when expecting one of the

I am trying to create a stored procedure in oracle 12c database and I am getting error when I am running code to store the procedure.
PLS-00103: Encountered the symbol "CREATE" when expecting one of the
There are multiple stack overflow question already asked on this topic. but they suggest some different syntax. which is deviation from actual oracle documentation. and even those didn't worked for me
I checked for documentation on multiple website including oracle documentation. oracle documetation suggest syntax as following
Oracle STORED PROCEDURE DOCUMENTATION
so I as per the syntax I wrote the following procedure.
CREATE PROCEDURE PDD_PROC_BASE
AS
--DROP TABLE BASE;
CREATE TABLE BASE as
SELECT idno
,DATE
,diff
,SUBSTR(idNO,7,2) AS PRD
,COMPLETED
,CATG
,OP_Number
FROM table1
WHERE = date >= '30-JUN-2018'
AND STATUS = 'G'
END;
and I got the following error.
Procedure PDD_PROC_BASE compiled
Errors: check compiler log
Errors for PROCEDURE AN_5043152.PDD_PROC_BASE:
LINE/COL ERROR
-------- ------------------------------------------------------------------------------
7/5 PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
json_exists json_value json_query json_object json_array
I checked other resources as well but still didn't understood what went wrong.
I even tried code example from Oracle documentation and got similar error.
I am using SQLdeveloper tool as client
You cant use directly sql ddl statements in plsql block, you can do the same thing using dynamic sql with the "EXECUTE IMMEDIATE" statement like this:
begin
execute immediate 'create table test_table1 (test_column1 varchar2(40))';--your create table statement here
end;
In Oracle in stored procedures you can use DDL statements only as dynamic SQL, that means as EXECUTE IMMEDIATE 'CREATE TABLE ' ...
Check here, for example: http://www.dba-oracle.com/t_using_ddl_create_index_table_plsql.htm

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

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;

I am executing following procedure in oracle but get compilation error

Actually I want to get usernames from table using Stored Procedure
CREATE OR REPLACE PROCEDURE GetRecord
(
p_ID IN integer ,
p_user OUT VARCHAR2
)
AS
BEGIN
SELECT
USERNAMES
INTO
p_user
FROM tblUsers
WHERE ID = p_ID ;
END GetRecord;
BEGIN
DECLARE A VARCHAR2
EXECUTE GetRecord(21,A);
END
When I run the above procedure I got following errors
Error starting at line : 17 in command -
BEGIN
DECLARE A VARCHAR2
EXECUTE GetRecord(21,A);
END
Error report -
ORA-06550: line 3, column 1:
PLS-00103: Encountered the symbol "EXECUTE" when expecting one of the following:
:= . ( # % ; not null range default character
The symbol ";" was substituted for "EXECUTE" to continue.
ORA-06550: line 4, column 1:
PLS-00103: Encountered the symbol "END" when expecting one of the following:
begin function pragma procedure subtype type <an identifier>
<a double-quoted delimited-identifier> current cursor delete
exists prior
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Actually I want to get usernames from table using Stored Procedure
The code that you are using to execute your procedure appears to be incorrect. It sounds like you want
DECLARE
l_usernames tblUsers.usernames%type;
BEGIN
GetRecord( 21, l_usernames );
END;
In general, I'd strongly object to your naming convention. It makes no sense to use a plural usernames as a column name. A procedure named GetRecord tells you nothing about what it does-- GetUsername would be much more meaningful. Your anonymous block also appears to be declaring a local variable a that stores the returned username which also makes no sense from the standpoint of using meaningful identifiers.
I'd also suggest that a procedure is the wrong approach here. If your goal is to return a single value, use a function. Functions return things, procedures do not. A function can potentially be used in a SQL statement, a procedure cannot be.

stored functions execution issue

I have a table from where I want get next lowest priority DVDid, give me that as output and delete it from table. I'm doing this by creating a Package...code gets compiled but when I execute it give me error. Any suggestion?
table:
RENTALQUEUE (MEMBERID, DVDID, DATEADDEDINQUEUE, PRIORITY)
Here is the package:
create or replace
PACKAGE pk_GET_TITLEID AS
procedure sp_GET_TITLEID(memberid_arg IN NUMBER,
titleid_arg out number) ;
end pk_GET_TITLEID;
create or replace
package body pk_GET_TITLEID as
procedure sp_GET_TITLEID
(memberid_arg IN NUMBER,
titleid_arg out number)
IS
v_priority number;
begin
SELECT MIN(PRIORITY)into v_priority
FROM RENTALQUEUE
WHERE memberid = memberid_arg;
DBMS_OUTPUT.PUT_LINE (titleid_arg);
DELETE FROM RENTALQUEUE
WHERE MEMBERID=memberid_arg AND dvdid=titleid_arg;
END sp_GET_TITLEID;
END PK_GET_TITLEID;
Here is how I call it:
execute pk_get_titleid.sp_get_titleid('1');
Here is the error:
Error:
Error starting at line 403 in command:
execute pk_get_titleid.sp_get_titleid('1')
Error report:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'SP_GET_TITLEID'
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.
The error is:
PLS-00306: wrong number or types of arguments in call to 'SP_GET_TITLEID'
How many parameters does your procedure have in its signature?
procedure sp_GET_TITLEID
(memberid_arg IN NUMBER
, titleid_arg out number)
Two. How many parameters are you passing?
execute pk_get_titleid.sp_get_titleid('1')
One.
An out parameter is still a parameter. You need to include it in the call. Specifically you need a variable in the calling environment which can receive the answer from the called procedure. The fact that your code doesn't actually populate the titleid_arg parameter doesn't affect this.
The non-population of the titleid_arg parameter just highlights the confusion in your code. Perhaps it should be an IN parameter? Perhaps you need to select it from another table? What is the point of v_priority?