plsql使用一个立即执行命令插入多行

发布于 2024-10-19 22:22:40 字数 99 浏览 2 评论 0原文

有没有一种方法可以在一个EXECUTE IMMEDIATE中插入多行?而不是为每个插入编写 EXECUTE IMMEDIATE...

Is there a way to insert multiple rows in one EXECUTE IMMEDIATE? Rather than writing EXECUTE IMMEDIATE for each insert...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南城旧梦 2024-10-26 22:22:40

很难说出你正在插入什么。您可以使用 EXECUTE IMMEDIATE 轻松地执行 INSERT...SELECT ,但我怀疑这不是您想要的,并且可能您不仅仅是想要围绕 EXECUTE IMMEDIATE 进行循环。

如果多表插入不是您想要的,您可以在 PL/SQL 块上和/或在 FORALL 中使用 EXECUTE IMMEDIATE

create table test_forall_dyn (val varchar2(1));

declare
  type tab_char is table of varchar2(1) index by binary_integer;
  t_char tab_char;
begin
  for i in 1..26 loop
    t_char(i) := chr(64 + i);
  end loop;
  forall i in 1..26
    execute immediate 
      'begin 
         insert into test_forall_dyn (val) values(:1);  
         insert into test_forall_dyn (val) values(:1); 
       end;' 
       using t_char(i);
end;
/

select count(*) from test_forall_dyn;

Hard to tell what you are inserting. You can use EXECUTE IMMEDIATE to do an INSERT...SELECT easily enough, but I suspect that isn't what you are after, and probably you're not simply wanting a loop around the EXECUTE IMMEDIATE.

If the multi-table insert isn't what you are looking for, you can use EXECUTE IMMEDIATE on a PL/SQL block and/or within a FORALL

create table test_forall_dyn (val varchar2(1));

declare
  type tab_char is table of varchar2(1) index by binary_integer;
  t_char tab_char;
begin
  for i in 1..26 loop
    t_char(i) := chr(64 + i);
  end loop;
  forall i in 1..26
    execute immediate 
      'begin 
         insert into test_forall_dyn (val) values(:1);  
         insert into test_forall_dyn (val) values(:1); 
       end;' 
       using t_char(i);
end;
/

select count(*) from test_forall_dyn;
余生一个溪 2024-10-26 22:22:40
EXECUTE IMMEDIATE
INSERT INTO table (col1, col2, col3) (
            SELECT 1 AS col1, 2 AS col2, 3 AS col3 FROM dual
  UNION ALL SELECT 4,         5,         6         FROM dual
  UNION ALL SELECT 7,         8,         9         FROM dual ) ;
EXECUTE IMMEDIATE
INSERT INTO table (col1, col2, col3) (
            SELECT 1 AS col1, 2 AS col2, 3 AS col3 FROM dual
  UNION ALL SELECT 4,         5,         6         FROM dual
  UNION ALL SELECT 7,         8,         9         FROM dual ) ;
尤怨 2024-10-26 22:22:40

玛丽亚,
为什么首先使用动态sql?
大多数时候,使用动态 SQL 并不能完全提高可伸缩性。
对于可读性也是如此。
调试比较困难....
在许多情况下,还存在奇怪的安全问题......
我不知道为什么使用动态 sql,但如果这是生产应用程序的一部分,我会重新考虑使用它。

罗纳德.

Mariya,
why use dynamic sql in the first place?
Most of the times scalability is not exactly improved using dynamic sql.
The same is for readability.
Debugging is harder ....
In many cases there are also weird security issues....
I don't know why you use dynamic sql but if this is part of a production application I would reconsider using it.

Ronald.

转身泪倾城 2024-10-26 22:22:40

@玛丽亚
第一帧选择查询为您提供了您要插入的多行...
您选择stament应按照您要插入表中的数据顺序给出数据

然后使用..

插入Table1(col1,col2,col3)(从tabl2中选择姓名,地址,电话)
犯罪;

@maria
First frame select quesry which gives u multiple row wich u are going to insert...
You select stament shoud give data in order of data u want to insert in table

Then use..

Insert into Tabl1 (col1,col2,col3)(select name,address,phone from tabl2)
commit;

猫烠⑼条掵仅有一顆心 2024-10-26 22:22:40

当然,您可以批量插入...

Sure, you can go for batch insert...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文