日期:2014-05-18  浏览次数:20516 次

两个相关联的插入操作
要在两个表中插入记录,如果有一个不成功,则删除另一表中刚插入的操作。不知该如何做?
比如:
SQL code

insert into tba (Aid,name) values ("101","AA")
insert into tbb (idb,price) values ("101","BB")


以上两语句如有一条不能成功执行,则取消另一个已成功能操作

------解决方案--------------------
SQL code

begin tran
insert into tba (Aid,name) values ("101","AA")
insert into tbb (idb,price) values ("101","BB")
commit tran

------解决方案--------------------
SQL code
create table tba(aid int,name varchar(10))
go
create table tbb(idb int,price dec(18,2))
go

begin tran
  insert into tba (Aid,name) values ('101','AA')
  insert into tbb (idb,price) values ('101','BB')
commit;
--select * from tba
--select* from tbb

begin tran
  insert into tba (Aid,name) values ('101','AA')
  insert into tbb (idb,price) values ('101','12')
commit;

--select * from tba
--select* from tbb

------解决方案--------------------
SQL code

set xact_abort on
begin tran
 insert into tba (Aid,name) values ("101","AA")
 insert into tbb (idb,price) values ("101","BB")
commit tran