Getting an error while creating a PL/SQL function - oracle

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>

Related

Warning: Function created with compilation errors

im having a error im so frustrate right now. I think due to this error im also having another error of cannot reference other columns while adding a check constraint in the table. I have to use this function in check constraint to compare with end time.
1 create or replace function timing(dat in date, bran in varchar2(30), audi in number)
2 return number is time number
3 begin
4 select s_end into time from checking
5 where s_date=dat and branch=bran and a_id = audi;
6 return time;
7* end timing
SQL> /
and my table is
Name Null? Type
----------------------------------------- -------- -------------
S_ID NOT NULL NUMBER
M_ID NUMBER
A_ID NUMBER
S_DATE DATE
S_START NUMBER
S_END NUMBER
BRANCH VARCHAR2(30)
The error is:
1/46 PLS-00103: Encountered the symbol "(" when expecting following: := . ) , # % default character The symbol ":=" was substituted for "(" to continue.
3/1 PLS-00103: Encountered the symbol "BEGIN" when expecting the following: := . ( # % ; not null range default character
The symbol ";" was substituted for "BEGIN" to continue.
7/10 PLS-00103: Encountered the symbol "end-of-file" when expecting
You have posted this same question in a slightly different form. I'll answer here in slightly different form. A check constraint cannot reference a user defined function. Oracle does not permit it so your contention "have to use this function in check constraint" cannot be!
try like this
create or replace function timing(dat in date, bran in varchar2, audi in number)
return number
is
time number;
begin
select s_end into time
from checking
where s_date=dat and branch=bran
and a_id = audi;
return time;
end :
/

PL/SQL Procedure is not running properly

Please consider the following code (query.sql):
create or replace function age (dateOfBirth date)
return number
is
mAge number(5,2);
begin
mAge:=(sysdate-dateOfBirth)/365.25;
return mAge;
end;
SQL> #query.sql
13
14
15 /
Warning: Function created with compilation errors.
And when I click on Show error, I get the following:
Code:
SQL> show error
Errors for FUNCTION AGE:
LINE/COL ERROR
-------- -----------------------------------------------------------------
5/8 PL/SQL: Item ignored
5/15 PLS-00325: non-integral numeric literal 5.2 is inappropriate in
this context
8/8 PL/SQL: Statement ignored
8/8 PLS-00320: the declaration of the type of this expression is
incomplete or malformed
9/5 PL/SQL: Statement ignored
9/12 PLS-00320: the declaration of the type of this expression is
incomplete or malformed
LINE/COL ERROR
-------- -----------------------------------------------------------------
I tried to do the following from : Oracle Procedure
1) SQL> set role none;
and
2) SELECT ON DBA_TAB_COLUMNS;
But the second query above is throwing error : Missing expression.
Please let me know what's wrong with all of the above stuff.
Thanks
You're missing a BEGIN and your NUMBER variable should be declared with a comma not a period.
create or replace function age (
pDateOfBirth date ) return number is
l_age number(5,2);
begin
l_age := ( sysdate - pDateOfBirth ) / 365.25;
return l_age;
end;
/
You've now edited the question to include the BEGIN but you haven't fixed your declaration of the variable. As your error message says:
PLS-00325: non-integral numeric literal 5.2 is inappropriate in this context
Personally, I believe you're calculating age incorrectly. There are 365 or 366 days in a year. I'd do this instead, which uses internal Oracle date functions:
function get_age (pDOB date) return number is
/* Return the the number of full years between
the date given and sysdate.
*/
begin
return floor(months_between(sysdate, pDOB)/12);
end;
That is if you only want the number of full years.

Why do I get an error as I try to call a procedure?

I created a procedure named greet as :
create procedure greet(message in char(50))
as
begin
dbms_output.put_line('Greet Message : ' || message);
end;
The procedure compiled successfully but when I try to call it as :
execute greet('Hey ! This is a self created procedure :)');
I get an error :
execute greet('Hey ! This is a self created procedure :)')
Error report:
ORA-06550: line 1, column 7:
PLS-00905: object SUHAIL.GREET 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:
What error is it ? Why do I get it ?
Note : 'suhail' is name of the current user connected to oracle server
I don't believe that your procedure compiled successfully. When I try to compile it on my system, I get syntax errors
SQL> create procedure greet(message in char(50))
2 as
3 begin
4 dbms_output.put_line('Greet Message : ' || message);
5 end;
6 /
Warning: Procedure created with compilation errors.
SQL> sho err
Errors for PROCEDURE GREET:
LINE/COL ERROR
-------- -----------------------------------------------------------------
1/32 PLS-00103: Encountered the symbol "(" when expecting one of the
following:
:= ) , default varying character large
The symbol ":=" was substituted for "(" to continue.
If I resolve the syntax errors (you cannot specify a length for an input parameter), it works
SQL> ed
Wrote file afiedt.buf
1 create or replace procedure greet(message in char)
2 as
3 begin
4 dbms_output.put_line('Greet Message : ' || message);
5* end;
SQL> /
Procedure created.
SQL> set serveroutput on;
SQL> execute greet('Hey ! This is a self created procedure :)');
Greet Message : Hey ! This is a self created procedure :)
PL/SQL procedure successfully completed.
I would be shocked if you really wanted the input parameter to be declared as CHAR. Almost always, you should use VARCHAR2 for character strings. It is exceptionally rare to come across a case where you really want the blank-padding semantics of a CHAR.
this is working dude;
create or replace
procedure greet(message in char)
as
begin
dbms_output.put_line('Greet Message : ' || message);
end;
see main property of char datatype is is the length of input data is less than the size you specified it'll add blank spaces.this case is not happened for varchar2.
in procedure above mentioned char property is violated so it's almost treat like varchar2. so if you remove size of input parameter it will work and also char support maximum length of input.

MD5 in Oracle (DBMS_OBFUSCATION_TOOLKIT.MD5)

I'm trying to compose a function to obtain MD5 hashes from bits I've gathered here and there. I want to obtain the lower-case hexadecimal representation of the hash. I have this so far:
CREATE OR REPLACE FUNCTION MD5 (
CADENA IN VARCHAR2
) RETURN DBMS_OBFUSCATION_TOOLKIT.VARCHAR2_CHECKSUM
AS
BEGIN
RETURN LOWER(
RAWTOHEX(
UTL_RAW.CAST_TO_RAW(
DBMS_OBFUSCATION_TOOLKIT.MD5(INPUT_STRING => CADENA)
)
)
);
END;
I'm not sure about the return type of the function. DBMS_OBFUSCATION_TOOLKIT.VARCHAR2_CHECKSUM looks like the appropriate choice and as far as I can tell it works as expected but the package definition for dbms_obfuscation_toolkit as displayed by SQL Developer shows this:
SUBTYPE varchar2_checksum IS VARCHAR2(16);
The output has 32 characters so I must be doing something wrong. My questions:
What's the correct type for the RETURN statement?
Am I doing unnecessary conversions to calculate the hash?
Here you go:
create or replace function getMD5(
in_string in varchar2)
return varchar2
as
cln_md5raw raw(2000);
out_raw raw(16);
begin
cln_md5raw := utl_raw.cast_to_raw(in_string);
dbms_obfuscation_toolkit.md5(input=>cln_md5raw,checksum=>out_raw);
-- return hex version (32 length)
return rawtohex(out_raw);
end;
The 32 length is because it is a hex representation of the raw(16) value. Or, modify above to output the raw version and store the raw in a RAW column (less space used, but you'll be doing future rawtohex and hextoraw conversions, believe me).
Cheers
It's a peculiarity of Oracle PL/SQL that stored procedure parameters and function return types cannot be limited. That is, we cannot have a procedure with a signature like this:
SQL> create or replace procedure my_proc (p1 in varchar2(30))
2 is
3 begin
4 null;
5 end;
6 /
Warning: Procedure created with compilation errors.
SQL> show error
Errors for PROCEDURE MY_PROC:
LINE/COL ERROR
-------- -----------------------------------------------------------------
1/34 PLS-00103: Encountered the symbol "(" when expecting one of the
following:
:= . ) , # % default character
The symbol ":=" was substituted for "(" to continue.
SQL> create or replace procedure my_proc (p1 in varchar2)
2 is
3 begin
4 null;
5 end;
6 /
Procedure created.
SQL>
Sure we can define the procedure's parameter using a SUBTYPE but Oracle will ignore it. Same goes for function return types...
SQL> create or replace package my_subtypes as
2 subtype ltd_string is varchar2(30);
3 end;
4 /
Package created.
SQL> create or replace function my_func return my_subtypes.ltd_string
2 is
3 begin
4 return lpad('a', 4000, 'a');
5 end;
6 /
Function created.
SQL> select length(my_func) from dual
2 /
LENGTH(MY_FUNC)
---------------
4000
SQL>
The only way of limiting parameters and return types is to declare variables using subtypes within the stored procedure. Use the variables within the package, and assign them to the OUT paramters (or RETURN the variable for functions).
Which is a long-winded way of saying, you can use DBMS_OBFUSCATION_TOOLKIT.VARCHAR2_CHECKSUM in your code confident that it won't prevent your function returning 32 characters.
However, it will confuse developers who will lookup the SUBTYPE declaration. In the worst case these people will use the subtype to declare their own working variables with the following tragic result:
SQL> declare
2 v my_subtypes.ltd_string;
3 begin
4 v := my_func;
5 end;
6 /
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 4
SQL>
So, it is better not to use an inappropriate subtype. Instead declare your own.

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