有一个表B1中有一个字段IID,我要求它的值是1,2,3,现在表中已有数据,但字段IID的值是乱的,我想重新设置,用一个语句怎么写啊,谢谢了!

解决方案 »

  1.   


    alter table xxx
    add IID_EX int identity(1,1) not null
    goupdate xxx set IID=IID_EX
    go
      

  2.   

    你要怎样设置,没明白。是在select中,还是在企业管理器中
      

  3.   

    是 update 呀,比如数据如下:
    iid    code     name
    1      001      张三
    5      002      李四
    10     005      赵五现在我希望iid的值是1、2、3,用code排序
      

  4.   

    如果iid在數據庫中沒有重復,可以這麼寫
    Update A Set iid=(Select Count(*) From TableName Where iid<=A.iid) From TableName A
      

  5.   

    有重復,就麻煩些。刪除這一列,重加上一個自增列。或者按下面示例的方法,借用一下自增列。Create Table TEST(ID Int,Name Varchar(10))
    Insert TEST Select 1,'aa'
    Union All Select 2,'bb'
    Union All Select 2,'cc'
    GO
    Alter Table TEST Add IID Int Identity(1,1)Update TEST Set ID=IIDAlter Table TEST Drop Column IIDSelect * From TEST
    GO
    Drop Table TEST
    GO
    /*
    ID Name
    1 aa
    2 bb
    3 cc
    */
      

  6.   

    create table A
    (
       iid int,
       code varchar(10),
       name varchar(10)
    )insert A select 1,'001','张三'
    insert A select 5,'002','李四'
    insert A select 5,'002','李四'
    insert A select 10,'005','赵五'
    select identity(int,1,1) as id,* into # from A
    delete  from Ainsert A select id,code,name from #select * from A