在SQL的一个语句中插入多条记录,如何跳过不合法的记录继续插入合法的记录?

解决方案 »

  1.   

    默认情况下,xact_abort  是关闭的,意味着 如果你 提交一个批语句 当其中某条语句出错,仅回滚该语句不会回滚所有的
    create table dbo.t (id int not null primary key)
    declare @i int
    set @i =1begin tran
    while @i<=10
    begin
    insert into t select @i
    insert into t select @i --报错
    set @i=@i+1
    end
    commit tran
    如果 set xact_abort on则上述语句全部回滚,表t中不会有数据