多表插入

liudun1988 2009-12-19 10:52:46
我想要在一个存储过程中实现多表插入操作。要能对不同的表插入数据。
...全文
259 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
时光.. 2011-06-24
  • 打赏
  • 举报
回复
关注下
碧水幽幽泉 2010-01-12
  • 打赏
  • 举报
回复
还没结贴啊!
问题还没解决?
Northgale 2010-01-12
  • 打赏
  • 举报
回复
用merger Into试试
liudun1988 2009-12-26
  • 打赏
  • 举报
回复
我想只要一个inset语句,用传参的方式。封装一个存储过程。
eviler 2009-12-25
  • 打赏
  • 举报
回复
存储过程是随便搞的 ,插入多少表都中吧,一般

Create Or Replace Procedure My_Test(……)

as
Begin

Insert into a values('a') ;
insert into a values('b') ;

insert into b valuse(1) ;
insert into b values(100);

……
……
End ;
sql_xeppp 2009-12-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 liudun1988 的回复:]
有没有更简单一点的。
[/Quote]

INSERT ALL | FIRST
when condition then
INTO T1 (C1, C2, ...) VALUES (C1, C2, ...)
when condition then
INTO T2 (C1, C2, ...) VALUES (C1, C2, ...)
...
SELECT C1, C2, ... FROM T9;
安特矮油 2009-12-25
  • 打赏
  • 举报
回复
这个看你自己需要插入哪些表 就在存储过程里面写对应的sql就是了 相当简单的
xtxjx 2009-12-23
  • 打赏
  • 举报
回复
具体分析表结构。建议用开发工具加ORACLE一起来实现比较简单
liudun1988 2009-12-23
  • 打赏
  • 举报
回复
有没有更简单一点的。
ACMAIN_CHM 2009-12-19
  • 打赏
  • 举报
回复
存储过程中,你可以执行多个 insert ,好象这不是什么问题啊? 你的具体问题是什么?
wh62592855 2009-12-19
  • 打赏
  • 举报
回复
http://blog.csdn.net/wh62592855/archive/2009/12/05/4948293.aspx

参见上面的文章
关于INSERT ALL和INSERT FIRST的用法
sulins 2009-12-19
  • 打赏
  • 举报
回复
在一个存储过程里头,可以写多条insert语句插入不同的表,没有限制的。
若是要求一条SQL语句往多张表插数据:

INSERT ALL
INTO T1 (C1, C2, ...) VALUES (C1, C2, ...)
INTO T2 (C1, C2, ...) VALUES (C1, C2, ...)
...
SELECT C1, C2, ... FROM T9;
crazylaa 2009-12-19
  • 打赏
  • 举报
回复
利用自定义类型和index by表,对两表进行批量插入:java代码调用详见我的blog里面。给出sql:

create table parent(
id number(10),
name varchar2(100),
title varchar2(10)
);

create table child(
id number(10),
parent_id number(10),
child_name varchar2(100),
child_title varchar2(10),
child_content varchar2(200),
child_time timestamp
);

create sequence seq_p_c_id
minvalue 1
maxvalue 9999999999
start with 1
increment by 1
nocache;

drop type t_child_lst_map;
drop type t_child_lst;
drop type t_parent_lst;

create or replace type t_parent as object (
name varchar2(100),
title varchar2(10)
);
/

create or replace type t_child as object (
child_name varchar2(100),
child_title varchar2(10),
child_content varchar2(200)
);
/

create or replace type t_parent_lst as table of t_parent;
/

create or replace type t_child_lst as table of t_child;
/

create or replace type t_child_lst_map as table of t_child_lst;
/

create or replace procedure proc_ins_parent_child(
i_parent_lst in t_parent_lst, --parent列表
i_child_map_lst in t_child_lst_map, --child列表集合,一个map元素对应一个child_lst,其下标与 parent列表的下标相同。
o_ret out number
) as
var_parent t_parent;
var_child_lst t_child_lst;
var_child t_child;
var_parent_id number;
var_child_id number;
begin
for i in 1..i_parent_lst.count loop
--取得parent各列的值
var_parent := i_parent_lst(i);

--取得parent_id;
select seq_p_c_id.nextVal into var_parent_id from dual;

--插入parent表
insert into parent(
id,
name,
title
)
values(
var_parent_id,
var_parent.name,
var_parent.title
);

--取得该parent对应的child列表
var_child_lst := i_child_map_lst(i);

for j in 1..var_child_lst.count loop
var_child := var_child_lst(j);

--取得child_id;
select seq_p_c_id.nextVal into var_child_id from dual;

--插入child表
insert into child(
id,
parent_id,
child_name,
child_title,
child_content,
child_time
)
values(
var_child_id,
var_parent_id,
var_child.child_name,
var_child.child_title,
var_child.child_content,
systimestamp
);
end loop;

end loop;
o_ret := 0;

exception when others then
begin
o_ret := -1;
raise;
end;
end proc_ins_parent_child;
/


3,491

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧