如题在一张数据表中有很多列
想要删除含有指定值的列
比如,列值为空 或者列值为0 的列 从表中 把这些列drop掉谢谢了先

解决方案 »

  1.   

    delete from tb where col1 is null 
    or col1=0
    or col2 is null
    or col2=0
    .....
      

  2.   

    应该用alter table 吧.....
      

  3.   


    declare tab_cur cursor  for select name  from  dbo.sysobjects where xtype='U'   and name<>'dtproperties'
           declare @tab_name varchar(50)
           declare @sql  varchar(100)
              open tab_cur 
              fetch next from tab_cur into  @tab_name
              while   @@fetch_status=0
             begin
                fetch next from tab_cur into  @tab_name  
                declare  col_cur cursor for select a.name from  dbo.syscolumns a, .dbo.sysobjects b  where a.id=b.id  and   b.name=@tab_name
                declare @col varchar(30)
                begin
                   open col_cur
                   fetch next from col_cur into @col
                   while   @@fetch_status=0
                   begin
                       set @sql='select *  from  dbo.'+@tab_name+' where '+@col+'<>'''''
                       execute(@sql)
                       if @@rowcount=0 
                       begin
                         set @sql=' alter table  dbo.'+@tab_name+'   drop column '+@col
                         execute(@sql)
                       end
                       fetch next from col_cur into @col
                   end
                  close col_cur
                  deallocate col_cur
             end
             close tab_cur
             deallocate tab_cur
           end