我现在要批量修改一数据库里所有表,添加一列,希望用最简单的语句达到目的。ALTER TABLE dbo.t10 ADD col1 varchar(50) NULL最好不要用循环,游标什么的

解决方案 »

  1.   

    不过要确认这一列的名称所有表都没有被引用。
    declare @sql varchar(max)
    select @sql = isnull(@sql,'')+' alter table dbo.'+[name]+' add col1 varchar(50) null '
    from sys.objects 
    where [type] = 'U'
    print @sql
    exec(@sql)
      

  2.   

    > 不过要确认这一列的名称所有表都没有被引用。
    这个当然也可以用where not exists()自动判定。
      

  3.   

    sp_MSforeachtable @command1 = "alter  TABLE ? ADD col1 varchar(50) NULL"