怎样把所有text类型并且为空的字段改为" "

TsungLee 2006-06-14 12:10:13
因为用jdbc查询的时候会出错,所以要重新修改数据库。
怎样把表里面所有text类型并且为null的字段,update为" "呢?
如果能对整个数据库操作就更好,因为我有两个数据库,n多个表要修改的。

请各位高手指教。
...全文
135 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
TsungLee 2006-06-14
  • 打赏
  • 举报
回复
先谢谢大家的帮忙。

尝试了xeqtr1982(Visual C# .NET)和wgsasd311(自强不息) 的办法,但都出现这个报错:

子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
语句已终止。
xeqtr1982 2006-06-14
  • 打赏
  • 举报
回复
如果动态@sql变量长度不足,可考虑多个变量连接。

或采用游标
wgsasd311 2006-06-14
  • 打赏
  • 举报
回复
--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 cur

go

wgsasd311 2006-06-14
  • 打赏
  • 举报
回复
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 cur

go
--

xeqtr1982 2006-06-14
  • 打赏
  • 举报
回复
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)
edp08 2006-06-14
  • 打赏
  • 举报
回复
在系统表中找出所有表\所有字段(用游标装着),然后循环判断是否text类型字段,是就拼一条SQL,然后执行之

应该很容易写的,不会超过二十条语句

fengfangfang 2006-06-14
  • 打赏
  • 举报
回复
update tablename set textcol=''
from tablename
where textcol is null
xeqtr1982 2006-06-14
  • 打赏
  • 举报
回复
update 表 set 字段='' where 字段 is NULL
lxzm1001 2006-06-14
  • 打赏
  • 举报
回复
update tablename set textcol=''
wgsasd311 2006-06-14
  • 打赏
  • 举报
回复
--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 cur

go

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧