Creating Oracle SQL Java Function does not work - oracle

When trying to create oracle SQL java function to generate random UUID as described in this StackOverflow answer, I get following error:
sql> CREATE or REPLACE FUNCTION random_uuid
RETURN VARCHAR2
AS LANGUAGE JAVA NAME 'java.util.UUID.randomUUID() return java.lang.String'
[2019-01-29 09:28:29] [99999][17110] Warning: execution completed with warning
[2019-01-29 09:28:29] completed in 23 ms
[2019-01-29 09:28:29] 3:78:PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
[2019-01-29 09:28:29] ;
[2019-01-29 09:28:29] The symbol ";" was substituted for "end-of-file" to continue.
Even this is just a warning, calling it does not work afterwards:
sql> select random_uuid() from dual
[2019-01-29 09:36:28] [65000][6575] ORA-06575: Package or function RANDOM_UUID is in an invalid state
I use IntelliJ IDEA's Database Console to execute the script. What is wrong?
EDIT: Mine original script does contain semicolon at the end:
create or replace function random_uuid return varchar2 as
language java
name 'java.util.UUID.randomUUID() return String';
Adding ';' after the String does not help:
create or replace function random_uuid return varchar2 as
language java
name 'java.util.UUID.randomUUID() return String;';

Since the problem appears to be the IDE not recognising where the statement terminates then you can try to wrap it in a PL/SQL anonymous block and use EXECUTE IMMEDIATE to force it to parse the statement correctly:
BEGIN
EXECUTE IMMEDIATE 'CREATE OR REPLACE FUNCTION RandomUUID RETURN VARCHAR2 AS LANGUAGE JAVA NAME ''java.util.UUID.randomUUID() return java.lang.String'';';
END;
/

Related

Getting an error while creating a PL/SQL function

Question is : Function: Create a Function named 'find_credit_card' which takes card_no as input and returns the holder name of type varchar.
Function name: find_credit_card
Input Parameter: card_no with data type as varchar
Output variable : holder_name with data type as varchar(30)
Hint: Add '/' after the end statement
Refer to the schema.
My code :-
CREATE OR REPLACE FUNCTION find_credit_card(card_no IN VARCHAR2(255))
RETURN VARCHAR
IS
holder_name VARCHAR(255)
BEGIN
SELECT name
INTO holder_name
from credit_card
where card_number = card_no;
RETURN(holder_name);
END;
/
Warning : Function created with compilation errors.
Error(s) you got can be reviewed in SQL*Plus by running show err:
Warning: Function created with compilation errors.
SQL> show err
Errors for FUNCTION FIND_CREDIT_CARD:
LINE/COL ERROR
-------- -----------------------------------------------------------------
1/46 PLS-00103: Encountered the symbol "(" when expecting one of the
following:
:= . ) , # % default character
The symbol ":=" was substituted for "(" to continue.
5/1 PLS-00103: Encountered the symbol "BEGIN" when expecting one of
the following:
:= ; not null default character
SQL>
Alternatively, query user_errors:
select line, position, text
from user_errors
where name = 'FIND_CREDIT_CARD';
Oracle says that there are two errors:
first one means that function's parameter shouldn't have size, so - not card_no in varchar2(255) but only card_no in varchar2
another one means that you forgot to terminate line where local variable was declared (missing semi-colon at the end of that line):
holder_name VARCHAR(255);
However, consider inheriting datatype from column description. If it is ever changed, you wouldn't have to modify your code.
Furthermore, it would be good if you distinguish parameters and local variables from column names. How? Use prefixes, e.g. par_ for parameters, l_ or v_ for local variables.
Also, Oracle recommends us to use varchar2, not varchar.
Finally, that function might look like this:
SQL> CREATE OR REPLACE FUNCTION find_credit_card
2 (par_card_number IN credit_card.card_number%TYPE)
3 RETURN credit_card.name%type
4 IS
5 l_holder_name credit_card.name%TYPE;
6 BEGIN
7 SELECT name
8 INTO l_holder_name
9 FROM credit_card
10 WHERE card_number = par_card_number;
11
12 RETURN l_holder_name;
13 END;
14 /
Function created.
SQL> select find_credit_card('HR123456789') holder from dual;
HOLDER
---------------------------------------------------------------------
Littlefoot
SQL>

PL/SQL - Can't create package because of my function

Im trying to create a package and it works with procedures-only. As soon as I try to add a function following errors appear:
15:47:11 line 1: ORA-24344: success with compilation error
15:47:11 14/3 PLS-00103: Encountered the symbol "TITLE" when expecting one of the following:
15:47:11 . # % ; is default authid as cluster order using external
15:47:11 character deterministic parallel_enable pipelined aggregate
15:47:11 result_cache accessible rewrite
15:47:11 The symbol ";" was substituted for "TITLE" to continue.
My code:
create or replace package job_pkg is
procedure add_job
(jobid jobs_test.job_id%type, jobtitle jobs_test.job_title%type);
---------------------------------------------------------------------
procedure upd_job
(alt jobs_test.job_id%type, neu jobs_test.job_id%type);
---------------------------------------------------------------------
procedure del_job
(jobid jobs_test.job_id%type);
---------------------------------------------------------------------
function get_job
(id jobs_test.job_id%type) return varchar2
title jobs_test.job_title%type;
end job_pkg;
Thank you in advance
You're missing a semicolon.
function get_job
(id jobs_test.job_id%type) return varchar2;
title jobs_test.job_title%type;
Assuming you're creating a package level variable 'title' AND declaring your function spec here.

procedure execution in oracle sql

I want to execute a procedure inside another procedure, which includes a cursor definition,by inserting this line:
create or replace
PROCEDURE SAP_IMP_IE_FGZ_INSCRI_RULES (V_START_DATE IN DATE
DEFAULT SYSDATE, v_count_max IN NUMBER DEFAULT 1000) AS
EXECUTE procedure SAP_IMP_FGZ_INSCRI_DBL;
CURSOR c_CURSEUR IS
SELECT
..
but it displays for me the following error .
Note that the procedure includes a cursor definition
Erreur(3,1): PLS-00103: Encountered the symbol "DECLARE" when expecting
one of the following: begin function package pragma procedure subtype
type use <identificateur> <identificateur entre guillemets> form
current cursor external language

error when entering values by Oracle PL/SQL

I want to enter a value by console in Oracle 10g with PL SQL. I am using the web based interface and not the SQL*.
The program is like this:
DECLARE
v_desc VARCHAR2(50);
BEGIN
v_desc:=show_desc(&sv_cnumber);
DBMS_OUTPUT.PUT_LINE(v_desc);
END;
and the function is:
CREATE OR REPLACE FUNCTION show_desc
(i_course_id course.course_id%TYPE)
RETURN varchar2
AS
v_desc varchar2(50);
BEGIN
SELECT description
INTO v_desc
FROM courses
WHERE course_id=i_course_id;
RETURN v_desc;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
RETURN('no description found');
END;
when I run this code, I got an error that says:
ORA-06550: line 4, column 37:
PLS-00103: Encountered the symbol "&" when expecting one of the following:
what is the mistake?
Thanks
If you want a prompt to be displayed so you can enter a value for the variable, try replacing & with colon
v_desc:=show_desc(:sv_cnumber);
& is a special symbol in PL/SQL

Error PLS-00103 compiling user-defined function in Oracle

I'm trying to create a user defined function in Oracle that will return a DATE when given a text argument containing a date substring. I've tried a couple ways of writing this, and all seem to throw the same error:
CREATE OR REPLACE FUNCTION lm_date_convert (lm_date_in IN VARCHAR2(50))
RETURN DATE DETERMINISTIC IS
BEGIN
RETURN(TO_DATE(REGEXP_REPLACE(lm_date_in, '([[:digit:]]{2})[-/.]*([[:digit:]]{2})[-/.]*([[:digit:]]{4})','\3-\1-\2'), 'YYYY-MM-DD'));
END;
the error:
FUNCTION lm_date_convert Compiled. 1/46
PLS-00103: Encountered
the symbol "(" when expecting one of
the following:
:= . ) , # % default character The
symbol ":=" was substituted for "(" to
continue.
Any thoughts on this, and general UDF writing tips (and good references) are welcome! Thanks.
We cannot restrict the datatype when specifying parameters in stored procedures. That is, just use VARCHAR2 rather than VARCHAR2(50).
Just to prove I'm reproducing your problem ...
SQL> CREATE OR REPLACE FUNCTION lm_date_convert (lm_date_in IN VARCHAR2(50))
2 RETURN DATE DETERMINISTIC IS
3 BEGIN
4 RETURN(TO_DATE(REGEXP_REPLACE(lm_date_in, '([[:digit:]]{2})[-/.]*([[:digit:]]{2})[-/.]*([[:digit:]]{4})','\3-\1-\2'), 'YYYY-MM-DD'));
5 END;
6 /
Warning: Function created with compilation errors.
SQL> sho err
Errors for FUNCTION LM_DATE_CONVERT:
LINE/COL ERROR
-------- -----------------------------------------------------------------
1/49 PLS-00103: Encountered the symbol "(" when expecting one of the
following:
:= . ) , # % default character
The symbol ":=" was substituted for "(" to continue.
SQL>
Now to fix it:
SQL> ed
Wrote file afiedt.buf
1 CREATE OR REPLACE FUNCTION lm_date_convert (lm_date_in IN VARCHAR2)
2 RETURN DATE DETERMINISTIC IS
3 BEGIN
4 RETURN(TO_DATE(REGEXP_REPLACE(lm_date_in, '([[:digit:]]{2})[-/.]*([[:digit:]]{2})[-/.]*([[:digit:]]{4})','\3-\1-\2'), 'YYYY-MM-DD'));
5* END;
SQL> r
1 CREATE OR REPLACE FUNCTION lm_date_convert (lm_date_in IN VARCHAR2)
2 RETURN DATE DETERMINISTIC IS
3 BEGIN
4 RETURN(TO_DATE(REGEXP_REPLACE(lm_date_in, '([[:digit:]]{2})[-/.]*([[:digit:]]{2})[-/.]*([[:digit:]]{4})','\3-\1-\2'), 'YYYY-MM-DD'));
5* END;
Function created.
SQL>
"If you really do want a VARCHAR2(50)
then declare a type of VARCHAR2(50)
and use the type."
Declaring a SQL TYPE to enforce sizing is a bit of overkill. We can declare SUBTYPEs in PL/SQL but their sizes are not actually enforced in stored procedure signatures. However there are workarounds as I discuss in this other thread.
As an aside, why are you using Regex to solve this problem? Or rather, what problem are you trying to solve which cannot be solved with TO_CHAR and TO_DATE? Oracle's pretty forgiving with format masks.

Resources