How to call a SQL function inside package body of another function - sql

I have a package body as give bellow. I need to call a function named TESTING() inside the function body and call another query. Code is given bellow
CREATE OR REPLACE PACKAGE BODY TEST_PCAKAGE AS -- body
FUNCTION OUTER_FUNCTION (
INPUT_A IN VARCHAR2,
INPUT_B IN DATE,
) RETURN REF_CURSOR_TYPE IS
CUR_CA_RECEIPTS REF_CURSOR_TYPE;
BEGIN
OPEN CUR_CA_RECEIPTS FOR
TESTING();
SELECT * FROM TEST_TABLE;
RETURN CUR_CA_RECEIPTS;
END OUTER_FUNCTION;
END TEST_PCAKAGE ;
when creating this package body, it gives me errors. Can anyone please tell me how to do this?
Error : Error(14,1): PLS-00428: an INTO clause is expected in this SELECT statement
create or replace
FUNCTION "TESTING" RETURN VARCHAR2
AS
BEGIN
RETURN('SUCCESS');
END;

I'm guessing you'r trying to return the cursor from the function
Try this :
CREATE OR REPLACE PACKAGE BODY TEST_PCAKAGE AS -- body
FUNCTION OUTER_FUNCTION (
INPUT_A IN VARCHAR2,
INPUT_B IN DATE,
) RETURN REF_CURSOR_TYPE IS
-- use sys_refcursor for dynamic cursors
CUR_CA_RECEIPTS sys_refcursor;
BEGIN
-- Open the cursor for a query and not a function
OPEN CUR_CA_RECEIPTS FOR SELECT * FROM TEST_TABLE;
-- i'm guessing this is for debug
TESTING();
-- you can now iterate it in a different procedure
RETURN CUR_CA_RECEIPTS;
END OUTER_FUNCTION;
END TEST_PCAKAGE ;

Related

How can I add a second private function in Package Body oracle pl/sql?

Hey guys I am having an issue where I get an error when I try to add the second private header underneath my first private header.
CREATE OR REPLACE PACKAGE TEST IS
PROCEDURE TEST2(VARIABLE1 IN NUMBER, VARIABLE2 OUT NUMBER);
END;
CREATE OR REPLACE PACK BODY TEST IS
FUNCTION PRIVATE1 (VARIABLE1 IN NUMBER)
RETURN NUMBER;
FUNCTION PRIVATE2 (VARIABLE2 IN NUMBER)
RETURN NUMBER;
PROCEDURE TEST2(VARIABLE IN NUMBER, VARIABLE OUT NUMBER)
BEGIN
......
END;
FUNCTION PRIVATE1 (VARIABLE1 IN NUMBER)
RETURN NUMBER
IS
BEGIN
........
END;
FUNCTION PRIVATE2 (VARIABLE2 IN NUMBER)
RETURN NUMBER
IS
BEGIN
.......
END;
END;
Any suggestions on how I can fix it?
FUNCTION PRIVATE2 (VARIABLE2 IN NUMBER)
RETURN NUMBER; // REMOVE semi colon from here
IS
BEGIN
.......
END;
You are probably using private function in SQL - this is not supported. In order to use function in SQL you have to define it in the package specification.
We need source code of private1 and private2 functions in order to be able to help more.
[Edit] Here is the example of the package that will fail to compile due to using private function in SQL:
create or replace package test_pkg is
end;
/
create or replace package body test_pkg is
function F1 return number is
begin
return 2;
end;
function F2 return number is
lnNumber number;
begin
select F1 -- function declared only in the package body
into lnNumber
from dual;
return lnNumber + 1;
end;
end;
/
PLS-00231: function 'F1' may not be used in SQL
PL/SQL: ORA-00904: "F1": invalid identifier
PL/SQL: SQL Statement ignored

How should I use the cursor in the Function?

I want to use the cursor in the package's function like this:
PACKAGE PKG
AS
TYPE RESULT_T
IS
TABLE OF varchar2(30);
FUNCTION GENERATEF
RETURN RESULT_T PIPELINED;
END PKG;
/
create or replace
PACKAGE BODY PKG
AS
FUNCTION GENERATEF
RETURN RESULT_T PIPELINED
IS
BEGIN
FOR TLC IN (select name from users)
LOOP
PIPE ROW(TLC.name);
END LOOP;
RETURN;
END;
END PKG;
/
SELECT * FROM TABLE(PKG.GENERATEF);
I think right now the issue is focusing on the
select name from users
because if I use
select sysdate from dual
the function works well.
If I want to extract data from other VIEW, it will bring some error like this:
Error(7,45): PL/SQL: ORA-00942: table or view does not exist".
But actually, the view exists.
I don't know where is the problem. And I'm not sure whether it's OK to use the cursor like I mentioned.

how to access an Oracle procedure's OUT parameter when calling it?

If I write a simple function doSomething, I can get its result by executing :
select doSomething() from dual;
But, if I wish to call a procedure that has an OUT cursor being passed to it (along with another int parameter), how do I call that procedure inside a query and access the result of the cursor ?
Calling it inside a query is not compulsory.. its just that I want to access the results of that procedure
You can create a procedure like
CREATE OR REPLACE PROCEDURE your_procedure(out_cursor OUT sys_refcursor)
IS
BEGIN
OPEN out_cursor FOR
SELECT employee_name
FROM employees;
END;
/
Once you create your procedure wrap the procedure in a function which returns a cursor like the following
CREATE OR REPLACE FUNCTION your_function
RETURN sys_refcursor
AS
o_param sys_refcursor;
BEGIN
o_param := NULL;
your_procedure(o_param);
RETURN o_param;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
-- raise
WHEN OTHERS
THEN
-- raise
END your_function;
/
To see the results from sql do as
select your_function from dual;
Update 1
To see result in SQL Developer
Step 1
Double click on your results in SQL Developer
[Results][1]
Step 2 Single Click on the button with dots. That will pop up the values
[Grid][2]
You can Do Something Like This
select doSomething(cursor (select int_col from your_table)) colname from dual
Hope this Help

Oracle: Possible to return nothing from PL/SQL function?

As the title says, is it possible to return nothing from a PL/SQL function?
My function is as follows, and I am getting errors when I leave out the return:
create or replace
FUNCTION DeleteAttributes
(code IN varchar2)
CURSOR c_attributes = SELECT ...
BEGIN
FOR attribute_record IN c_attributes
LOOP
...
END LOOP;
END;
Oracle PL/SQL functions must have a return. If you declare your subprogram as a procedure, then it won't give you errors when it is lacking a return (procedures can't have a return).
create or replace
PROCEDURE DeleteAttributes
(code IN varchar2)
CURSOR c_attributes = SELECT ...
BEGIN
FOR attribute_record IN c_attributes
LOOP
...
END LOOP;
END;
By definition, a function returns a value, so you must have a RETURN statement. Have a look at the syntax definition for a function in the Oracle documentation. RETURN is not an option.
I'm not even sure why you would want to do this.

PL SQL How to put many wrappers in one package

reference: How to make a wrapper to return something other than ref cursor
I've got many wrapped functions that are similar to the one below, only they return different columns of course. How can i put them all in one package. because what the examples below does is replace my wrapper package everytime.
I want to know how to remove the global declarations on top so that i can add many in the same package each with different return columns
create or replace package WrapperSample is
type TResultRow is record(
if_type codes.cd%type
,number_infected Integer);
type TResultRowList is table of TResultRow;
function GetADedIcWarningsProv
(
p_hos_id in work_entity_data.hos_id%type
,p_date in date
) return TResultRowList
pipelined;
end WrapperSample;
/
create or replace package body WrapperSample is
function GetADedIcWarningsProv
(
p_hos_id in work_entity_data.hos_id%type
,p_date in date
) return TResultRowList
pipelined is
v_refcur eOdatatypes_package.eOrefcur;
currentRow TResultRow;
begin
v_refcur := YourSchema.getADedIcWarningsProv(p_hos_id, p_date);
loop
fetch v_refcur
INTO currentRow;
exit when v_refcur%NotFound;
pipe row(currentRow);
end loop;
close v_refcur;
return;
end;
end WrapperSample;
/
you would use one record definition and one table definition per different column set.
create or replace package WrapperSample is
type R_WarningsProv is record(/*...*/);
type T_WarningsProv is table of R_WarningsProv ;
function GetADedIcWarningsProv(/*...*/) return T_WarningsProv pipelined;
type R_OtherFunction is record(/*...*/);
type T_OtherFunction is table of R_OtherFunction ;
function OtherFunction(/*...*/) return T_OtherFunction pipelined;
/* Multiple functions can use the same types as long as
they share the same column definition */
function SomeOtherFunction(/*...*/) return T_OtherFunction pipelined;
end WrapperSample;