Thanks for the question, Ananth.
Asked: September 19, 2007 - 9:10 am UTC
Last updated: September 26, 2007 - 1:23 pm UTC
Version: 9.2.0.5.0
Viewed 10K+ times! This question is
You Asked
I am trying to insert rows into NESTED TABLE using simple PL/SQL block and it is writing as single row tham mulple row. Please find the setup scripts
CREATE OR REPLACE TYPE CourseList AS TABLE OF VARCHAR2(64);
/
CREATE TABLE department (
name VARCHAR2(20),
director VARCHAR2(20),
office VARCHAR2(20),
courses CourseList)
NESTED TABLE courses STORE AS courses_tab;
create table courses (courses varchar2(64));
insert into courses values ('MATHS SIMPLE');
insert into courses values ('MATHS COMPLEX');
INSERT INTO department
(name, director, office, courses)
VALUES
('English', 'Lynn Saunders', 'Breakstone Hall 205', CourseList(
'Expository Writing',
'Film and Literature',
'Modern Science Fiction',
'Discursive Writing',
'Modern English Grammar',
'Introduction to Shakespeare',
'Modern Drama',
'The Short Story',
'The American Novel'));
The above insert works fine. But below using the PL/SQL BLOCK doesn't.
declare
v_courses CourseList;
str varchar2(1000);
cursor get_courses is
select courses from courses;
begin
for x in get_courses loop
str:=str || ',''' || x.courses || '''';
end loop;
str:=substr(str,2,length(str));
dbms_output.put_line(str);
-- v_courses:=CourseList(str);
insert into department
(name, director, office, courses)
values ('Maths', 'Lynn Saunders', 'Breakstone Hall 205', CourseList(str));
commit;
end;
SQL> SELECT t1.name, t2.*
2 FROM department t1, TABLE(t1.courses) t2;
NAME COLUMN_VALUE
-------------------- ------------------------------
English Expository Writing
English Film and Literature
English Modern Science Fiction
English Discursive Writing
English Modern English Grammar
English Introduction to Shakespeare
English Modern Drama
English The Short Story
English The American Novel
English 'MATHS SIMPLE','MATHS COMPLEX'
/
I was hoping to get 2 rows inserted? Please explin why?
Thanks & Regards
Ananth
and Tom said...
why should be sort of clear.
You have a string with 'x','y' in it. We inserted it.
You have a SINGLE string.
Strings are allowed to have quotes, Strings are allowed to have commas - they are nothing "special". You just have a variable with quotes and commas in it, it is still just a string.
You wanted to do something like:
delare
l_data CourseList;
....
begin
select courses bulk collect into l_data from course;
....
insert into department
(name, director, office, courses)
values ('Maths', 'Lynn Saunders', 'Breakstone Hall 205', l_data );
end;
/see also:
http://asktom.oracle.com/Misc/varying-in-lists.html it is very much the same sort of thing.
Rating
(1 rating)
Is this answer out of date? If it is, please let us know via a Comment