SQL Server数据库内容替换方法

数据库 SQL Server 数据库运维
在使用iwms系统的过程中,我们会经常遇到数据内容的替换操作。在告诉大家如何替换数据内容之前,我建议大家先了解一下sql server数据库的数据存储类型

在使用iwms系统的过程中,我们会经常遇到数据内容的替换操作。在告诉大家如何替换数据内容之前,我建议大家先了解一下SQL Server数据库的数据存储类型:

SQL Server数据类型:

以上是数据库的基础知识,是做网站的朋友都应该知道的内容(无论你使用什么cms),所以建议大家都耐心看一下。

数据替换一般都发生在字符串数据字段中,除了ntext类型字段以外的其他字符串数据字段都可以使用以下的sql语句进行替换:

update [swf_Upload] set [Dir] = replace([Dir],'200901/14','200901/15')
update [swf_Content] set [Description] =
replace([Description],'200901/14','200901/15')
update [swf_Content_01] set [content] = 
replace(convert(varchar(4000), [content]),'200901/14','200901/15')

UPDATE [数据表名] SET [字段名] = REPLACE([字段名],'老字符串','新字符串')
比如,替换iwms文章数据表(iwms_news)中的标题字段(title)的部分内容,我们应该这么写:

UPDATE [iwms_news] SET [title] = REPLACE([title],'老字符串','新字符串')
上面的sql语句在iwms后台的sql执行里面可以直接执行,基本上可以搞定所有的替换操作,但是由于ntext数据长度的原因,这一方法对ntext类型字段无效。那我们该用什么方法替换ntext类型字段的内容呢?方法有两种:

一是类型转换,将ntext类型转换为varchar类型,然后再用replace。适合于单页内容***长度<4000的文章。

update [数据表名] set [字段名] = replace(convert(varchar(4000), [字段名]),'老字符串','新字符串')
比如,替换iwms文章数据表(iwms_news)中的标题字段(content,ntext类型字段)的部分内容,我们应该这么写:

update iwms_news set [content] = replace(convert(varchar(4000),[content]),'老字符串','新字符串')

二是SQL Server存储过程

declare @ptr varbinary(16)
declare @artId int
declare @Position int,@len int
set @len = datalength('老字符串')
declare wux_Cursor scroll Cursor
for
select textptr([字段名]),[key字段名] from [数据表名]
for read only
open wux_Cursor
fetch next from wux_Cursor into @ptr,@artId
while @@fetch_status=0
begin
select @Position=patindex('%老字符串%',[字段名]) from [数据表名] where [key字段名]=@artId
while @Position>0
begin
set @Position=@Position-1
updatetext [数据表名].[字段名] @ptr @Position @len '新字符串'
select @Position=patindex('%老字符串%',[字段名]) from [数据表名] where [key字段名]=@artId
end
fetch next from wux_Cursor into @ptr,@artId
end
close wux_cursor
deallocate wux_cursor
go

比如,替换iwms文章数据表(iwms_news)中的标题字段(content,ntext类型字段)的部分内容,我们应该这么写

declare @ptr varbinary(16)
declare @artId int
declare @Position int,@len int
set @len = datalength('老字符串')
declare wux_Cursor scroll Cursor
for
select textptr([content]),[articleid] from iwms_news
for read only
open wux_Cursor
fetch next from wux_Cursor into @ptr,@artId
while @@fetch_status=0
begin
select @Position=patindex('%老字符串%',[content]) from iwms_news where [articleid]=@artId
while @Position>0
begin
set @Position=@Position-1
updatetext iwms_news.[content] @ptr @Position @len '新字符串'
select @Position=patindex('%老字符串%',[content]) from iwms_news where [articleid]=@artId
end
fetch next from wux_Cursor into @ptr,@artId
end
close wux_cursor
deallocate wux_cursor
go

ok了,需要注意的是:存储过程只能在SQL Server查询分析器中执行。

另外,由于iwms数据库结构的问题,有分页的文章内容需要先后对iwms_news和iwms_pages两个表内容进行替换操作。

【编辑推荐】

  1. 有效使用SQL Server的自动管理功能
  2. 将SQL 2000日志迁移到SQL Server 2008
  3. 升级SQL Server 2008数据库引擎
责任编辑:book05 来源: wujianrong.com
相关推荐

2011-08-25 16:13:31

SQL Server批量替换数据

2010-07-08 09:53:57

SQL Server

2011-08-29 15:40:00

SQL Server获取TEXT字段的内容DATALENGTH

2009-03-19 09:44:07

SQL Server数据库迁移数据库

2010-09-14 09:53:52

sql server还

2010-07-15 17:28:50

SQL Server

2009-03-30 10:56:58

SQL Server数据库死锁数据库

2009-01-27 21:00:00

服务器数据库SQL Server

2009-05-04 13:43:16

SQL Server置疑数据库恢复

2009-04-10 09:22:48

SQL Server并发测试

2011-03-29 09:40:31

SQL Server数据库链接

2011-03-30 15:36:31

SQL Server

2010-09-06 09:53:41

SQL Server语句

2010-09-03 11:00:47

SQL删除

2011-03-28 14:29:46

SQL Server数主键列

2010-11-08 16:04:06

SQL SERVER连

2010-11-08 14:02:40

SQL Server系

2010-09-13 15:31:14

sql server数

2010-09-13 15:55:17

SQL Server数

2010-10-26 15:54:02

连接oracle数据库
点赞
收藏

51CTO技术栈公众号