begin tran
create table t
(id int identity(1,1) primary key,name  varchar(10),fatherid int)
go
insert into t(name,fatherid)
select 'test',isnull(max(id),0)+1 from tinsert into t(name,fatherid)
select 'test',max(id)+1 from tselect * from t
rollback

解决方案 »

  1.   

    create trigger tr_insert on table
    for  insert
    as update r set b=@@identity where id=@@identity
    @@IDENTITY
    返回最后插入的标识值
    @@IDENTITY 的值不会被其他用户的插入语句影响,但会被触发器影响
      

  2.   

    方法1:用插入触发器方法2:
    insert into mytable values(name,title,fatherid )
    select @id = @@identity
    update mytable set fatherid = @id where id = @@id
      

  3.   

    插入语句就一条啊,其它都是辅助性的
    insert into t(name,fatherid)
    select 'test',max(id)+1 from t要不然你注就用楼上两位的方法,要插入数据,再更新fatherid的值,这梓用到两条语句,当然,如果数据量大的情况下,楼上两位的方法好过我的方法,我当时帮你考虑的是怎样用一条语句摘定
      

  4.   

    多谢楼上指点!但那个@id = @@identity是什麽意思?谁能解释一下?
      

  5.   

    我不明白你要干什么?既然fatherid与id的值相同,为什么还要建此列,
    这违反了RDBMS的原则!