数据库字符串内容批量更新
2010-06-04 23:01
344 查看
数据库经常会遇到需要把一些内容白批量替换的问题,有时是因为存数据时没有编码,有时是因为有些不良的信息,要直接替换。
整理了下如何用SQL语句来替换:
替换指定列内容语句
update [t_test] set [detail] = REPLACE([detail],'打到XXX','新字符串')
注意,这个语句是不能替换ntext的,除了ntext类型的字符类型是可以全部替换
如果要替换ntext类型字段是需要进行类型转换
update [t_test] set [detail] = replace(convert(varchar(4000), [detail]),'打到XXX','新字符串'') where id<4
写一小段SQL来执行完成整个数据表的替换
declare @ptr varbinary(16)
declare @artId int
declare @Position int,@len int
set @len = datalength('XXXA')
declare wux_Cursor scroll Cursor
for
select textptr([detail]),id from t_test
for read only
open wux_Cursor
fetch next from wux_Cursor into @ptr,@artId
while @@fetch_status=0
begin
select @Position=patindex('%打到XXX%',[detail]) from t_test where id=@artId
while @Position>0
begin
set @Position=@Position-1
updatetext [t_test].[detail] @ptr @Position @len 'XXXA'
select @Position=patindex('%打到XXX%',detail) from t_test where id=@artId
end
fetch next from wux_Cursor into @ptr,@artId
end
close wux_cursor
deallocate wux_cursor
go
整理了下如何用SQL语句来替换:
替换指定列内容语句
update [t_test] set [detail] = REPLACE([detail],'打到XXX','新字符串')
注意,这个语句是不能替换ntext的,除了ntext类型的字符类型是可以全部替换
如果要替换ntext类型字段是需要进行类型转换
update [t_test] set [detail] = replace(convert(varchar(4000), [detail]),'打到XXX','新字符串'') where id<4
写一小段SQL来执行完成整个数据表的替换
declare @ptr varbinary(16)
declare @artId int
declare @Position int,@len int
set @len = datalength('XXXA')
declare wux_Cursor scroll Cursor
for
select textptr([detail]),id from t_test
for read only
open wux_Cursor
fetch next from wux_Cursor into @ptr,@artId
while @@fetch_status=0
begin
select @Position=patindex('%打到XXX%',[detail]) from t_test where id=@artId
while @Position>0
begin
set @Position=@Position-1
updatetext [t_test].[detail] @ptr @Position @len 'XXXA'
select @Position=patindex('%打到XXX%',detail) from t_test where id=@artId
end
fetch next from wux_Cursor into @ptr,@artId
end
close wux_cursor
deallocate wux_cursor
go
相关文章推荐
- 数据库字符串内容批量更新
- php 批量更新某字段内容的部分内容 replace(要替换的字段,'被替换的字符串,'替换成的字符串')
- 批量更新数据库所有表中字段的内容,中木马后的急救处理
- 批量更新某字段内容的部分内容 replace(要替换的字段,'被替换的字符串,'替换成的字符串')
- 批量更新数据库所有表中字段的内容,中木马后的急救处理
- Discuz批量替换帖子内容的方法(使用SQL更新数据库)
- linux 批量替换文件内容及查找某目录下所有包含某字符串的文件(批量修改文件内容)
- 对过万条数据的数据库字段内容批量替换程序
- 如何批量更新表中一个字段里面某一部分内容
- C#目录内文件批量查找替换字符串内容
- 使用Go对数据库批量更新
- 数据库_基础知识_MySQL_UpdateSelect(根据查询出来的结果批量更新)
- linux 批量替换文件内容及查找某目录下所有包含某字符串的文件(批量修改文件内容)
- sqlserver replace函数 批量替换数据库中指定字段内指定内容
- sqlserver replace函数 批量替换数据库中指定字段内指定字符串参考方法
- ibaits 数据库批量更新:批处理和事物
- flash上传插件uploadify详解_图片批量上传_更新数据库
- mysql和oracle增删改字段并且批量更新字段内容的操作
- 批量修改数据库某字段内容