比如一张表 有id,ids这两个字段 id字段是自动加1的 要怎样才可以当添加一条新记录时 使ids字段的值等于id字段的值例:
id   ids
1     1
2     2
3     3
4     4

解决方案 »

  1.   

    可以用触发器,也可以将 ids 字段设置为计算列.
      

  2.   

    create table tb(id int identity(1,1),col1 varchar(10),id1 int)
    go
    create trigger updateid1
    on tb
    after insert
    as
    update a set id1=b.id from tb a inner join inserted b on a.id=b.id
    go
    insert into tb(col1) select 'aaa'
    select * from tb
    /*
    id          col1       id1
    ----------- ---------- -----------
    1           aaa        1(1 行受影响)*/
    drop table tb
    go
    create table tb(id int identity(1,1),col1 varchar(10),id1 as id)
    go
    insert into tb(col1) select 'aaa'
    select * from tb
    /*
    id          col1       id1
    ----------- ---------- -----------
    1           aaa        1(1 行受影响)*/
    drop table tb
      

  3.   

    计算列create table tb(id int identity(1,1),col1 varchar(10),id1 as id)
    go
    insert into tb(col1) select 'aaa'
    select * from tb