如何在oracle中克隆具有树状结构的表中的特殊根?

时间:2016-10-04 11:57:23

标签: sql oracle plsql

我有一张与自身有一对多关系的表。现在我想克隆一个分支并将其移动到其他根目录下。我的意思是 enter image description here

成为: enter image description here

到目前为止,我已经尝试过:

INSERT INTO MY_TABLE (ID,
                  CODE,
                  NAME,
                  PARENT)
   SELECT MY_TABLE_SEC.NEXTVAL,
          E.CODE,
          E.NAME,
          NVL (NULLIF (E.PARENT, TO_NUMBER ( :OLDPARENT)),
               TO_NUMBER ( :NEWPARENT))
     FROM MY_TABLE E
    WHERE E.ID != :OLDPARENT
CONNECT BY NOCYCLE PRIOR E.ID = E.PARENT
START WITH E.ID = :OLDPARENT

但是你可能已经意识到,它只复制了第一级,并且是其他级别的两倍。 我该怎么办?

1 个答案:

答案 0 :(得分:1)

INSERT INTO MY_TABLE (ID,
                  CODE,
                  NAME,
                  PARENT)
SELECT NEW_ID, CODE, NAME, NVL(PRIOR NEW_ID, :NEWPARENT) 
FROM (
  SELECT A.*, MY_TABLE_SEC.NEXTVAL NEW_ID
  FROM 
    (
      SELECT A.* 
      FROM MY_TABLE A 
      START WITH PARENT=:OLDPARENT 
      CONNECT BY NOCYCLE PRIOR ID = PARENT
    )
) A
START WITH PARENT=:OLDPARENT 
CONNECT BY NOCYCLE PRIOR ID = PARENT

<强>被修改: 我再添加一个级别来防止错误“此处不允许序列”

AVCaptureSession