我有4个表,他们是tbl_A,tbl_A_detail(有A_id),tbl_B,tbl_B_detail(有B_id),我已经做了一个返回GenerateCode的“函数”
我想插入tbl_A,值来自tbl_B但是带有新ID。
所以它会是这样的:
tbl_B#1行(GX-110,'Rony')
tbl_B_detail(#1)#1行(GXD-421,'夹克',GX-110)
tbl_B_detail(#1)#2行(GXD-421,'帽子',GX-110)
tbl_B#2行(GX-111,'Joseph')
tbl_B_detail(#2)#1行(GXD-512,'太阳眼镜',GX-111)
tbl_B_detail(#2)#2行(GXD-3623,'Tissue',GX-111)
我想将这些记录移至tbl_A(相同的结构),但使用新的generate_id FOR tbl_A
然后我想从tbl_B_detail插入tbl_A_detail并使用new generate_id FOR tbl_A
INSERT INTO tbl_A (A_id, a_name, ...) SELECT (SELECT (Generate Code Here)), b.name FROM tbl_b
insert into tbl_A_detail(A_detail_id, A_detail_name, A_parent_id) SELECT (SELECT GenerateCode()), B_detail_name, (GeneratedCode FROM inserted Record) FROM tbl_B_detail
我该怎么做? #i很抱歉我的英文不好
答案 0 :(得分:0)
你的表应该支持事务,例如InnoDB。
SET autocommit=0;
begin;
set @generated_id=(Generate Code Here);
insert into table_a (id,title) select @generated_id, title from table_b where id=4;
insert into table_a_detail (id,detail) select @generated_id, detail from table_b_detail where id=4;
commit;