请问多表插入的问题

[复制链接]
查看11 | 回复6 | 2010-2-9 01:01:02 | 显示全部楼层 |阅读模式
现有表a(id,a_name),表b(id,a_id,b_name,b_value).为一对多的关系。
现要把一条记录同时插入表a和b,但表b的a_id要存a的id,如何实现呢?
回复

使用道具 举报

千问 | 2010-2-9 01:01:02 | 显示全部楼层
you want to finish this work with a sql?
I think It's unrealistic
回复

使用道具 举报

千问 | 2010-2-9 01:01:02 | 显示全部楼层
begin
insert into a (id,a_name) values (10,'ten');
commit;如b.a_id為外關聯,則要先commit再繼續,否則你將無法把新的a.id插到b.a_id中)
insert into b (id,a_id,b_name,b_value)

values(100,10,'tenten',100100);
commit;
end;
回复

使用道具 举报

千问 | 2010-2-9 01:01:02 | 显示全部楼层
我想了一种方法,如果在a中还有一个字段a_time,b中也有个字段b_time,a_time=b_time,可以先取得a_time的值
create procedure
@tim
insert into a(a_name,a_time) value("myname",@time);
insert into b(a_id,b_name,b_time) values select a_id,'mybame',@time from a where a_time=@time
回复

使用道具 举报

千问 | 2010-2-9 01:01:02 | 显示全部楼层
用trigger试试看
回复

使用道具 举报

千问 | 2010-2-9 01:01:02 | 显示全部楼层
同意楼上~
回复

使用道具 举报

千问 | 2010-2-9 01:01:02 | 显示全部楼层
在new_sql 环境下:
先把这条记录插入一个空临时表中:他t_temptable(id1,name1,id1,name2 valuename,....)第然后:
insert into a(id,a_name) as select id1,name1 from t_temptable;
insert into b (id,a_id,b_name,b_value) as select id2,id1,name2,valuename from t_temptable;
这样就轻松ok了!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行