2019/3/14 8:48:36
在使用(yòng)iwms系統的(de)過程中,我們會(huì)經常遇到(dào)數據内容的(de)替換操作。在告訴大(dà)家如(rú)何替換數據内容之前,我建議大(dà)家先了解一下SQL Server數據庫的(de)數據存儲類型:
SQL Server數據類型:
以上(shàng)是數據庫的(de)基礎知識,是做網站的(de)朋友(yǒu)都(dōu)應該知道的(de)内容(無論你(nǐ)使用(yòng)什(shén)麽cms),所以建議大(dà)家都(dōu)耐心看一下。
數據替換一般都(dōu)發生在字符串數據字段中,除了ntext類型字段以外的(de)其他(tā)字符串數據字段都(dōu)可(kě)以使用(yòng)以下的(de)sql語句進行(xíng)替換:
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([字段名],'老字符串','新字符串')
比如(rú),替換iwms文(wén)章(zhāng)數據表(iwms_news)中的(de)标題字段(title)的(de)部分(fēn)内容,我們應該這麽寫:
UPDATE [iwms_news] SET [title] = REPLACE([title],'老字符串','新字符串')
上(shàng)面的(de)sql語句在iwms後台的(de)sql執行(xíng)裏面可(kě)以直接執行(xíng),基本上(shàng)可(kě)以搞定所有(yǒu)的(de)替換操作,但(dàn)是由于ntext數據長度的(de)原因,這一方法對ntext類型字段無效。那我們該用(yòng)什(shén)麽方法替換ntext類型字段的(de)内容呢?方法有(yǒu)兩種:
一是類型轉換,将ntext類型轉換爲varchar類型,然後再用(yòng)replace。适合于單頁内容最大(dà)長度<4000的(de)文(wén)章(zhāng)。
update [數據表名] set [字段名] = replace(convert(varchar(4000), [字段名]),'老字符串','新字符串')
比如(rú),替換iwms文(wén)章(zhāng)數據表(iwms_news)中的(de)标題字段(content,ntext類型字段)的(de)部分(fēn)内容,我們應該這麽寫:
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
比如(rú),替換iwms文(wén)章(zhāng)數據表(iwms_news)中的(de)标題字段(content,ntext類型字段)的(de)部分(fēn)内容,我們應該這麽寫
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了,需要注意的(de)是:存儲過程隻能(néng)在SQL Server查詢分(fēn)析器(qì)中執行(xíng)。
另外,由于iwms數據庫結構的(de)問題,有(yǒu)分(fēn)頁的(de)文(wén)章(zhāng)内容需要先後對iwms_news和(hé)iwms_pages兩個(gè)表内容進行(xíng)替換操作。
深圳市南山區南山街(jiē)道南海(hǎi)大(dà)道西(xī)桂廟路(lù)北陽光(guāng)華藝大(dà)廈1棟4F、4G-04
咨詢電話(huà):136 8237 6272
大(dà)客戶咨詢:139 0290 5075
業(yè)務QQ:195006118
技術(shù)QQ:179981967