我想使用Procedures(Pl / sql)将列从一个表转换为另一个表的行

时间:2016-09-10 23:34:26

标签: plsql

从表A

| id |col1 | col2  | col3|
--------------------------
| 1  |  a  |  x1   | y1  |
--------------------------
| 2  |  b  |  x2   |  y2 |
--------------------------
| 3  |  c  |   x3  |  y3 |
--------------------------

至表B

| id |x_a | y_a  | x_b | y_b | x_c | y_c |
------------------------------------------
| 1  | x1 |  y1  | x2  | y2  | x3  | y3  |
------------------------------------------

感谢

1 个答案:

答案 0 :(得分:0)

您的PLSQL代码如下:

declare

var varchar2(4000):= NULL;
var1 varchar2(4000):= NULL;

begin

for i in (select * from tabA where id = 1)
loop

  dbms_output.put_line('| id |x_a | y_a  | x_b | y_b | x_c | y_c |');
  dbms_output.put_line('-------------------------------------------');

  for j in (select col2,col3 from tabA)
    loop

        var1:= j.col2 ||'  |  '|| j.col3||' | ';
        var:= var ||var1; 

    end loop;

  dbms_output.put_line('|'|| i.id ||' | '|| var);

  var:=NULL;
end loop;


end;

,输出为:

| id |x_a | y_a  | x_b | y_b | x_c | y_c |
-------------------------------------------
|1 | x1  |  y1 | x2  |  y2 | x3  |  y3 |