有业务如下:
 A是总表,B是明细表
往A,B表同时插入数据。A表中有个自增列ID值,需要从A表中得到ID,再插入B表中请问高手,用事务如何处理这种业务?

解决方案 »

  1.   

    自增ID,不是业务採番取得ID?
      

  2.   

    给A表添加一个after插入触发器
      

  3.   

    如果A表插入成功,返回A表的@@identity,这个@@identity就是B表所需的ID
      

  4.   

    SQLserver数据库就可以使用这个方法
      

  5.   

    declare @a table
    (
    id int IDENTITY(1,1),
    name varchar(20)
    );
    declare @detail table
    (
    id int,
    detailId int
    );
    --插入过程执行
    declare @temp table
    (
    tempid int
    );
    insert into @a  --插入表a并输出id
    OUTPUT inserted.id
    INTO @temp
    values('种类1')insert into @detail --插入明细表  @detail
      select  (select top(1) tempid from @temp), 1 union all
    select  (select top(1) tempid from @temp), 2 union all
    select  (select top(1) tempid from @temp), 3--测试明细表数据
    select * from @detail
      

  6.   

    7楼的如果业务比较简单可以,但我需要在前台处理,不在后台处理
    ==>
    你在前台,查询 @@identity 就可以得到总表,刚插入的id