Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
pdtInitDate IN DATE := '01/01/1753',
CREATE OR REPLACE FUNCTION davism
(
-- =============================================
-- declare input parametes
-- =============================================
-- pRESULT_CURSOR OUT SYS_REFCURSOR,
pdtInitDate IN DATE := to_date('01/01/1753','mm/dd/yyyy'),
pvcValue1 IN VARCHAR2 default null
)
RETURN NUMBER
AS
hold_date date;
begin
hold_date := pdtInitDate;
IF pdtInitDate = to_date('01/01/1753','mm/dd/yyyy') THEN
hold_date := SYSDATE;
END IF;
return to_char(hold_date,'yyyy');
end;
/
Function created.
select davism(sysdate-2000,'hello') davism from dual;
DAVISM
----------
2003
select davism from dual;
DAVISM
----------
2008
CREATE OR REPLACE FUNCTION mySchema.Myfunction
(
-- =============================================
-- declare input parametes
-- =============================================
pRESULT_CURSOR OUT SYS_REFCURSOR,
pdtInitDate IN DATE := '01/01/1753',
pvcValue1 IN VARCHAR2 := ''
)
RETURN NUMBER
AS
RETURN <whatever> IS
You can "use" pdtInitDate all you want, so long as the "use" is read-only. That limitation exists because you declared pdtInitDate as an IN-usage expression (which = "READ ONLY"). If you declare pdtInitDate as IN OUT (and whatever you you passed into the function as the expression for pdtInitDate from the invoking environment is a defined memory variable elsewhere, then, yes you can assign a value from within the function since now pdtInitDate is not only readable but also writable.Davism said:Are you saying that I have to declar and use a variable rather than using what is there? (i.e. hold_date instead of pdtInitDate)
Your problem is symptomatic of incorrectly specifying (and OPENing) your sys_refcursor. A working model of the syntax to use to populate a sys_refcursor (which you can then return to the invoking environment) appears in my post timestamped, "21 Nov 08 20:29", above. The "brains" of accessing a sys_refcursor lie in the code:Davism said:At the end of my processing in the function I am trying to do a SELECT with an order by thinking that it was going to go into the refcursor...I get an error message on the compilation that says "an INTO clause is expected in this SELECT statement."
open <cursor_name> for <some SELECT statement>; -- Notice there is no "INTO" clause in the OPEN statement
...
loop
fetch <cursor name> into <record description>; -- [B][I]Here[/I][/B] is the "INTO" clause
exit when <cursor name>%notfound;
<do some processing here>
end loop;
...
end;
/
No difference, except spelling. They have the same effect, syntactically.Davism said:What is the difference with the "AS" and the "IS".
Notice in my earlier example (from timestamp "21 Nov 08 20:29") that my function returns a sys_refcursor.Davism said:I really want the refcursor (which basically contains the information from the final SELECT that I do.)
OPEN pRESULT_CURSOR FOR
set serveroutput on
declare
foo sys_refcursor;
y s_emp%rowtype;
begin
foo := open_cursor('select * from s_emp order by last_name, first_name');
loop
fetch foo into y;
exit when foo%notfound;
dbms_output.put_line(y.last_name||', '||y.first_name);
end loop;
end;
/
Absolutely!Davism said:Would I be able to test with a revision of the same you did...?
...SELECT <whatever>
FROM <wherever>
WHERE rownum <= 3...
set serveroutput on
declare
foo sys_refcursor;
y gblTempTable%rowtype;
begin
foo := MySchema.MyFunction();
loop
fetch foo into y;
exit when foo%notfound;
dbms_output.put_line(y.GBL_LIST||', '||y.GBL_OUT);
end loop;
end;
/
CREATE OR REPLACE FUNCTION mySchema.Myfunction
(
-- =============================================
-- declare input parametes
-- =============================================
pRESULT_CURSOR OUT SYS_REFCURSOR,
pdtInitDate IN DATE := to_date('01/01/1753','mm/dd/yyyy'),
pvcValue1 IN VARCHAR2 default null
)
RETURN NUMBER
AS
OPEN pRESULT_CURSOR FOR
SELECT GBL_LIST, GBL_OUT from gblTempTable
ORDER BY GBL_LIST;
RETURN 0;
foo := MySchema.MyFunction;
set serveroutput on
declare
foo sys_refcursor;
y gblTempTable%rowtype;
begin
foo := MySchema.MyFunction();
loop
fetch foo into y;
exit when foo%notfound;
dbms_output.put_line(y.GBL_LIST||', '||y.GBL_OUT);
end loop;
end;
/
select * from gblTempTable;
GBL_LIST GBL_OUT
---------- ----------
abc def
ghi jkl
mno pqr
3 rows selected.
CREATE OR REPLACE FUNCTION Myfunction
(
-- =============================================
-- declare input parametes
-- =============================================
pRESULT_CURSOR OUT SYS_REFCURSOR,
pdtInitDate IN DATE := to_date('01/01/1753','mm/dd/yyyy'),
pvcValue1 IN VARCHAR2 default null
)
RETURN NUMBER
AS
begin
OPEN pRESULT_CURSOR FOR
SELECT GBL_LIST, GBL_OUT from gblTempTable
ORDER BY GBL_LIST;
RETURN 0;
end;
/
Function created.
set serveroutput on
declare
foo sys_refcursor;
y gblTempTable%rowtype;
some_number number;
begin
some_number := MyFunction(foo);
loop
fetch foo into y;
exit when foo%notfound;
dbms_output.put_line(y.GBL_LIST||', '||y.GBL_OUT);
end loop;
end;
/
abc, def
ghi, jkl
mno, pqr
PL/SQL procedure successfully completed.
foo := MyFunction(foo);
*
ERROR at line <n>:
ORA-06550: line <n>, column 12:
PLS-00382: expression is of wrong type
set serveroutput on
declare
foo sys_refcursor;
y MySchema.gblTempTable%rowtype;
some_number number;
begin
some_number := MySchema.HARSP_MyFunction(foo);
loop
fetch foo into y;
exit when foo%notfound;
dbms_output.put_line(y.GBL_LIST||', '||y.GBL_OUT);
end loop;
end;
/
SQL> @test.sql
declare
*
ERROR at line 1:
ORA-01001: invalid cursor
ORA-06512: at line 8