在
使用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两个表内容进行替换操作 。