Home » SQL & PL/SQL » SQL & PL/SQL » getting error in a simple collection method (10g)
getting error in a simple collection method [message #617984] |
Sun, 06 July 2014 06:45 |
|
harishankar_kar
Messages: 22 Registered: July 2014 Location: India
|
Junior Member |
|
|
declare
type books_table is TABLE OF books%ROWTYPE;
type empcurtype is REF CURSOR ;
nt_books_table books_table:=books_table();
begin
open empcurtype for select * from books ;
fetch empcurtype BULK COLLECT INTO nt_books_table;
close empcurtype ;
FOR i in nt_books_table.FIRST..nt_books_table.LAST loop
dbms_output.put_line(nt_books_table(i));
end loop;
end;
when i am executing this code i am getting error like "invalid use of type name or sub type name ".
Please help me on this .
|
|
|
Re: getting error in a simple collection method [message #617985 is a reply to message #617984] |
Sun, 06 July 2014 06:49 |
John Watson
Messages: 8929 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
Welcome to the forum. Please read our OraFAQ Forum Guide and please read How to use [code] tags and make your code easier to read
You need to show the complete session, so that the line number on which the error occurs is displayed. Like this:orclz> declare
2 type books_table is TABLE OF books%ROWTYPE;
3 type empcurtype is REF CURSOR ;
4 nt_books_table books_table:=books_table();
5 begin
6 open empcurtype for select * from books ;
7 fetch empcurtype BULK COLLECT INTO nt_books_table;
8 close empcurtype ;
9 FOR i in nt_books_table.FIRST..nt_books_table.LAST loop
10 dbms_output.put_line(nt_books_table(i));
11 end loop;
12 end;
13 /
type books_table is TABLE OF books%ROWTYPE;
*
ERROR at line 2:
ORA-06550: line 2, column 30:
PLS-00201: identifier 'BOOKS' must be declared And do not forget to use [code] tags.
|
|
|
Re: getting error in a simple collection method [message #617986 is a reply to message #617985] |
Sun, 06 July 2014 06:56 |
|
harishankar_kar
Messages: 22 Registered: July 2014 Location: India
|
Junior Member |
|
|
Hi ,
Kindly create the tables like BOOKS and some insert statements are here like
INSERT INTO books (isbn, category, title, num_pages, price, copyright, author1, author2)
VALUES ('72122048', 'Oracle Basics', 'Oracle8i: A Beginner''s Guide', 765, 44.99, 1999, 4, 5);
INSERT INTO books (isbn, category, title, num_pages, price, copyright, author1, author2, author3)
VALUES ('72131454', 'Oracle Basics', 'Oracle Performance Tuning 101', 404, 39.99, 2001, 6, 7, 8);
suppose 2 books are there
CREATE TABLE books (
isbn CHAR(10) PRIMARY KEY,
category VARCHAR2(20),
title VARCHAR2(100),
num_pages NUMBER,
price NUMBER,
copyright NUMBER(4),
author1 NUMBER CONSTRAINT books_author1
REFERENCES authors(id),
author2 NUMBER CONSTRAINT books_author2
REFERENCES authors(id),
author3 NUMBER CONSTRAINT books_author3
REFERENCES authors(id)
);
once the books table is created kindly execute the snippets of code as i had written earlier.The complete error is
ORA-06550: line 6, column 6:
PLS-00330: invalid use of type name or subtype name
ORA-06550: line 6, column 1:
PL/SQL: SQL Statement ignored
ORA-06550: line 7, column 7:
PLS-00330: invalid use of type name or subtype name
ORA-06550: line 7, column 1:
PL/SQL: SQL Statement ignored
ORA-06550: line 8, column 7:
PLS-00330: invalid use of type name or subtype name
ORA-06550: line 8, column 1:
PL/SQL: SQL Statement ignored
ORA-06550: line 10, column 1:
PLS-00306: wrong number or types of arguments in call to 'PUT
|
|
|
Re: getting error in a simple collection method [message #617988 is a reply to message #617986] |
Sun, 06 July 2014 07:43 |
|
Michel Cadot
Messages: 68641 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
SQL> CREATE TABLE books (
2 isbn CHAR(10) PRIMARY KEY,
3 category VARCHAR2(20),
4 title VARCHAR2(100),
5 num_pages NUMBER,
6 price NUMBER,
7 copyright NUMBER(4),
8 author1 NUMBER CONSTRAINT books_author1
9 REFERENCES authors(id),
10 author2 NUMBER CONSTRAINT books_author2
11 REFERENCES authors(id),
12 author3 NUMBER CONSTRAINT books_author3
13 REFERENCES authors(id)
14 );
REFERENCES authors(id),
*
ERROR at line 9:
ORA-00942: table or view does not exist
|
|
|
Re: getting error in a simple collection method [message #617991 is a reply to message #617984] |
Sun, 06 July 2014 07:53 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
open empcurtype for select * from books ;
is wrong since empcurtype is TYPE not cursor. You need to declare cursor of empcurtype type:
declare
type books_table is TABLE OF books%ROWTYPE;
type empcurtype is REF CURSOR ;
empcur empcurtype;
nt_books_table books_table:=books_table();
begin
open empcur for select * from books ;
fetch empcur BULK COLLECT INTO nt_books_table;
close empcur ;
FOR i in nt_books_table.FIRST..nt_books_table.LAST loop
dbms_output.put_line(nt_books_table(i));
end loop;
end;
/
But then you will get error in DBMS_OUTPUT since it can only dosplay scalar non-complex types. You need to change it to:
dbms_output.put_line(nt_books_table(i).isbn);
dbms_output.put_line(nt_books_table(i).category);
.
.
.
Also, FOR i in nt_books_table.FIRST..nt_books_table.LAST loop will fail if books is empty.
And there is no need to declare REF CURSOR type. Oracle already provides one - SYS_REFCURSOR.
SY.
|
|
|
|
|
Goto Forum:
Current Time: Fri Apr 19 08:02:06 CDT 2024
|