ALTER TABLE doc_exa ADD column_b VARCHAR(20) NULL

解决方案 »

  1.   

    用alter语句修改数据表的结构不就可以了吗?
      

  2.   

    select * into #temp from table
    drop table table
    select col1,col2,'' as NewCol,col3..... into table from #temp
    drop table #temp
      

  3.   

    create table testco (id1 int,id2 int)alter table testco add id3 intEXEC sp_configure 'allow updates',1  RECONFIGURE WITH OVERRIDE
    update syscolumns set colid = 4 where name = 'id2' and id = 2066106401
    update syscolumns set colid = 2 where name = 'id3' and id = 2066106401
    update syscolumns set colid = 3 where name = 'id2' and id = 2066106401
    EXEC sp_configure 'allow updates',0  RECONFIGURE WITH OVERRIDEselect * from testco
      

  4.   

    create table testco (id1 int,id2 int)alter table testco add id3 intEXEC sp_configure 'allow updates',1  RECONFIGURE WITH OVERRIDE
    update syscolumns set colid = 4 where name = 'id2' and id = object_id('testco')
    update syscolumns set colid = 2 where name = 'id3' and id = object_id('testco')
    update syscolumns set colid = 3 where name = 'id2' and id = object_id('testco')
    EXEC sp_configure 'allow updates',0  RECONFIGURE WITH OVERRIDEselect * from testco
      

  5.   

    回复Chiff:
        我按你写的试了一下运行后系统提示:未启用对系统目录的特殊更新。系统管理员必须重新配置 SQL Server 以允许这种操作。
    请问怎么回这样???
      

  6.   

    加几个go如下 :
    create table testco (id1 int,id2 int)alter table testco add id3 int
    go
    EXEC sp_configure 'allow updates',1  RECONFIGURE WITH OVERRIDE
    go
    update syscolumns set colid = 4 where name = 'id2' and id = object_id('testco')
    update syscolumns set colid = 2 where name = 'id3' and id = object_id('testco')
    update syscolumns set colid = 3 where name = 'id2' and id = object_id('testco')
    go
    EXEC sp_configure 'allow updates',0  RECONFIGURE WITH OVERRIDE
      

  7.   

    如果在存储过程不宜写go的情况下,也可这样:create table testco (id1 int,id2 int)alter table testco add id3 intEXEC sp_configure 'allow updates',1  RECONFIGURE WITH OVERRIDEexec('update syscolumns set colid = 4 
          where name = ''id2'' and id = object_id(''testco'')')
    exec('update syscolumns set colid = 2 
          where name = ''id3'' and id = object_id(''testco'')')
    exec('update syscolumns set colid = 3 
          where name = ''id2'' and id = object_id(''testco'')')EXEC sp_configure 'allow updates',0  RECONFIGURE WITH OVERRIDEselect * from testco
      

  8.   

    回复Chiff: 
        已经可以插入字段了!!!谢谢!!!
    但是数据不对了???还是不能用呀
      

  9.   

    回复Chiff: 
        好的!谢谢!!!
      

  10.   

    create proc addcolumn
    @tablename varchar(30),  --表名
    @colname varchar(30),    --要加的列名
    @coltype varchar(100),   --要加的列类型
    @colid int               --加到第几列
    asdeclare @colid_max int    
    declare @sql varchar(1000) --动态sql语句
    --------------------------------------------------
    if not exists(select  1 from sysobjects 
                  where name = @tablename and xtype = 'u')
      begin
      raiserror 20001 '没有这个表'
      return -1
      end
    --------------------------------------------------
    if exists(select 1 from syscolumns
              where id = object_id(@tablename) and name = @colname)
      begin
      raiserror 20002 '这个表已经有这个列了!'
      return -1
      end
    --------------------------------------------------
    --保证该表的colid是连续的
    select @colid_max = max(colid) from syscolumns where id=object_id(@tablename)if @colid > @colid_max or @colid < 1 
       set @colid = @colid + 1
    --------------------------------------------------
    set @sql = 'alter table '+@tablename+' add '+@colname+' '+@coltype 
    exec(@sql)select @colid_max = colid 
    from syscolumns where id = object_id(@tablename) and name = @colname
    if @colid_max is null
      begin
      raiserror 20003 '加一个新列不成功,请检查你的列类型是否正确'
      return -1
      end
    --------------------------------------------------
    --打开修改系统表的开关
    EXEC sp_configure 'allow updates',1  RECONFIGURE WITH OVERRIDE--将新列列号暂置为-1
    set @sql = 'update syscolumns
                set colid = -1
                where id = object_id('''+@tablename+''') 
                      and colid = '+cast(@colid_max as varchar(10))
    exec(@sql)--将其他列的列号加1
    set @sql = 'update syscolumns
                set colid = colid + 1
                where id = object_id('''+@tablename+''') 
                      and colid >= '+cast(@colid as varchar(10))
    exec(@sql)--将新列列号复位
    set @sql = 'update syscolumns
                set colid = '+cast(@colid as varchar(10))+'
                where id = object_id('''+@tablename+''') 
                      and name = '''+@colname +''''
    exec(@sql)
    --------------------------------------------------
    --关闭修改系统表的开关
    EXEC sp_configure 'allow updates',0  RECONFIGURE WITH OVERRIDE
    go
    调用方法:
    exec addcolumn '表名','新列名','新列类型',加到第几个位置
    如:
    exec addcolumn 'test','id2','char(10)',2
    表示将id2这个列加到表test的第二个位置,类型是char(10)。
      

  11.   

    ---------更正-----------------------------------------
    create proc addcolumn
    @tablename varchar(30),  --表名
    @colname varchar(30),    --要加的列名
    @coltype varchar(100),   --要加的列类型
    @colid int               --加到第几列
    asdeclare @colid_max int    
    declare @sql varchar(1000) --动态sql语句
    --------------------------------------------------
    if not exists(select  1 from sysobjects 
                  where name = @tablename and xtype = 'u')
      begin
      raiserror 20001 '没有这个表'
      return -1
      end
    --------------------------------------------------
    if exists(select 1 from syscolumns
              where id = object_id(@tablename) and name = @colname)
      begin
      raiserror 20002 '这个表已经有这个列了!'
      return -1
      end
    --------------------------------------------------
    --保证该表的colid是连续的
    select @colid_max = max(colid) from syscolumns where id=object_id(@tablename)if @colid > @colid_max or @colid < 1 
       set @colid = @colid + 1
    --------------------------------------------------
    set @sql = 'alter table '+@tablename+' add '+@colname+' '+@coltype 
    exec(@sql)select @colid_max = colid 
    from syscolumns where id = object_id(@tablename) and name = @colname
    if @@rowcount <> 1
      begin
      raiserror 20003 '加一个新列不成功,请检查你的列类型是否正确'
      return -1
      end
    --------------------------------------------------
    --打开修改系统表的开关
    EXEC sp_configure 'allow updates',1  RECONFIGURE WITH OVERRIDE--将新列列号暂置为-1
    set @sql = 'update syscolumns
                set colid = -1
                where id = object_id('''+@tablename+''') 
                      and colid = '+cast(@colid_max as varchar(10))
    exec(@sql)--将其他列的列号加1
    set @sql = 'update syscolumns
                set colid = colid + 1
                where id = object_id('''+@tablename+''') 
                      and colid >= '+cast(@colid as varchar(10))
    exec(@sql)--将新列列号复位
    set @sql = 'update syscolumns
                set colid = '+cast(@colid as varchar(10))+'
                where id = object_id('''+@tablename+''') 
                      and name = '''+@colname +''''
    exec(@sql)
    --------------------------------------------------
    --关闭修改系统表的开关
    EXEC sp_configure 'allow updates',0  RECONFIGURE WITH OVERRIDE
    go
    调用方法:
    exec addcolumn '表名','新列名','新列类型',加到第几个位置
    如:
    exec addcolumn 'test','id2','char(10)',2
    表示将id2这个列加到表test的第二个位置,类型是char(10)。
      

  12.   

    回复Chiff:
        这位大哥(大姐)不好意思,好象还是不行,数据丢失了。
      

  13.   

    回复Chiff:
       id1   id2
        1 2
        3 4
        5 6        
        7 8
        9 0
    正确的:
       id1   id3    id2
        1    NULL    2
        3    NULL    4
        5    NULL    6     
               .
               .
      

  14.   


    使用Chiff(~o~) 的存储过程
    测试结果正确select * into #tTemp from tablename 
    exec addcolumn 'tablename','testname2','varchar',2
    go
    delete tablename 
    insert tablename select [colname1],'',[colname.....字段列表] from #tTemp
    drop table #tTemp
      

  15.   

    zxdragon(zxdragon), 测试结果是和我上面的结果一样的吗???