【问题标题】:Duplicate rows in a table based on foregin key in the same table - oracle sql / plsql基于同一表中外键的表中的重复行 - oracle sql / plsql
【发布时间】:2022-11-16 22:05:03
【问题描述】:

我有这个表包含主键和外键作为对它的引用 如图所示

表名 = ACV_FOLDERS_INFO

列 = ID --------------------------PK

所有者
姓名
ALT_NAME
类别
描述
图标
已删除
FDR_PUBLIC
INHERIT_PARENT_ACL
评论
FIO_ID ---------------------- FK 引用 PK(当 FIO_ID 为 null 时,表示该记录是主文件夹)

OTHER_ID
年
参考文献

像这样构建表以创建一个 TREE

我正在尝试根据 ID 引用的 FIO_ID 复制整行(文件夹)及其子文件夹,但我仍然遇到问题

/*** lasted code .

declare
  l_ID number;
begin
  for i in (select case
                     when connect_by_isleaf = 1 then
                      0
                     when level = 1 then
                      1
                     else
                      -1
                   end as status,
                   level,
                   
                   f.*
            
              from acv_folders_info f,
                   (SELECT *
                      FROM acv_folder_acl
                     WHERE privilege = 'B'
                       and ERD_ID = 483) a
             where F.ID = A.FIO_ID
               and acv_get_validation.have_upd(p_erd_id => 483,
                                               p_fio_id => a.FIO_ID) = 'Y'
               and A.ERD_ID = 483
               and year = 2020
             start with F."FIO_ID" is null
            connect by prior f."ID" = F."FIO_ID"
             order by status desc, level) loop
    begin
      insert into acv_folder_info x
        (x.ID,
         x.OWNER,
         x.NAME,
         x.ALT_NAME,
         x.CATEGORY,
         x.DESCRIPTION,
         x.ICON,
         x.DELETED,
         x.FDR_PUBLIC,
         x.INHERIT_PARENT_ACL,
         x.REMARKS,
         x.FIO_ID,
         x.OTHER_ID,
         x.YEAR,)
      values
        (null,
         483,
         i.name || ' - 2020 / 2021',
         i.alt_name || ' - 2020 / 2021',
         i.category,
         i.description,
         i.icon,
         i.deleted,
         i.fdr_public,
         i.inherit_parent_acl,
         i.remarks,
         nvl(i.ID, l_ID),
         i.other_id,
         2020);
      returning id
        into l_ID;
    
    exception
      when others then
        dbms_output.put_line(i.id || ' - ' || l_ID || sqlerrm);
    end loop;
  end;

【问题讨论】:

  • “我还是有问题” 但是,你还没有告诉我们问题是什么。
  • 您似乎缺少 END;(在 END LOOP; 之前)来终止循环内的 PL/SQL 块。 INSERT 的列列表中多了一个逗号。
  • 我无法获得新 ID 以将其作为 FK 外键的参考 您可以查看图像以查看数据排列
  • 您总是将 null 插入到 id 列中,然后将该 null 值返回到 l_id pl/sql 变量中。目前还不清楚您如何期望该值是 null 以外的任何值。
  • 男人来吧,(ID 为空,但它在触发器 BIS/BIR 上工作,如果它为空,它将调用一个 sequence.nextval)-循环是基于主文件夹到子文件夹到子文件夹到子文件夹排序我得到主文件夹 id 并插入到下一级的外部并获取下一级 id 并插入到 fio_id

标签: sql oracle plsql


【解决方案1】:

您没有提供表格的最小示例或任何可以复制/粘贴的示例数据,并且您定义了从未共享源代码的函数和触发器,因此无法测试您的代码。

但是,您想在游标中使用 ORDER SIBLINGS BY ...,以便按深度优先搜索顺序对其求值,并且您可以将生成的 ID 存储在关联数组中。

类似的东西(未经测试,因为您的问题缺少太多依赖项):

declare
  l_ID acv_folder_info.id%TYPE;
  TYPE ids_assoc_array IS TABLE OF NUMBER INDEX BY PLS_INTEGER;
  l_ids ids_assoc_array;
begin
  for i in (
    select f.*
    from   acv_folders_info f
           INNER JOIN acv_folder_acl a
           ON F.ID = A.FIO_ID
    WHERE  privilege = 'B'
    and    ERD_ID = 483
    and    acv_get_validation.have_upd(
             p_erd_id => 483,
             p_fio_id => a.FIO_ID
           ) = 'Y'
    and    A.ERD_ID = 483
    and    year = 2020
    start with F."FIO_ID" is null
    connect by prior f."ID" = F."FIO_ID"
    order siblings by name
  )
  loop
    insert into acv_folder_info (
      ID,
      OWNER,
      NAME,
      ALT_NAME,
      CATEGORY,
      DESCRIPTION,
      ICON,
      DELETED,
      FDR_PUBLIC,
      INHERIT_PARENT_ACL,
      REMARKS,
      FIO_ID,
      OTHER_ID,
      YEAR
    ) values (
      null,
      483,
      i.name || ' - 2020 / 2021',
      i.alt_name || ' - 2020 / 2021',
      i.category,
      i.description,
      i.icon,
      i.deleted,
      i.fdr_public,
      i.inherit_parent_acl,
      i.remarks,
      l_ids(i.FIO_ID),
      i.other_id,
      2020
    ) RETURNING id INTO l_ID;

    l_ids(i.id) := l_id;
  end loop;
end;
/

【讨论】:

    猜你喜欢
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2014-01-20
    相关资源
    最近更新 更多