通过syscolumns 表来取字段名,取得的字段名再update表的内容,做一个存储过程.
我有几个表,而且各个表的字段都不一样,因为有些不同的记录和不同的字段为NULL, 
 想通过一个存储过程指表更新某表为NULL值的记录.   
 如下tablea 
 id      a            b            c    
 1         null      a            null 
 2         a               b            null 
 3         b            null      c   
 update   tablea   set   a=isnull(a, ' '),b=isnull(b, ' '),c=isnull(c, ' ')   
 id      a            b            c    
 1                     a                
 2         a         b             
 3         b                  c   
 因为每个表都有20个字段不等,而且字段名都不同.所以直接用一个UPDATE语句比较长,而且每个表也不一样的语句.这样比较麻烦   
 现在想用存储过程在syscolumns表中读取字段名,再更新,但不会写,,高手请帮忙 
------解决方案--------------------create proc procTab 
 	@table varchar(100) 
 as 
 declare @t table(a int,b varchar(20)) 
 declare @a varchar(1000)   
 insert @t select distinct b.colid, b.name from sysobjects a Inner Join  syscolumns b On a.id=b.id  Inner Join systypes c On b.xtype=c.xtype  
 where a.id=object_id(@table) and a.xtype= 'U ' and c.xtype in(34,35,99,167,175,231,239) order by b.colid   
 select @a=coalesce(@a+ ', ', ' ')+b+ '=isnull( '+b+ ', ' ' ' ') '  from @t 
 select @a= 'update  '+@table+ ' set  '+@a    
 exec(@a)   
 go  
 --如果更新整个库中的表则如下 
 sp_msforeachtable  'exec procTab  ' '? ' '  ' 
------解决方案--------------------declare @col_name varchar(50),@tab_name varchar(50) 
 set @tab_name= 'a '  ----------------------调用表名a 其它不用改,你也可放在存在过程中 
 declare i cursor for 
 select name from syscolumns where id=OBJECT_ID (@tab_name) 
 open i   
   fetch next  from i into @col_name      
     exec( 'update  '+@tab_name+ ' set  '+@col_name+ '=isnull( '+@col_name+ ', ' ' ' ') ') 
   while (@@fetch_status=0) 
   begin 
     fetch next from i into @col_name 
     exec( 'update  '+@tab_name+ ' set  '+@col_name+ '=isnull( '+@col_name+ ', ' ' ' ') ')    
   end   
 close i 
 deallocate i