Error in return of PLSQL ORACLE Not Working - sql

This Code is:
CREATE OR REPLACE
PROCEDURE Actualiza_Saldo(fecha_ini IN DATE, fecha_fin IN DATE) RETURN NUMBER
AS
fechaTemp DATE;
diasTotales NUMBER := fecha_fin- fecha_ini;diasLaborables NUMBER;
sab VARCHAR2(10) := 'SÁBADO';dom VARCHAR2(10) := 'DOMINGO';diasTemp VARCHAR2(10);
BEGIN
diasLaborables:= diastotales;
FOR i IN 0..diasTotales LOOP
fechaTemp := fecha_ini + i;
DBMS_OUTPUT.PUT_LINE(to_char(fechaTemp));
diasTemp := TO_CHAR(fechaTemp, 'DAY', 'NLS_DATE_LANGUAGE=SPANISH');
IF (TRIM(diasTemp)=sab or TRIM(diasTemp)=dom) THEN
diaslaborables := diaslaborables-1;
END IF;
END LOOP;
dbms_output.put_line(diaslaborables);
RETURN diasLaborables;
END Actualiza_Saldo;
If I execute without returning it works, if I try to return a value it fails, I do not know what could be happening.
The error of oracle is:
Warning: la ejecución ha terminado con advertencias
PROCEDURE Actualiza_Saldo(fecha_ini Compilado.
Error que empieza en la línea 1 del comando:
EXEC Actualiza_Saldo();
Informe de error:
ORA-06550: línea 1, columna 7:
PLS-00905: el objeto HR.ACTUALIZA_SALDO no es válido
ORA-06550: línea 1, columna 7:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
The code with constant value:
CREATE OR REPLACE
PROCEDURE Actualiza_Saldo(fecha_ini IN DATE DEFAULT '10/08/2018', fecha_fin IN DATE DEFAULT '30/08/2018')
AS
fechaTemp DATE;
diasTotales NUMBER := fecha_fin- fecha_ini;diasLaborables NUMBER;
sab VARCHAR2(10) := 'SÁBADO';dom VARCHAR2(10) := 'DOMINGO';diasTemp VARCHAR2(10);
BEGIN
diasLaborables:= diastotales;
FOR i IN 0..diasTotales LOOP
fechaTemp := fecha_ini + i;
DBMS_OUTPUT.PUT_LINE(to_char(fechaTemp));
diasTemp := TO_CHAR(fechaTemp, 'DAY', 'NLS_DATE_LANGUAGE=SPANISH');
IF (TRIM(diasTemp)=sab or TRIM(diasTemp)=dom) THEN
diaslaborables := diaslaborables-1;
END IF;
END LOOP;
dbms_output.put_line(diaslaborables);
END Actualiza_Saldo;
EXEC Actualiza_Saldo();
And the exit of the code without the returns and the test values ​​is a route from the start date and the final date subtracting the days Saturday and Sunday.
PROCEDURE Actualiza_Saldo(fecha_ini Compilado.
anonymous block completed
10/08/18
11/08/18
12/08/18
13/08/18
14/08/18
15/08/18
16/08/18
17/08/18
18/08/18
19/08/18
20/08/18
21/08/18
22/08/18
23/08/18
24/08/18
25/08/18
26/08/18
27/08/18
28/08/18
29/08/18
30/08/18
14
But if I try to return the value the algorithm dies.
I have no idea what I am doing wrong or where is the fault, if you could help me I would greatly appreciate it.

Procedures cannot return a value, in Oracle, You can have out-parameters which would be visible after the procedure runs.
But here is another option you can try, instead of writing code to generate dates between two, try to do it using a single select query and open a out cursor to have the values populated after the proc completes
An example as follows
create or replace procedure generate_dates(start_date in date, end_date in date, result_set out sys_refcursor)
as
begin
open result_set for
select trunc(start_date)+level as output_dates
from dual
connect by level<=trunc(end_date)-trunc(start_date);
end;
if you are using sqlplus to connect to your database
you would call the proc as follows
var x refcursor
begin
generate_dates(date '2018-01-01',date '2018-12-31',:x);
end;
print x;

My mistake was that I was not doing a function but a procedure solved.
CREATE OR REPLACE
FUNCTION HR.Actualiza_Saldo(fecha_ini IN DATE/* DEFAULT '10/08/2018'*/, fecha_fin IN DATE/* DEFAULT '30/08/2018'*/) RETURN NUMBER
AS

Related

PL/SQL Function to check whether a number is prime or not

Please find the error and rectify this code
This is a PL/SQL function to check whether a number is prime or not
By using the flag method instead of using count to check the requirement, it's working perfectly but not for this method.
create or replace function isprime(x in number)
RETURN number
IS
i int;
count int;
BEGIN
count:=0;
for i in 2..x/2 LOOP
if mod(x,i)=0 then
count:=count+1;
end if;
end loop;
return count;
end;
/
Warning: Function created with compilation errors.
DECLARE
n int;
c int;
BEGIN
n:=&n;
c:=isprime(n);
if c=0 then
dbms_output.put_line(n||'is a prime number');
else
dbms_output.put_line(n||'is not prime');
end if;
end;
/
Enter value for n: 5
old 5: n:=&n;
new 5: n:=5;
c:=isprime(n);
*
ERROR at line 6:
ORA-06550: line 6, column 4:
PLS-00905: object SYSTEM.ISPRIME is invalid
ORA-06550: line 6, column 1:
PL/SQL: Statement ignored
Here is the error list:
SQL> show errors
Errors for FUNCTION ISPRIME:
LINE/COL ERROR
-------- -----------------------------------------------------------------
10/1 PL/SQL: Statement ignored
10/8 PLS-00204: function or pseudo-column 'COUNT' may be used inside a
SQL statement only
13/1 PL/SQL: Statement ignored
13/8 PLS-00204: function or pseudo-column 'COUNT' may be used inside a
SQL statement only
COUNT is reserve sql word, have a look at this as an alternative solution for this task.
DECLARE
value_ NUMBER default :A;
i NUMBER DEFAULT 5;
is_prime VARCHAR2(5 CHAR);
BEGIN
IF value_ = 2 OR value_ = 3 THEN
is_prime := 'TRUE';
ELSIF MOD(value_,2)=0 OR MOD(value_,3)=0 OR value_ <= 1 THEN
is_prime := 'FALSE';
END IF;
WHILE POWER(i,2) <= value_ LOOP
IF MOD(value_,i)=0 OR MOD(value_,i + 2)=0 THEN
is_prime := 'FALSE';
exit;
END IF;
i := i + 6;
end loop;
is_prime := nvl(is_prime,'TRUE');
DBMS_Output.Put_Line(is_prime||' IT IS '||CASE is_prime WHEN 'FALSE' THEN 'NOT' END||'A PRIME NUMBER');
END;

How to call Stored Procedure from BI Publisher?

I am trying to call a stored procedure from BI Publisher, and need some help to achieve this.
Here is what I have:
In my Data Model I have the following code:
DECLARE
type refcursor is REF CURSOR;
xdo_cursor refcursor;
BEGIN
OPEN :xdo_cursor FOR
SELECT IPVOWN.F1(5) FROM DUAL;
COMMIT;
END;
Type of SQL = Non-standard SQL
Running this code works in SQL Developer when you remove the ":" from xdo_cursor, but in BI ":" is required. I get the following error in the log file:
<txt>java.sql.SQLException: ORA-06550: line 2, column 22:
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
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:462)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:405)
thanks in advance.
Below are the steps/documentation on how to call a Function from BI Publisher:
1) Make sure you are using SYS_REFCURSOR.
2) Create your function like below in DB:
CREATE OR REPLACE FUNCTION FUNC1 (P1 VARCHAR2) RETURN SYS_REFCURSOR IS
XDO_CURSOR SYS_REFCURSOR;
BEGIN
IF P1 = 'USA' THEN
OPEN XDO_CURSOR FOR
'SELECT TO_CHAR(SYSDATE,''MM-DD-YYYY'') AS CURRENT_DATE, X.STATE FROM schemaName.XX1 X WHERE X.ID IN (100,200,400)';
RETURN XDO_CURSOR;
ELSE
OPEN XDO_CURSOR FOR
'SELECT TO_CHAR(SYSDATE,''MM-DD-YYYY'') AS CURRENT_DATE, X.STATE FROM schemaName.XX1 X WHERE X.ID IN (300,500,600)';
RETURN XDO_CURSOR;
END IF ;
END FUNC1;
3) Log in as System and grant execute to the BI User
GRANT EXECUTE ON schemaName.FUNC1 TO BI_User;
In your Data Set in BI Publisher - You can do the following to call the function: Make sure the type of SQL is "Procedure Call"
DECLARE
type refcursor is REF CURSOR;
xdo_cursor refcursor;
BEGIN
:xdo_cursor := SchemaName.func1(:P1);
END;
For more info you can use the link below:
https://community.oracle.com/thread/888365

Invalid Identifier When Trying to Use Package Function in Summation Expression

I'm trying to use the sum function with a package function but running into an "invalid identifier" bug. Here's some example code with the error causing function commented
create or replace type numType as object
(
myNum number
)
;
/
create or replace type numTypes is table of numType;
/
create or replace package testNumberPackage as
function ReturnNum(in_numType numType) return number;
end;
/
create or replace package body testNumberPackage as
function ReturnNum(in_numType numType) return number is
begin
return in_numType.myNum;
end;
end;
/
declare l_numTypes numTypes;
l_count number;
begin
l_numTypes := numTypes();
for i in 1 .. 100 loop
l_numTypes.extend(1);
l_numTypes(l_numTypes.last) := numType(i);
end loop;
select sum(n.myNum) into l_count from table(l_numTypes) n;
select sum(testNumberPackage.ReturnNum(n)) into l_count from table(l_numTypes) n; --causes the error
dbms_output.put_line(l_count);
end;
/
The exact error for this code is
ORA-06550: line 11, column 42
PL/SQL: ORA-00904: "N": invalid identifier
ORA-6550: line 11, column 3:
PL/SQL: SQL Statement ignored
Thanks for any help.
The first issue is that you can't pass a table into a parameter by using its alias. It doesn't even make sense to try doing that.
The next issue is how to get the column mynum that is returned from the table(l_numTypes) into the correct format to pass into testNumberPackage.ReturnNum, since it's of NUMBER datatype, and the function is expecting a numtype parameter.
To do that, you need to pass in an object with that column, like so: numtype(n.mynum).
The following works for me:
declare
l_numTypes numTypes;
l_count number;
begin
l_numTypes := numTypes();
for i in 1 .. 100 loop
l_numTypes.extend(1);
l_numTypes(l_numTypes.last) := numType(i);
end loop;
select sum(n.myNum) into l_count from table(l_numTypes) n;
select sum(testNumberPackage.ReturnNum(numtype(n.mynum))) into l_count from table(l_numTypes) n; --causes the error
dbms_output.put_line(l_count);
end;
/
5050
Clear as mud?

ORA-21700: object does not exist or is marked for delete

I have package with Period and TableOfPeriod types:
TYPE Period
IS RECORD
( StartPeriod Date,
EndPeriod Date
);
TYPE TableOfPeriod
IS TABLE OF Period;
and in this package I have three simple function:
FUNCTION Temp1
RETURN TableOfPeriod IS
returnedValue TableOfPeriod := TableOfPeriod();
BEGIN
returnedValue.extend(1);
returnedValue(1).StartPeriod := sysdate-100;
returnedValue(1).EndPeriod := sysdate;
RETURN returnedValue;
END Temp1;
FUNCTION CalculateFine
return VARCHAR2
IS
freewillLockTableRP TableOfPeriod:=TableOfPeriod();
compulsoryLockTableRP TableOfPeriod:=TableOfPeriod();
BEGIN
--for testing
compulsoryLockTableRP:=Temp1();
FOR i IN compulsoryLockTableRP.FIRST..compulsoryLockTableRP.LAST LOOP
IF(((compulsoryLockTableRP(i).EndPeriod - compulsoryLockTableRP(i).StartPeriod)>1)) THEN
BEGIN
-- RAISE_APPLICATION_ERROR(-20001, 'Hello world');
SELECT T111.StartPeriod StartPeriod,
T111.EndPeriod EndPeriod
bulk collect into freewillLockTableFull
FROM TABLE(DistributeDaysByPeriods(compulsoryLockTableRP, 5)) T111;
END;
END IF;
END LOOP;
/*SELECT T111.StartPeriod StartPeriod,
T111.EndPeriod EndPeriod
-- BULK COLLECT INTO compulsoryLockTableRP
bulk collect into freewillLockTableFull
FROM TABLE(DistributeDaysByPeriods(compulsoryLockTableRP, 5)) T111;*/
--===========
--SOME OTHER PROCESSING
RETURN 'Ok '
|| '#r';
EXCEPTION
WHEN No_Data_Found THEN return 'No data found#g';
-- WHEN OTHERS THEN RETURN SQLERRM;
END CalculateFine;
When I execute this function, I have next error:
"ORA-21700: object does not exist or is marked for delete ORA-06512:
at "MyPackageName", line 1181 ORA-06512: at line 1
21700. 00000 - "object does not exist or is marked for delete""
where 1181 line is a line with Select statement of CalculateFine function. Can anybody tell me, whats wrong and how I can solve this problem?
Check if you have in the same folder where your package is located a file with the same name but with an extention *.~sql (e.g.: your_file_name.~sql). I had the same error but after I have deleted an *.~sql file I could compile my package without ORA errors.

Given Date valid in Oracle [duplicate]

This question already has answers here:
PL/SQL check date is valid
(2 answers)
Given Date valid [closed]
Closed 9 years ago.
I want to verify valid date for all the formats
I am getting this error.
My input is IDateFormat = 'MM/YY/DD' and IStringDate = '2001-01-01'
I am getting the below error.
ORA-01843: not a valid month
ORA-06512: at "NECHO_APP.CONVERTTODATE" ORA-06512: at line 10 2011-01-01 Process exited.
CREATE OR REPLACE PROCEDURE convertToDate
(IDateFormat Varchar2,
IStringDate Varchar2,
OConvertedDate out timestamp,
OError OUT Int )
AS
TDateFormat Varchar2(20);
TStringDate Varchar2(20);
BEGIN
OError := 0;
TDateFormat := LTrim(RTrim(IDateFormat));
TStringDate := LTrim(RTrim(IStringDate));
DBMS_OUTPUT.PUT_LINE(TStringDate);
IF IS_VALID(TStringDate,'yyyy-mm-dd') = 1 THEN
SELECT TO_DATE(TStringDate,TDateFormat) INTO OConvertedDate FROM DUAL;
OError := 0;
END IF;
END;
- FUNCTION:
create or replace function is_valid(p_val in varchar2, fmt varchar2)
return number
is
not_a_valid_day exception;
not_a_valid_month exception;
pragma exception_init(not_a_valid_day, -1847);
pragma exception_init(not_a_valid_month, -1843);
l_date date;
begin
l_date := to_date(p_val, fmt);
return 1;
exception
when not_a_valid_day or not_a_valid_month
then return 0;
end is_valid;
You should use this
SELECT TO_CHAR(TO_DATE(TStringDate,'YYYY-MM-DD'),TDateFormat) INTO OConvertedDate FROM DUAL;
However, you should go through the usage of to_date and to_char.