【问题标题】:how to fire two oracle insert statement together?Oracle 11g如何同时触发两个 oracle 插入语句?Oracle 11g
【发布时间】:2012-07-03 04:56:25
【问题描述】:

我试图一次触发两个插入语句。实际上我已经尝试过以下查询,但它只插入一个表中。

EXECUTE IMMEDIATE 'select * from abc.test where test_NAME = ''aaa''' BULK COLLECT INTO T_SC;
IF T_SC.count = 0 THEN
    Insert into abc.test (test_ID,test_NAME,status) 
    VALUES(1,'aaa','a') BULK COLLECT INTO insert_cnt;
    IF insert_cnt.count = 1 THEN
    INSERT INTO abc.test1(test1_id,test1_NAME,test1_ALIAS,test_ID)
    VALUES(1,'bbb','b',1);
    COMMIT;
END IF;

它只是插入 abc.test1 表..我会错过什么。如果有人知道,请帮助我。

【问题讨论】:

  • 触发器在这种情况下很有用。调查一下。
  • @vjshah:我不想使用触发器,因为我只是在编写脚本。这就是为什么。
  • 您可以使用存储过程,您可以轻松地将两个插入语句一起使用并触发操作。顺便说一句,在你的代码中,你有两个 if 但单端 if.. 看看那个。
  • 这绝不需要/保证触发器。

标签: oracle insert oracle11g


【解决方案1】:

您的整个代码似乎都不正确:

  1. 为什么是动态 sql?
  2. 有很多语法错误
    • 未封闭的“如果”(正如 vj shah 评论的那样)
    • 缺少returning关键字
  3. 如果您只从插入返回一行,为什么还需要bulk collect
  4. 第二个“如果”代表什么?

等等……

无论如何,这段代码有效:

EXECUTE IMMEDIATE 'select * from abc.test where test_NAME = ''aaa''' BULK COLLECT INTO T_SC;

/* BTW, why not 
select * bulk collect into T_SC from abc.test where test_NAME = 'aaa';
*/

IF T_SC.count = 0 THEN
    Insert into abc.test (test_ID, test_NAME, status) 
    VALUES(1,'aaa','a') returning test_ID, test_NAME, status BULK COLLECT INTO insert_cnt;
    IF insert_cnt.count = 1 THEN
       INSERT INTO abc.test1(test1_id,test1_NAME,test1_ALIAS,test_ID)
       VALUES(1,'bbb','b',1);
    END IF;
    COMMIT;
END IF;

【讨论】:

  • 在使用您的代码时给我这个错误。 Error at Command Line:1 Column:9 Error report: SQL Error: ORA-03001: unimplemented feature 03001. 00000 - "unimplemented feature" *Cause: This feature is not implemented. *Action: None.
【解决方案2】:

你能更好地解释你的问题吗?你的逻辑和你展示的数据都不能说明你想要完成什么。 (if 背后的逻辑)。

这也不是功能代码(太多语法错误),您可以使用您正在触发的真实代码进行更新吗?可能只是更改表名?

如果您想确保两个语句都成功完成或都回滚,则将它们包含在块中的方法是正确的。

SQL> create table test_rc_2(
  2    id number
  3  );

Table created.

--Sample 1 : 单独提交inserts(只滚动最新的语句

SQL> insert into test_rc_2 values (100);

1 row created.

SQL> insert into test_rc_2 values ('hello');
insert into test_rc_2 values ('hello')
                              *
ERROR at line 1:
ORA-01722: invalid number


SQL> commit;

Commit complete.

SQL> select * from test_rc_2;

        ID
----------
       100

--case 2 : 分块提交。

    SQL> truncate table test_rc_2
      2  ;

    Table truncated.

    SQL> begin
      2    insert into test_rc_2 values(100);
      3    insert into test_rc_2 values('hello..');
      4  end;
      5  /
    begin
    *
    ERROR at line 1:
    ORA-01722: invalid number
    ORA-06512: at line 3

SQL> commit;

Commit complete.

SQL> select * from test_rc_2;

no rows selected

【讨论】:

    猜你喜欢
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2014-02-27
    • 2019-04-21
    相关资源
    最近更新 更多