获取colum值中的varchar

时间:2017-02-03 09:04:16

标签: sql oracle11g nested create-table

CREATE TYPE CourseList1 AS TABLE OF VARCHAR2(64);
/

CREATE TABLE department1 
(
    name     VARCHAR2(20),
    director VARCHAR2(20),
    office   VARCHAR2(20),
    courses1  CourseList1) 
    NESTED TABLE courses1 STORE AS courses_tab1;
    /

    BEGIN
   INSERT INTO department1
      VALUES('English', 'Lynn Saunders', 'Breakstone Hall 205',
         CourseList1('Expository Writing',
                    'Film and Literature',
                    'Modern Science Fiction',
                    'Discursive Writing',
                    'Modern English Grammar',
                    'Introduction to Shakespeare',
                    'Modern Drama',
                    'The Short Story',
                    'The American Novel'));
END;

使用

检索数据时
select * from department1 

我的输出是:

英语Lynn Saunders Breakstone Hall 205 VARCHAR (说明文写作,电影与文学,现代科幻,话语写作,现代英语语法,莎士比亚简介,现代戏剧,短篇小说,美国小说)

我不知道并且困惑,为什么 VARCHAR 正在显示

1 个答案:

答案 0 :(得分:0)

您只是看到SQL Developer显示嵌套表的正常方式。虽然它显示为嵌套表列名(COURSELIST1('Expository Writing', ...))而不是VARCHAR(...),但至少在SQL Developer 4.2中。如果将查询作为语句运行,那么结果将显示在“查询结果”窗口中,它也会显示模式名称。不同的客户端可以选择以其他方式显示嵌套表。

你可以unnest your nested table

select d.name, d.director, d.office, t.column_value as course
from department1 d
cross join table(courses1) t;

NAME                 DIRECTOR             OFFICE               COURSE                                                          
-------------------- -------------------- -------------------- ----------------------------------------------------------------
English              Lynn Saunders        Breakstone Hall 205  Expository Writing                                              
English              Lynn Saunders        Breakstone Hall 205  Film and Literature                                             
English              Lynn Saunders        Breakstone Hall 205  Modern Science Fiction                                          
English              Lynn Saunders        Breakstone Hall 205  Discursive Writing                                              
English              Lynn Saunders        Breakstone Hall 205  Modern English Grammar                                          
English              Lynn Saunders        Breakstone Hall 205  Introduction to Shakespeare                                     
English              Lynn Saunders        Breakstone Hall 205  Modern Drama                                                    
English              Lynn Saunders        Breakstone Hall 205  The Short Story                                                 
English              Lynn Saunders        Breakstone Hall 205  The American Novel                                              

9 rows selected. 

如果您希望它们以逗号分隔的字符串形式显示,则可以使用listagg()生成该字符串:

select d.name, d.director, d.office,
  listagg(t.column_value, ',') within group (order by t.column_value) as courses
from department1 d
cross join table(courses1) t
group by d.name, d.director, d.office;

NAME                 DIRECTOR             OFFICE               COURSES                                                                                                                                                                             
-------------------- -------------------- -------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
English              Lynn Saunders        Breakstone Hall 205  Discursive Writing,Expository Writing,Film and Literature,Introduction to Shakespeare,Modern Drama,Modern English Grammar,Modern Science Fiction,The American Novel,The Short Story 

这取决于你想要对嵌套表数据做什么。