【问题标题】:Oracle sql insert into multiple tables from with clause with restrictionOracle sql从带有限制的子句插入多个表
【发布时间】:2019-03-28 13:06:50
【问题描述】:

我想从一个查询/输入数据集中将数据插入到多个表中,同时使用 where 子句获取不同的数据。我正在使用 Oracle SQL Developer。

我已经尝试过以下不起作用的逻辑:

Insert into A (X, Y, Z)
Values(Select x, y, z From inputdata where x = 1)

Insert into B (X, Y, Z)
Values(Select x, y, z From inputdata where x = 2)

Insert into C (X, Y, Z)
Values(Select x, y, z From inputdata where x = 3)

With inputdata as (Select x, y, z From source)
Select x, y, z From inputdata

【问题讨论】:

    标签: sql oracle where-clause insert-into


    【解决方案1】:

    使用条件insert all 像这里:

    create table a(x, y, z) as (select 0, 0, 0 from dual);
    create table b(x, y, z) as (select 0, 0, 0 from dual);
    create table c(x, y, z) as (select 0, 0, 0 from dual);
    
    create table src(x, y, z) as (
        select 1, 1, 1 from dual union all
        select 2, 2, 2 from dual union all
        select 3, 3, 3 from dual );
    

    insert all 
      when x = 1 then into a (x, y, z) values (x, y, z)
      when x = 2 then into b (x, y, z) values (x, y, z)
      when x = 3 then into c (x, y, z) values (x, y, z)
    select * from src
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-18
      • 2011-07-27
      • 2015-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多