eg:
create table A
(
   id  int IDENTITY (1,1)NOT NULL,
   par_ID  int
)--AFTER触发器
CREATE TRIGGER TEMP_SC ON A
AFTER Insert
as
 update A set par_ID=inserted.ID  from A,inserted where A.ID=inserted.ID
insert A
select 1select * from A

解决方案 »

  1.   

    使用触发器中的inserted表获取
      

  2.   

    那在你插入之前:
    select max(id)+标量增加值 from Item
      

  3.   

    插入前不可能知道这个自增列的值,只有插入后才知道。如果不想用触发器,那么只能
    先插入,利用返回的系统常量@@identity获取刚插入的ID,然后Update字段par_ID为@@identity
      

  4.   

    select max(id)+标量增加值 from Item--如果删除了很多最后的记录,那么这个方法不准哦!我就是不知道系统是如何知道插入的那个标识列的值的!我总觉得系统有东西记录着这东西!
      

  5.   

    你可以对表的结构做如下修改
    示例:
    declare @tb1 table(id int identity(1,1),name char(10),par_id as [id])
    insert into @tb1 (name) values ('a')
    insert into @tb1 (name) values ('b')
    insert into @tb1 (name) values ('c')
    insert into @tb1 (name) values ('d')
    select * from @tb1
    /*
    测试结果
    id          name       par_id      
    ----------- ---------- ----------- 
    1           a          1
    2           b          2
    3           c          3
    4           d          4
    */
      

  6.   

    楼上的这样做,能否修改par_id列的值的?
      

  7.   

    你可以这样:
    declare @identity
    insert into 表1 values(需插入的字段)
    @identity=@@identity  
    insert into 表2 values(@identity,其他字段)