【问题标题】:Insert into table value for rows fetched from previous query if not exists如果不存在,则插入从先前查询中获取的行的表值
【发布时间】:2020-08-24 11:14:41
【问题描述】:

我有 2 个表,比如说 'ACCESSENTRY'PROJECTPROJECT 包含列 ROLE、ACCESSENTRY。我需要找到 ACCESSENTRY 等于 'A' 的第一行,并将这些行插入到 ACCESSENTRY 新行中并获取 'ROLE' 并给出 ACCESSENTRY ,但如果该行不存在。 所以让我们在这个查询中说:

【问题讨论】:

    标签: sql oracle sql-insert


    【解决方案1】:

    您可以将INSERT ... SELECTNOT EXISTS 一起使用:

    insert into UM_ACCESSENTRY(role, accessentry) 
    select p.role, 'test' 
    from UM_PROJECT p 
    where p.PROJECT='ProjectName'
    and not exists (
      select 1 from UM_ACCESSENTRY a
      where a.role = p.role and a.accessentry = 'test'
    )
    

    【讨论】:

      【解决方案2】:

      我想你想要not exists:

      insert into ROLEE (role, accessentry) 
          select r.role, 'ACCESS'
          from ROLEE r
          where r.ACCESSENTRY = 'A' and
                not exists (select 1 from rolee r2 where r2.role = r.role and r2.column2 = 'ACCESS');
      

      【讨论】:

        猜你喜欢
        • 2023-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-24
        • 2011-07-14
        • 2022-12-15
        • 2017-03-07
        • 1970-01-01
        相关资源
        最近更新 更多