Sql Serverのデータ型textに対する操作

4617 ワード

Sql Server     text   
 
--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
 
--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      ,   */