日期:2014-05-18  浏览次数:20445 次

如何根据字段内容里面包含的信息修改? 没有成功
content字段是text的,所以按照各位大的结果是函数   replace   的参数   1   的数据类型   text   无效。而我不能改字段,因为确实有大于8000的内容。怎么解决?急
源问题:数据表名称:ywwyindex
里面有个字段:content
我想把这个字段的内容里包含cellpadding= "5 "的,内容其他部分不变,只是修改这一句为cellpadding= "0 "
如何根据字段内容里面包含的信息修改?谢谢
注意content是text的

------解决方案--------------------
content字段是text的,所以按照各位大的结果是函数 replace 的参数 1 的数据类型 text 无效。而我不能改字段,因为确实有大于8000的内容。怎么解决?急
源问题:数据表名称:ywwyindex
里面有个字段:content
我想把这个字段的内容里包含cellpadding= "5 "的,内容其他部分不变,只是修改这一句为cellpadding= "0 "
如何根据字段内容里面包含的信息修改?谢谢
注意content是text的

select id , content from ywwyindex
将结果存问文本文件.
在文本文件中替换你的东西.
将文本文件导入到SQL中,如A表.
然后
update ywwyindex
set content = a.content
from ywwyindex,a
where ywwyindex.id = a.id

------解决方案--------------------
--創建測試環境
Create Table ywwyindex
(ID Int,
content Text)
--插入數據
Insert ywwyindex Select 1, 'cellpadding= "0 "dasd '
Union All Select 2, 'cellpadding= "5 "dsa '
Union All Select 3, 'cellpadding= "5 "dsdasa '
Union All Select 4, 'cellpadding= "2 " '
Union All Select 5, 'cellpadding= "3 " '
GO
--測試
Declare @S1 Varchar(8000), @S2 Varchar(8000)
Select @S1 = 'cellpadding= "5 " ' --要替换的字符串
Select @S2 = 'cellpadding= "0 " ' --替换後的字符串
Declare @ID Int
Declare Cursor_ID Cursor For Select ID From ywwyindex
Open Cursor_ID
Fetch Next From Cursor_ID Into @ID
While @@Fetch_Status = 0
Begin
Declare @P Varbinary(16), @Postion Int, @Len Int
Select @P = TextPTR(content), @Len = Len(@S1), @Postion = CharIndex(@S1, content) - 1 From ywwyindex Where ID = @ID
While @Postion > = 0
Begin
UpdateText ywwyindex.content @P @Postion @Len @S2
Select @Postion = CharIndex(@S1, content) - 1 From ywwyindex Where ID = @ID
End
Fetch Next From Cursor_ID Into @ID
End
Close Cursor_ID
Deallocate Cursor_ID

Select * From ywwyindex
GO
--刪除測試環境
Drop Table ywwyindex
--結果
/*
ID content
1 cellpadding= "0 "dasd
2 cellpadding= "0 "dsa
3 cellpadding= "0 "dsdasa
4 cellpadding= "2 "
5 cellpadding= "3 "
*/
------解决方案--------------------
paoluo(一天到晚游泳的鱼)大哥
(呵呵,可以这样叫的不,要是你年龄够就按年龄了,年龄不够就按你的技术叫的.^-^)
--好像犯了个越注释越不明白的bug
--你技术高,人又热心.按说 "三人行必有我师 "我应该尊称你一声老师.
--那样你说话的时候我就不能插嘴了.不好.呵呵

楼主的表 content字段是text类型的,看样子他存的是个网页.应该会有汉字(双字节字符)
这样的话.按照你上面的方法.会出现定位不准的问题的.
当然是要楼主的content字段里放的全是单字节字符.
就可以直接用你上面的方法的.

呵呵
------解决方案--------------------

--創建測試環境
Create Table ywwyindex
(ID Int,
content NText)
--插入數據
Insert ywwyindex Select 1, N 'cellpadding= "0 "dasd '
Union All Select 2, N '打算的撒cellpadding= "5 "大隊 '
Union All Select 3, N '单字节cellpadding= "5 "中國 '
Union All Select 4, N '打電話cellpadding= "2 " '
Union All Select 5, N 'cellpadding= "3 " '
GO
--測試
Declare @S1 Varchar(8000), @S2 Varchar(8000)
Select @S1 = 'cellpadding= "5 " '--要替换的字符串
Select @S2 = 'cellpadding= "0 " '--替换後的字符串
Declare @ID Int
Declare Cursor_ID Cursor For Select ID From ywwyindex