因为用jdbc查询的时候会出错,所以要重新修改数据库。
怎样把表里面所有text类型并且为null的字段,update为" "呢?
如果能对整个数据库操作就更好,因为我有两个数据库,n多个表要修改的。请各位高手指教。

解决方案 »

  1.   

    update tablename set textcol=''
      

  2.   

    update 表 set 字段='' where 字段 is NULL
      

  3.   

    update tablename set textcol=''
    from tablename
    where textcol is null
      

  4.   

    在系统表中找出所有表\所有字段(用游标装着),然后循环判断是否text类型字段,是就拼一条SQL,然后执行之应该很容易写的,不会超过二十条语句
      

  5.   

    declare @sql varchar(8000)
    set @sql=''
    select @sql=@sql+'update ['+b.name+'] set ['+a.name+']='''' where ['+a.name+'] is NULL;' from syscolumns a,sysobjects b,systypes c where a.id=b.id and b.xtype='U' and a.xtype=c.xtype and c.name in('text')
    exec(@sql)
      

  6.   

    if exists(select 1 from sysobjects where xtype='p' and name='update_null')
    drop proc update_null
    go
    /*
    功能:处理表test字段中的NULL值,用''替换
    作者:WGS
    创建时间:2006-06-14
    调用事例:
    1。对一个表操作:update 'tb1'
    2。对整个数据库操作:sp_msforeachtable "update '?'"
    */
    create proc update_null(@tbname nvarchar(50))
    as
    declare @fld varchar(20),@ftype varchar(30),@fid int,@sql varchar(300)
    declare cur cursor for 
    select a.name,b.name,a.xusertype from syscolumns a,systypes b
    where a.xusertype=b.xusertype and a.id=object_id(@tbname) order by b.xusertype
    open cur
    fetch next from cur into @fld,@ftype,@fid
    while @@fetch_status=0
    begin
    if @ftype in('text','ntext')
    exec('update '+@tbname+' set '+@fld+'='''' where '+@fld+' is null')
    fetch next from cur into @fld,@ftype,@fid
    end
    close cur
    deallocate curgo
    --
      

  7.   

    --try
    if exists(select 1 from sysobjects where xtype='p' and name='update_null')
    drop proc update_null
    go
    /*
    功能:处理表test字段中的NULL值,用''替换
    作者:WGS
    创建时间:2006-06-14
    调用事例:
    1。对一个表操作:update_null 'tb1'
    2。对整个数据库操作:sp_msforeachtable "update_null  '?'"
    */
    create proc update_null(@tbname nvarchar(50))
    as
    declare @fld varchar(20),@ftype varchar(30),@fid int,@sql varchar(300)
    declare cur cursor for 
    select a.name,b.name,a.xusertype from syscolumns a,systypes b
    where a.xusertype=b.xusertype and a.id=object_id(@tbname) order by b.xusertype
    open cur
    fetch next from cur into @fld,@ftype,@fid
    while @@fetch_status=0
    begin
    if @ftype in('text','ntext')
    exec('update '+@tbname+' set '+@fld+'='''' where '+@fld+' is null')
    fetch next from cur into @fld,@ftype,@fid
    end
    close cur
    deallocate curgo
      

  8.   

    如果动态@sql变量长度不足,可考虑多个变量连接。或采用游标
      

  9.   

    先谢谢大家的帮忙。尝试了xeqtr1982(Visual C# .NET)和wgsasd311(自强不息) 的办法,但都出现这个报错:子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
    语句已终止。
      

  10.   

    --try
    if exists(select 1 from sysobjects where xtype='p' and name='update_null')
    drop proc update_null
    go
    /*
    功能:处理表中"text"和"ntext"类型字段中的NULL值,用''替换
    作者:WGS
    创建时间:2006-06-14
    调用事例:
    1。对一个表操作:update_null 'tb1'
    2。对整个数据库操作:sp_msforeachtable "update_null  '?'"
    */
    create proc update_null(@tbname nvarchar(50))
    as
    declare @fld varchar(20),@ftype varchar(30),@fid int,@sql varchar(300)
    declare cur cursor for 
    select a.name,b.name,a.xusertype from syscolumns a,systypes b
    where a.xusertype=b.xusertype and a.id=object_id(@tbname) order by b.xusertype
    open cur
    fetch next from cur into @fld,@ftype,@fid
    while @@fetch_status=0
    begin
    if @ftype in('text','ntext')
    exec('update '+@tbname+' set '+@fld+'='''' where '+@fld+' is null')
    fetch next from cur into @fld,@ftype,@fid
    end
    close cur
    deallocate curgo