dbTalk Databases Forums  

SHow a cursor

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss SHow a cursor in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Tim Slattery
 
Posts: n/a

Default SHow a cursor - 02-06-2012 , 02:09 PM






I've got a PL/SQL function that returns a SYS_REFCURSOR. I'm using SQL
Developer, and trying to call it and display what it's returned. And I
don't have a clue how to do it. Help!

--
Tim Slattery
Slattery_T (AT) bls (DOT) gov
http://members.cox.net/slatteryt

Reply With Quote
  #2  
Old   
ddf
 
Posts: n/a

Default Re: SHow a cursor - 02-06-2012 , 04:41 PM






On Feb 6, 1:09*pm, Tim Slattery <Slatter... (AT) bls (DOT) gov> wrote:
Quote:
I've got a PL/SQL function that returns a SYS_REFCURSOR. I'm using SQL
Developer, and trying to call it and display what it's returned. And I
don't have a clue how to do it. Help!

--
Tim Slattery
Slatter... (AT) bls (DOT) govhttp://members.cox.net/slatteryt
You fetch from the refcursorr select the function from dual:

SQL> create or replace function get_tab_data(tabname in varchar2)
2 return sys_refcursor as
3
4 mycur sys_refcursor;
5
6 sqlstr varchar2(4000);
7
8 begin
9 sqlstr := 'select * from '||tabname;
10
11 open mycur for sqlstr;
12
13 return mycur;
14
15 end;
16 /

Function created.

SQL>
SQL> show errors function get_tab_data
No errors.
SQL>
SQL> select get_tab_data('EMP') from dual;

GET_TAB_DATA('EMP')
--------------------
CURSOR STATEMENT : 1

CURSOR STATEMENT : 1

EMPNO ENAME JOB MGR HIREDATE SAL
COMM DEPTNO
---------- ---------- --------- ---------- --------- ----------
---------- ----------
7369 SMITH CLERK 7902 17-DEC-80
800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600
300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250
500 30
7566 JONES MANAGER 7839 02-APR-81
2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250
1400 30
7698 BLAKE MANAGER 7839 01-MAY-81
2850 30
7782 CLARK MANAGER 7839 09-JUN-81
2450 10
7788 SCOTT ANALYST 7566 09-DEC-82
3000 20
7839 KING PRESIDENT 17-NOV-81
5000 10
7844 TURNER SALESMAN 7698 08-SEP-81
1500 0 30
7876 ADAMS CLERK 7788 12-JAN-83
1100 20

EMPNO ENAME JOB MGR HIREDATE SAL
COMM DEPTNO
---------- ---------- --------- ---------- --------- ----------
---------- ----------
7900 JAMES CLERK 7698 03-DEC-81
950 30
7902 FORD ANALYST 7566 03-DEC-81
3000 20
7934 MILLER CLERK 7782 23-JAN-82
1300 10

14 rows selected.


SQL>



David Fitzjarrell

Reply With Quote
  #3  
Old   
ddf
 
Posts: n/a

Default Re: SHow a cursor - 02-06-2012 , 04:43 PM



On Feb 6, 1:09*pm, Tim Slattery <Slatter... (AT) bls (DOT) gov> wrote:
Quote:
I've got a PL/SQL function that returns a SYS_REFCURSOR. I'm using SQL
Developer, and trying to call it and display what it's returned. And I
don't have a clue how to do it. Help!

--
Tim Slattery
Slatter... (AT) bls (DOT) govhttp://members.cox.net/slatteryt
Answered in another group but I'll also post the example here:

SQL> create or replace function get_tab_data(tabname in varchar2)
2 return sys_refcursor as
3
4 mycur sys_refcursor;
5
6 sqlstr varchar2(4000);
7
8 begin
9 sqlstr := 'select * from '||tabname;
10
11 open mycur for sqlstr;
12
13 return mycur;
14
15 end;
16 /

Function created.

SQL>
SQL> show errors function get_tab_data
No errors.
SQL>
SQL> select get_tab_data('EMP') from dual;

GET_TAB_DATA('EMP')
--------------------
CURSOR STATEMENT : 1

CURSOR STATEMENT : 1

EMPNO ENAME JOB MGR HIREDATE SAL
COMM DEPTNO
---------- ---------- --------- ---------- --------- ----------
---------- ----------
7369 SMITH CLERK 7902 17-DEC-80
800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600
300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250
500 30
7566 JONES MANAGER 7839 02-APR-81
2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250
1400 30
7698 BLAKE MANAGER 7839 01-MAY-81
2850 30
7782 CLARK MANAGER 7839 09-JUN-81
2450 10
7788 SCOTT ANALYST 7566 09-DEC-82
3000 20
7839 KING PRESIDENT 17-NOV-81
5000 10
7844 TURNER SALESMAN 7698 08-SEP-81
1500 0 30
7876 ADAMS CLERK 7788 12-JAN-83
1100 20

EMPNO ENAME JOB MGR HIREDATE SAL
COMM DEPTNO
---------- ---------- --------- ---------- --------- ----------
---------- ----------
7900 JAMES CLERK 7698 03-DEC-81
950 30
7902 FORD ANALYST 7566 03-DEC-81
3000 20
7934 MILLER CLERK 7782 23-JAN-82
1300 10

14 rows selected.


SQL>



David Fitzjarrell

Reply With Quote
  #4  
Old   
Tim Slattery
 
Posts: n/a

Default Re: SHow a cursor - 02-07-2012 , 09:43 AM



ddf <oratune (AT) msn (DOT) com> wrote:


Quote:
You fetch from the refcursorr select the function from dual:

SQL> select get_tab_data('EMP') from dual;
And that's the magic word I had forgotten! Thank you.

Now...

I have a very simple function:

create or replace FUNCTION IHELP_GETSTATUS RETURN SYS_REFCURSOR AS
cv_1 SYS_REFCURSOR;
stmt VARCHAR2(700);
BEGIN
stmt := 'select * from ihelp.ihelp_survey_mapping';
open cv_1 for stmt;
RETURN cv_1;
END IHELP_GETSTATUS;


The "select" statement works when I run it from the SQL developer
console. But when I call the function I'm told "table or view does not
exist". But it *does* exist.

For the record, I'm trying to be able to construct a SQL select
statement dynamically. I figure if I can get this simple example to
work, then I'm home free.

--
Tim Slattery
Slattery_T (AT) bls (DOT) gov

Reply With Quote
  #5  
Old   
Tim Slattery
 
Posts: n/a

Default Re: SHow a cursor - 02-07-2012 , 11:20 AM



Tim Slattery <Slattery_T (AT) bls (DOT) gov> wrote:


Quote:
The "select" statement works when I run it from the SQL developer
console. But when I call the function I'm told "table or view does not
exist". But it *does* exist.
I saw a web post that may have the answer. He says that a common cause
of this is that select permission on the table has been granted to a
role, and that role has then been assigned (granted? not sure of the
proper terminology) to me. But that role does not propagate to
functions that I write. The result is just what I'm seeing: I can
select from the table directly, but the function can't find it.

Seems an irrational way for things to work, but then I'm only a
developer...

--
Tim Slattery
Slattery_T (AT) bls (DOT) gov

Reply With Quote
  #6  
Old   
ddf
 
Posts: n/a

Default Re: SHow a cursor - 02-08-2012 , 09:47 AM



On Feb 7, 10:20*am, Tim Slattery <Slatter... (AT) bls (DOT) gov> wrote:
Quote:
Tim Slattery <Slatter... (AT) bls (DOT) gov> wrote:
The "select" statement works when I run it from the SQL developer
console. But when I call the function I'm told "table or view does not
exist". But it *does* exist.

I saw a web post that may have the answer. He says that a common cause
of this is that select permission on the table has been granted to a
role, and that role has then been assigned (granted? not sure of the
proper terminology) to me. But that role does not propagate to
functions that I write. The result is just what I'm seeing: I can
select from the table directly, but the function can't find it.

Seems an irrational way for things to work, but then I'm only a
developer...

--
Tim Slattery
Slatter... (AT) bls (DOT) gov
Sounds like a post of mine from a while back. You will need a direct
grant on the object to create a function or procedure against it, but
you can also compile it authid current_user and those who do not have
direct grants on that table can still use the function as that
construct will traverse roles for object permissions (again, it won't
work for the author of the code). A simple 'grant select on ihelp
to ....' from the table owner will get you working.


David Fitzjarrell

Reply With Quote
  #7  
Old   
Gerard H. Pille
 
Posts: n/a

Default Re: SHow a cursor - 02-08-2012 , 12:39 PM



Tim Slattery wrote:
Quote:
Tim Slattery<Slattery_T (AT) bls (DOT) gov> wrote:


The "select" statement works when I run it from the SQL developer
console. But when I call the function I'm told "table or view does not
exist". But it *does* exist.

I saw a web post that may have the answer. He says that a common cause
of this is that select permission on the table has been granted to a
role, and that role has then been assigned (granted? not sure of the
proper terminology) to me. But that role does not propagate to
functions that I write. The result is just what I'm seeing: I can
select from the table directly, but the function can't find it.

Seems an irrational way for things to work, but then I'm only a
developer...

If you'd like to become a good developer, don't use dynamic SQL.

Reply With Quote
  #8  
Old   
Peter Schneider
 
Posts: n/a

Default Re: SHow a cursor - 02-08-2012 , 02:21 PM



Am 08.02.2012 19:39, schrieb Gerard H. Pille:
Quote:
Tim Slattery wrote:
Tim Slattery<Slattery_T (AT) bls (DOT) gov> wrote:


The "select" statement works when I run it from the SQL developer
console. But when I call the function I'm told "table or view does not
exist". But it *does* exist.

I saw a web post that may have the answer. He says that a common cause
of this is that select permission on the table has been granted to a
role, and that role has then been assigned (granted? not sure of the
proper terminology) to me. But that role does not propagate to
functions that I write. The result is just what I'm seeing: I can
select from the table directly, but the function can't find it.

Seems an irrational way for things to work, but then I'm only a
developer...


If you'd like to become a good developer, don't use dynamic SQL.
If you'd like to become a superb developer, learn how to use dynamic SQL at
great depth, know what bind variables are good for, learn what excessive
parsing means to your application performance...

And then you learn when dynamic SQL is appropriate and when not.

DBMS_SQL, EXECUTE IMMEDIATE and OPEN ... FOR '...' can do magic in the hand of
PL/SQL wizards ;-)

Regards
Peter

--
The only way to keep your health is to eat what you don't want, drink what
you don't like, and do what you'd rather not. -- Mark Twain

Reply With Quote
  #9  
Old   
Gerard H. Pille
 
Posts: n/a

Default Re: SHow a cursor - 02-08-2012 , 02:37 PM



Peter Schneider wrote:
Quote:
Am 08.02.2012 19:39, schrieb Gerard H. Pille:
Tim Slattery wrote:
Tim Slattery<Slattery_T (AT) bls (DOT) gov> wrote:


The "select" statement works when I run it from the SQL developer
console. But when I call the function I'm told "table or view does not
exist". But it *does* exist.

I saw a web post that may have the answer. He says that a common cause
of this is that select permission on the table has been granted to a
role, and that role has then been assigned (granted? not sure of the
proper terminology) to me. But that role does not propagate to
functions that I write. The result is just what I'm seeing: I can
select from the table directly, but the function can't find it.

Seems an irrational way for things to work, but then I'm only a
developer...


If you'd like to become a good developer, don't use dynamic SQL.

If you'd like to become a superb developer, learn how to use dynamic SQL at great depth, know
what bind variables are good for, learn what excessive parsing means to your application
performance...

And then you learn when dynamic SQL is appropriate and when not.

DBMS_SQL, EXECUTE IMMEDIATE and OPEN ... FOR '...' can do magic in the hand of PL/SQL wizards ;-)

Regards
Peter

And bring a production database to its knees.

Reply With Quote
  #10  
Old   
joel garry
 
Posts: n/a

Default Re: SHow a cursor - 02-08-2012 , 05:49 PM



On Feb 8, 12:37*pm, "Gerard H. Pille" <g... (AT) skynet (DOT) be> wrote:
Quote:
Peter Schneider wrote:
Am 08.02.2012 19:39, schrieb Gerard H. Pille:
Tim Slattery wrote:
Tim Slattery<Slatter... (AT) bls (DOT) gov> wrote:

The "select" statement works when I run it from the SQL developer
console. But when I call the function I'm told "table or view does not
exist". But it *does* exist.

I saw a web post that may have the answer. He says that a common cause
of this is that select permission on the table has been granted to a
role, and that role has then been assigned (granted? not sure of the
proper terminology) to me. But that role does not propagate to
functions that I write. The result is just what I'm seeing: I can
select from the table directly, but the function can't find it.

Seems an irrational way for things to work, but then I'm only a
developer...

If you'd like to become a good developer, don't use dynamic SQL.

If you'd like to become a superb developer, learn how to use dynamic SQL at great depth, know
what bind variables are good for, learn what excessive parsing means toyour application
performance...

And then you learn when dynamic SQL is appropriate and when not.

DBMS_SQL, EXECUTE IMMEDIATE and OPEN ... FOR '...' can do magic in the hand of PL/SQL wizards ;-)

Regards
Peter

And bring a production database to its knees.
Good is relative. When the choice is, solve a correlated update
ORA-1779 by adding a primary key constraint with all the testing and
design changes that implies, versus a one time script of 10K update
statements generated by a korn shell script, "on yer knees, beeyatch,
gettin' mah moneys worth tonight."

jg
--
@home.com is bogus.
http://www.utsandiego.com/news/2012/...llite-builder/

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.