【问题标题】:Load two related tables in an Oracle database在 Oracle 数据库中加载两个相关的表
【发布时间】:2009-06-03 19:31:54
【问题描述】:

我见过许多类似的问题和答案,但他们使用了其他特定于 DB 的技巧,或者在代码中完成,等等。我正在寻找一个直接的 SQL 批处理文件解决方案(如果它存在的话)。

我有两个具有父/子关系的表,称它们为 Runs & Run_Values。 Runs 有一个“自动”生成的 PK、runID(一个序列和一个触发器)和两个列,Model_Type 和 Time,它们也唯一地标识行(通过约束强制执行)。对于 Run 中的每一行,Run_Values 中有许多条目,其中有两列,RunId 和 Value。

我想从生成数据的过程中生成类似的东西(这是我知道存在的 SQL 和我想要的 SQL 的混合):

insert into Runs (Model_Type, Time) values ('model1', to_date('01-01-2009 14:47:00', 'MM-DD-YYYY HH24:MI:SS'));
set someVariable = SELECT runId FROM Runs WHERE Model_Type like 'model1' and Time =  to_date('01-01-2009 14:47:00', 'MM-DD-YYYY HH24:MI:SS'));
insert into Run_Values (run_id, Value) values (someVariable, 1.0);
etc - lots more insert statements with Values for this model run.

我该如何解决这个问题?

【问题讨论】:

    标签: sql oracle insert


    【解决方案1】:

    诀窍是使用序列的 CURRVAL。即使它是在数据库触发器中设置的,您也可以直接使用它。

    一个例子:

    SQL> create table runs
      2  ( runid      number
      3  , model_type varchar2(6)
      4  , time       date
      5  )
      6  /
    
    Table created.
    
    SQL> create sequence run_seq start with 1 increment by 1 cache 100
      2  /
    
    Sequence created.
    
    SQL> create trigger run_bri
      2  before insert on runs
      3  for each row
      4  begin
      5    select run_seq.nextval
      6      into :new.runid
      7      from dual
      8    ;
      9  end;
     10  /
    
    Trigger created.
    
    SQL> create table run_values
      2  ( run_id number
      3  , value number(3,1)
      4  )
      5  /
    
    Table created.
    
    SQL> insert into runs (model_type, time) values ('model1', to_date('01-01-2009 14:47:00', 'mm-dd-yyyy hh24:mi:ss'));
    
    1 row created.
    
    SQL> insert into run_values (run_id, value) values (run_seq.currval, 1.0);
    
    1 row created.
    
    SQL> insert into run_values (run_id, value) values (run_seq.currval, 2.0);
    
    1 row created.
    
    SQL> insert into runs (model_type, time) values ('model2', to_date('01-01-2009 15:47:00', 'mm-dd-yyyy hh24:mi:ss'));
    
    1 row created.
    
    SQL> insert into run_values (run_id, value) values (run_seq.currval, 3.0);
    
    1 row created.
    
    SQL> insert into run_values (run_id, value) values (run_seq.currval, 4.0);
    
    1 row created.
    
    SQL> insert into run_values (run_id, value) values (run_seq.currval, 5.0);
    
    1 row created.
    
    SQL> select * from runs
      2  /
    
         RUNID MODEL_ TIME
    ---------- ------ -------------------
             1 model1 01-01-2009 14:47:00
             2 model2 01-01-2009 15:47:00
    
    2 rows selected.
    
    SQL> select * from run_values
      2  /
    
        RUN_ID      VALUE
    ---------- ----------
             1          1
             1          2
             2          3
             2          4
             2          5
    
    5 rows selected.
    

    问候, 抢。

    【讨论】:

    • 谢谢 - 一个问题。如果另一个进程/会话在当前进程/会话插入前一个进程/会话的结果时创建了一个新模型运行怎么办?是否仅来自此会话/交易的 currval?
    • 问得好,马克。是的,CURRVAL 是针对会话的,不会受到其他会话的影响。
    • 是的,currval 仅适用于当前会话。如果另一个会话插入一个新的运行,它会增加序列,但另一个会话的 currval 保持不变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多