table1:
id name password 
1   sa    sa
2   bb    bb现在的要求是table1中新加一列rename,
先设置为可以为空,加成功之后把rename中的值更新成111,然后再把rename列更新为不能为空应该怎样写啊???急急急。

解决方案 »

  1.   

    alter table table1 add  rename VARCHAR(100) default '111' NOT NULL 
      

  2.   

    alter table  table1
    add rename varchar(50) null
    update table1 set rename='111'alter table  table1
    alter column rename varchar(50) not null
      

  3.   

    --> Test Data: table1
    create  table table1([id] int,[name] varchar(2),[password] varchar(2))
    insert into table1
    select 1,'sa','sa' union all
    select 2,'bb','bb'
    go
    alter table table1 add  rename varchar(50) nullupdate table1 set rename='111'
    select * from table1
    alter table table1 alter  column  rename varchar(50) not nullinsert into table1(rename) select null
    --Code--Drop
    drop table table1
    --Result
    /*
    id          name password rename                                             
    ----------- ---- -------- -------------------------------------------------- 
    1           sa   sa       111
    2           bb   bb       111(所影响的行数为 2 行)
    (所影响的行数为 2 行)服务器: 消息 515,级别 16,状态 2,行 1
    无法将 NULL 值插入列 'rename',表 'test.dbo.table1';该列不允许空值。INSERT 失败。
    语句已终止。*/
      

  4.   

    ALTER TABLE [table1] ADD rename int NULL CONSTRAINT ccc
    DEFAULT 111 WITH VALUES 
    ALTER TABLE [table1] ALTER COLUMN  rename int NOT NULL
      

  5.   


    这样可以啊,但我想要的是'111'是动态从别的地方取得的,不一定是相同的值alter table table1 add  rename VARCHAR(100) default (一段sql语句) NOT NULL也不好用啊???