将列数据转换为行数据在PL/SQL中

huangapple go评论73阅读模式
英文:

Converting column data into row in pl/sql

问题

嗨,当我去面试的时候,他们问了我这个问题。

创建表格 course(Name CHAR(10));

插入到 course 中的值为 ('Java');
插入到 course 中的值为 ('Oracle');
插入到 course 中的值为 ('Python');
插入到 course 中的值为 ('C');
插入到 course 中的值为 ('C++');

输出结果:
Java Oracle Python C C++

提前感谢,
Sandhya。

英文:

Hi When i went for an interview they asked me this question.

    Create table course(Name CHAR(10));

    insert into course values ('Java');
    insert into course values ('Oracle');
    insert into course values ('Python');
    insert into course values ('C');
    insert into course values ('C++');

     o/p:
        Java Oracle python c c++

Thanks in advance,
Sandhya.

答案1

得分: 0

你可以使用聚合函数 - LISTAGG 如下所示:

SQL> SELECT
       LISTAGG(TRIM(NAME), ' ') WITHIN GROUP(
          ORDER BY
              NULL
      ) AS RESULT
  FROM
      COURSE;

结果

C C++ Java Oracle Python

SQL>

英文:

You can use an aggregate function - LISTAGG as following:

> SQL> SELECT
> 2 LISTAGG(TRIM(NAME), ' ') WITHIN GROUP(
> 3 ORDER BY
> 4 NULL
> 5 ) AS RESULT
> 6 FROM
> 7 COURSE;
>
> RESULT
> --------------------------------------------------------------------------------
> C C++ Java Oracle Python
>
> SQL>

Cheers!!

答案2

得分: 0

我认为NAME列的数据类型应该是VARCHAR2,而不是CHAR

无论如何,另一个选项(类似于Tejash的LISTAGG),它使用XMLAGG,如果结果超过4000个字符,它更安全。

SQL> SELECT RTRIM (
  2            XMLAGG (XMLELEMENT (e, name || ' ') ORDER BY null).EXTRACT (
  3               '//text()'),
  4            ',')
  5            result
  6  FROM course;
  
RESULT
------------------------------------------------------------
Java       Oracle     Python     C          C++
  
SQL>;

或者,正如你在问题中标记了PL/SQL标签,那么一个匿名的PL/SQL块可能如下所示:

SQL> set serveroutput on
SQL>;
SQL> declare
  2    l_result varchar2(100);
  3  begin
  4    for cur_r in (select name from course) loop
  5      l_result := l_result || ' ' || cur_r.name;
  6    end loop;
  7
  8    dbms_output.put_line(trim(l_result));
  9  end;
  10  /

Java       Oracle     Python     C          C++

PL/SQL procedure successfully completed.

SQL>;

或者 - 类似地 - 一个函数:

SQL> create or replace function f_course
  2    return varchar2
  3  is
  4    l_result varchar2(100);
  5  begin
  6    for cur_r in (select name from course) loop
  7      l_result := l_result || ' ' || cur_r.name;
  8    end loop;
  9
  10    return trim(l_result);
  11  end;
  12  /

Function created.

SQL> select f_course from dual;

F_COURSE
--------------------------------------------------------------
Java       Oracle     Python     C          C++

SQL>;

或者,一个带有OUT参数的过程:

SQL> create or replace procedure p_course (par_result out varchar2)
  2  is
  3    l_result varchar2(100);
  4  begin
  5    for cur_r in (select name from course) loop
  6      l_result := l_result || ' ' || cur_r.name;
  7    end loop;
  8
  9    par_result := trim(l_result);
  10  end;
  11  /

Procedure created.

SQL> declare
  2    l_out varchar2(100);
  3  begin
  4    p_course(l_out);
  5    dbms_output.put_line(l_out);
  6  end;
  7  /

Java       Oracle     Python     C          C++

PL/SQL procedure successfully completed.

SQL>;

如你所见,有很多选项;选择最适合你需求的一个。

英文:

I presume that NAME column's datatype should have been VARCHAR2, not CHAR.

Anyway, another option (similar to Tejash's LISTAGG) which uses XMLAGG and is safer if the result is larger than 4000 characters.

SQL> SELECT RTRIM (
  2            XMLAGG (XMLELEMENT (e, name || ' ') ORDER BY null).EXTRACT (
  3               '//text()'),
  4            ',')
  5            result
  6  FROM course;

RESULT
------------------------------------------------------------
Java       Oracle     Python     C          C++

SQL>

Or, as you tagged the question with PL/SQL tag, then an anonymous PL/SQL block might look like this:

SQL> set serveroutput on
SQL>
SQL> declare
  2    l_result varchar2(100);
  3  begin
  4    for cur_r in (select name from course) loop
  5      l_result := l_result ||' '|| cur_r.name;
  6    end loop;
  7
  8    dbms_output.put_line(trim(l_result));
  9  end;
 10  /
Java       Oracle     Python     C          C++

PL/SQL procedure successfully completed.

SQL>

Or - similarly - a function:

SQL> create or replace function f_course
  2    return varchar2
  3  is
  4    l_result varchar2(100);
  5  begin
  6    for cur_r in (select name from course) loop
  7      l_result := l_result ||' '|| cur_r.name;
  8    end loop;
  9
 10    return trim(l_result);
 11  end;
 12  /

Function created.

SQL> select f_course from dual;

F_COURSE
--------------------------------------------------------------
Java       Oracle     Python     C          C++

SQL>

Or, a procedure with an OUT parameter:

SQL> create or replace procedure p_course (par_result out varchar2)
  2  is
  3    l_result varchar2(100);
  4  begin
  5    for cur_r in (select name from course) loop
  6      l_result := l_result ||' '|| cur_r.name;
  7    end loop;
  8
  9    par_result := trim(l_result);
 10  end;
 11  /

Procedure created.

SQL> declare
  2    l_out varchar2(100);
  3  begin
  4    p_course(l_out);
  5    dbms_output.put_line(l_out);
  6  end;
  7  /
Java       Oracle     Python     C          C++

PL/SQL procedure successfully completed.

SQL>

As you can see, quite a few options; use the one that most suits your needs.

huangapple
  • 本文由 发表于 2020年1月6日 14:44:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59607761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定