如何使用oracle中的游标将源表中的多行插入到目标表中的单行中

发布于 2024-11-10 18:53:11 字数 929 浏览 0 评论 0原文

可能的重复:
多行如何在Oracle中连接成一个而不创建存储过程?

create table pr_info(
   pr_ref varchar2(10),
   pr_text varchar2(3), 
   pr_key varchar2(12)
)

该表包含以下格式的数据

pr_ref pr_text  pr_key
a1     abc      qwertyui01 
a1     def      qwertyui02
b1     aaa      zxcvbnmj01
b1     bbb      zxcvbnmj02
b1     ccc      zxcvbnmj03

即,如果 pr_text 长度超过 3 个字符,则记录将被拆分并放置在具有相同 pr_ref 的新记录中但不同的pr_key(在这种情况下,前8个字符将保持相同,但最后两个字符将表示记录的顺序)

所以现在我需要将此表的数据放入一个新表中,该表具有以下规范

create table pv_cus(pv_ref vrachar2(10),pv_text varchar2(100))

所以基本上我需要连接源表中属于同一个人的行并将其放在目标表中的一行中。

pv_ref  pv_text    
a1      abc,def    
b1      aaa,bbb,ccc    

Possible Duplicate:
How can multiple rows be concatenated into one in Oracle without creating a stored procedure?

create table pr_info(
   pr_ref varchar2(10),
   pr_text varchar2(3), 
   pr_key varchar2(12)
)

This table contains the data in the following format

pr_ref pr_text  pr_key
a1     abc      qwertyui01 
a1     def      qwertyui02
b1     aaa      zxcvbnmj01
b1     bbb      zxcvbnmj02
b1     ccc      zxcvbnmj03

That is if the pr_text is more than 3 characters long then the record is split and placed in a new record with same pr_ref but different pr_key(in this case the first 8 characters will remain the same but the last two character will signify the sequence of the record)

So now i need to put the data of this table into a new table which has the following sprecification

create table pv_cus(pv_ref vrachar2(10),pv_text varchar2(100))

So basically i need to concatenate the rows belonging to same person from the source table and put it in one row in target table.

pv_ref  pv_text    
a1      abc,def    
b1      aaa,bbb,ccc    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霓裳挽歌倾城醉 2024-11-17 18:53:11

程序方法

DECLARE

  type pv_ref_t is TABLE of pv_cus.pv_ref%type;
  type pv_text_t is TABLE of pv_cus.pv_text%type;
  v_pv_ref_tab pv_ref_t;
  v_pv_text_tab pv_text_t;
  v_last_pr_ref  pr_info.pr_ref%type;
BEGIN
  v_pv_ref_tab := pv_ref_t();
  v_pv_text_tab := pv_text_t();

  FOR rec in (SELECT pr_ref, pr_text FROM pr_info order by  pr_ref, pr_key)
  LOOP
    IF v_last_pr_ref IS NULL
    OR v_last_pr_ref != rec.pr_ref
    THEN
      v_last_pr_ref := rec.pr_ref;
      v_pv_ref_tab.extend(1);
      v_pv_text_tab.extend(1);
      v_pv_ref_tab(v_pv_ref_tab.last) := rec.pr_ref;
      v_pv_text_tab(v_pv_text_tab.last) := rec.pr_text;
    ELSE
      -- tbd: check length of v_pv_text_tab(v_pv_text_tab.last)
      v_pv_text_tab(v_pv_text_tab.last) := v_pv_text_tab(v_pv_text_tab.last) || ',' || rec.pr_text;
    END IF;

  END LOOP;

  FORALL  i in 1..v_pv_ref_tab.last
    INSERT INTO pv_cus (pv_ref, pv_text) VALUES(v_pv_ref_tab(i), v_pv_text_tab(i))
  ;
END;
/

Procedural approach

DECLARE

  type pv_ref_t is TABLE of pv_cus.pv_ref%type;
  type pv_text_t is TABLE of pv_cus.pv_text%type;
  v_pv_ref_tab pv_ref_t;
  v_pv_text_tab pv_text_t;
  v_last_pr_ref  pr_info.pr_ref%type;
BEGIN
  v_pv_ref_tab := pv_ref_t();
  v_pv_text_tab := pv_text_t();

  FOR rec in (SELECT pr_ref, pr_text FROM pr_info order by  pr_ref, pr_key)
  LOOP
    IF v_last_pr_ref IS NULL
    OR v_last_pr_ref != rec.pr_ref
    THEN
      v_last_pr_ref := rec.pr_ref;
      v_pv_ref_tab.extend(1);
      v_pv_text_tab.extend(1);
      v_pv_ref_tab(v_pv_ref_tab.last) := rec.pr_ref;
      v_pv_text_tab(v_pv_text_tab.last) := rec.pr_text;
    ELSE
      -- tbd: check length of v_pv_text_tab(v_pv_text_tab.last)
      v_pv_text_tab(v_pv_text_tab.last) := v_pv_text_tab(v_pv_text_tab.last) || ',' || rec.pr_text;
    END IF;

  END LOOP;

  FORALL  i in 1..v_pv_ref_tab.last
    INSERT INTO pv_cus (pv_ref, pv_text) VALUES(v_pv_ref_tab(i), v_pv_text_tab(i))
  ;
END;
/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文