set xact_abort on
begin tran
insert 主表 ...
...
insert 子表 ...
commit tran
set xact_abort off

解决方案 »

  1.   

    主表在不COMMIT 之前,能产生自动增量的ID吗?
      

  2.   

    create table ta (id int identity(1,1),col int)
    go
    create table tb (id int identity(1,1),col int)
    go
    insert into ta(col) select 1
    insert into ta(col) select 1
    go
    create proc sp_insert
    @d int
    asbegin traninsert into ta(col) select @d
    insert into tb(col) select @@identity
    if @d > 8 
       rollback
    else
       commit tran
    go
    exec sp_insert 6
    select * from ta
    select * from tb
    /*id          col         
    ----------- ----------- 
    1           1
    2           1
    3           6(所影响的行数为 3 行)id          col         
    ----------- ----------- 
    1           3(所影响的行数为 1 行)*/
    exec sp_insert 9select * from ta
    select * from tb/*
    (所影响的行数为 1 行)id          col         
    ----------- ----------- 
    1           1
    2           1
    3           6(所影响的行数为 3 行)id          col         
    ----------- ----------- 
    1           3(所影响的行数为 1 行)*/
    drop table ta,tb
    drop proc sp_insert
      

  3.   

    [Quote=引用 6 楼 happyflystone 的回复:]
    SQL codecreate table ta (id int identity(1,1),col int)
    go
    create table tb (id int identity(1,1),col int)
    go
    insert into ta(col) select 1
    insert into ta(col) select 1
    go
    create proc sp_insert
    @d int
    asbegin traninsert into ta(col) select @d
    insert into tb(col) select @@identity
    if @d > 8 
       rollback
    else
       commit tran
    go
    exec sp_insert 6
    select * from ta
    select * f…
    [/Quo
    这我是明白的,但我浪沸了一个增量的值,ID已经是不连续的