ODBC 错误: [Microsoft][ODBC SQL Server Driver][SQL Server]无法将 NULL 值插入列 'Online',表 'TEST.dbo.Tmp_Members';该列不允许空值。INSERT 失败。
[Microsoft][ODBC SQL Server Driver][SQL Server]语句已终止。表是几天前创建的,而且里面也已经有了数据。
现在要给该表增加一个不为空的字段,以前的数据项可以让该字段为0。
请问我怎样才能在给这张表增加字段,并且将以前数据项的该字段设为0.

解决方案 »

  1.   

    SQL2000有没有一步做到位的方法?
      

  2.   

    --> 测试数据:#1
    if object_id('tempdb.dbo.#1') is not null drop table #1
    create table #1([dd] int)
    insert #1
    select 1 union all
    select 2 union all
    select 3 select * from #1alter table #1
    add  tt  varchar(2) not null default 0
      

  3.   

    出错原因是:你设定该字段不能为null,可是以前没有这个字段,现在要新增就导致必须插入null给这个字段,
    两者之间的是矛盾的。
    你的需求不太合理,由于是新增字段,你为什么不能为null呢。
    解决方法:建立新表,将以前数据导入
    e.g:
     select a,b,c ----a,b,c表示以前字段
        ,0-----表示新增字段的默认值
    from tb into newTB
      

  4.   

    alter table tb
    add  newcol  int not null default 0
    --add  newcol varchar(50) not null default '0'
      

  5.   

    alter table tb add colname int defatlt 0
      

  6.   

    alter table tb add colname int not null defatlt 0