--text字段增加處理

--創建測試表
create table test(id varchar(3),detail text)
insert into test
select '001','A*B'

--定義添加的的字符串
declare @s_str varchar(8000),@postion int
select @s_str='*C'                 --要添加的字符串
        ,@postion=null                --追加的位置,null 加在尾部,0 加在首部,其他值則加在指定位置

--字符串添加處理
declare @p varbinary(16)
select @p=textptr(detail) from test where id='001'
updatetext test.detail @p @postion 0 @s_str

--顯示處理結果
select * from test
go

--刪除測試表
drop table test


--text字段的替換處理

--創建數據測試環境
create table test(id varchar(3),txt text)
insert into test
select '001','A*B'
go

--定義替換的字符串
declare @s_str varchar(8000),@d_str varchar(8000)
select @s_str='*'         --要替換的字符串
        ,@d_str='+'                --替換成的字符串

--字符串替換處理
declare @p varbinary(16),@postion int,@rplen int
select @p=textptr(txt)
        ,@rplen=len(@s_str)
        ,@postion=charindex(@s_str,txt)-1
from test where id='001'

while @postion>0
begin
        updatetext test.txt @p @postion @rplen @d_str
        select @postion=charindex(@s_str,txt)-1 from test
end

--顯示結果
select * from test

go
--刪除數據測試環境
drop table test

--text字段的添加處理存儲過程--全表

--創建測試表
create table [user](uid int,UserLog text)
create table [order](uid int,state bit)

insert into [user]
select 1,'a'
union all select 2,'b'
union all select 3,'c'

insert into [order]
select 1,1
union all select 2,0
union all select 3,1
go

--處理的存儲過程
CREATE PROCEDURE spUpdateUserLog
@StrLog text,
@State int
AS
--定義游標,循環處理數據
declare @uid int
declare #tb cursor for select a.uid from [user] a join [order] b on a.uid=b.uid
where state=@state

open #tb
fetch next from #tb into @uid
while @@fetch_status=0
begin
        --字符串添加處理
        declare @p varbinary(16)
        select @p=textptr(UserLog) from [user] where uid=@uid
        updatetext [user].UserLog @p null 0 @StrLog
        fetch next from #tb into @uid
end
close #tb
deallocate #tb
go

--調用示例:
exec spUpdateUserLog '123',1

--顯示處理結果
select * from [user]

go

--刪除測試環境
drop table [user],[order]
drop proc spUpdateUserLog

/*--測試結果

uid         UserLog   
----------- ----------
1           a123
2           b
3           c123

(所影響的行數為 3 行)
--*/
--text字段的替換處理--全表替換

--創建數據測試環境
create table test(id varchar(3),txt text)
insert into test
select '001','A*B'
union all select '002','A*B-AA*BB'
go

--定義替換的字符串
declare @s_str varchar(8000),@d_str varchar(8000)
select @s_str='*'         --要替換的字符串
        ,@d_str='+'                --替換成的字符串


--定義游標,循環處理數據
declare @id varchar(3)
declare #tb cursor for select id from test
open #tb
fetch next from #tb into @id
while @@fetch_status=0
begin
        --字符串替換處理
        declare @p varbinary(16),@postion int,@rplen int
        select @p=textptr(txt)
                ,@rplen=len(@s_str)
                ,@postion=charindex(@s_str,txt)-1
        from test where id=@id
       
        while @postion>0
        begin
                updatetext test.txt @p @postion @rplen @d_str
                select @postion=charindex(@s_str,txt)-1 from test where id=@id
        end

        fetch next from #tb into @id
end
close #tb
deallocate #tb

--顯示結果
select * from test

go
--刪除數據測試環境
drop table test

************************
支持text字段處理的僅有:
下面的函數和語句可以與 ntext、text 或 image 數據一起使用。
函數          語句
DATALENGTH    READTEXT
PATINDEX      SET TEXTSIZE
SUBSTRING     UPDATETEXT
TEXTPTR       WRITETEXT
TEXTVALID


1:替換

--創建數據測試環境
create table #tb(aa text)
insert into #tb select 'abc123abc123,asd'

--定義替換的字符串
declare @s_str varchar(8000),@d_str varchar(8000)
select @s_str='123' --要替換的字符串
,@d_str='000' --替換成的字符串

--字符串替換處理
declare @p varbinary(16),@postion int,@rplen int
select @p=textptr(aa),@rplen=len(@s_str),@postion=charindex(@s_str,aa)-1 from #tb
while @postion>0
begin
updatetext #tb.aa @p @postion @rplen @d_str
select @postion=charindex(@s_str,aa)-1 from #tb
end

--顯示結果
select * from #tb

--刪除數據測試環境
drop table #tb

/****************全部替換************************/
DECLARE @ptrval binary(16)
SELECT @ptrval = TEXTPTR(aa)  FROM  #tb  WHERE aa like '%數據2%'
if @ptrval is not null        -- 一定要加上此句,否則若找不到數據下一句就會報錯
UPDATETEXT #tb.aa @ptrval 0 null '數據3'


/****************在字段尾添加**********************************/
--定義添加的的字符串
declare @s_str varchar(8000)
select @s_str='*C'   --要添加的字符串
--字符串添加處理
declare @p varbinary(16),@postion int,@rplen int
select @p=textptr(detail) from test where id='001'
updatetext test.detail @p null null @s_str


總結:
1:Text字段類型不能直接用replace函數來替換,必須用updatetext
2:字段比較不能用 where 字段 = ‘某數據’,可以用like來代替
3:updatetext時,若@ptrval值為空會出錯,需注意。