澳门新葡亰平台游戏网站sql ntext替换与批量替换方法

一款mssql server 的ntext字段里面数据替换ntext长度可变的 Unicode
数据,最大长度为 2^30 – 1 (1,073,741,823)
个字符。存储大小是所输入字符个数的两倍。ntext 的 ISO 同义词为 national
text。他和text不同

—ntext数据类型字符替换

create table tt ( sid INT IDENTITY(1,1), cont ntext ) go

insert into tt(cont) values(N’fd sad fdsa 涂聚文工团
缔友计算机信息技术有限公司 可能性 桔柑 ‘) go update tt set cont=’fd
sad fdsa 涂聚文工团 可能性 缔友计算机信息技术有限公司 桔柑 ‘ where
sid=1

–去空格 EXEC sp_dboption ‘pubs’, ‘select into/bulkcopy’, ‘true’
–sp_dboption ‘pubs’, ‘select into/bulkcopy’, ‘true’ GO DECLARE
@ptrval binary(16),@i int,@k int,@f int select @i=datalength(cont)
from tt where sid=1 set @k=1 while @i1 begin select
@f=CHARINDEX(SPACE(1),cont) from tt where sid=1 —SPACE(1) set
@f=@f-1 SELECT @ptrval = TEXTPTR(cont) FROM tt WHERE sid=1 if @f0
UPDATETEXT tt.cont @ptrval @f 1 null if @k=@i break else set @k=@k+1
continue end GO –sp_dboption ‘pubs’, ‘select into/bulkcopy’, ‘false’
EXEC sp_dboption ‘pubs’, ‘select into/bulkcopy’, ‘false’ GO

SELECT * FROM tt

—换英文字母 EXEC sp_dboption ‘pubs’, ‘select into/bulkcopy’, ‘true’

GO DECLARE @ptrval binary(16),@i int,@k int,@f int select
@i=datalength(cont) from tt where sid=1 set @k=1 while @i1 begin
select @f=CHARINDEX(‘sad’,cont) from tt where sid=1 —SPACE(1) set
@f=@f-1 SELECT @ptrval = TEXTPTR(cont) FROM tt WHERE sid=1 if @f0
UPDATETEXT tt.cont @ptrval @f 3 ‘ggg’ if @k=@i break else set @k=@k+1
continue end GO SELECT * FROM tt

—中文字母 EXEC sp_dboption ‘pubs’, ‘select into/bulkcopy’, ‘true’
GO DECLARE @ptrval binary(16),@i int,@k int,@f int select
@i=datalength(cont) from tt where sid=1 set @k=1 while @i1 begin
select @f=CHARINDEX(‘涂聚文’,cont) from tt where sid=1 —SPACE(1) set
@f=@f-1 SELECT @ptrval = TEXTPTR(cont) FROM tt WHERE sid=1 if @f0
UPDATETEXT tt.cont @ptrval @f 6 ‘涂斯博’ if @k=@i break else set
@k=@k+1 continue end GO SELECT * FROM tt

EXEC sp_dboption ‘pubs’, ‘select into/bulkcopy’, ‘true’ GO DECLARE
@ptrval binary(16),@i int,@k int,@f int,@change nvarchar(20),@newstr
nvarchar(20),@len int set @change=N’sad’ –要替换的字符 –set
@len=datalength(@change) set @len=len(@change) set @newstr=N’ggg’
–替换成的字符 select @i=datalength(cont) from tt where sid=1 set @k=1
while @i1 begin select @f=CHARINDEX(@change,cont) from tt where sid=1
—SPACE(1) set @f=@f-1 SELECT @ptrval = TEXTPTR(cont) FROM tt WHERE
sid=1 if @f0 UPDATETEXT tt.cont @ptrval @f @len @newstr if @k=@i break
else set @k=@k+1 continue end GO SELECT * FROM tt

EXEC sp_dboption ‘pubs’, ‘select into/bulkcopy’, ‘true’ GO DECLARE
@ptrval binary(16),@i int,@k int,@f int,@change nvarchar(20),@newstr
nvarchar(20),@len int set @change=N’涂聚文’ –要替换的字符 –set
@len=datalength(@change) –用此会出错 set @len=len(@change) set
@newstr=N’涂斯博’ –替换成的字符 select @i=datalength(cont) from tt
where sid=1 set @k=1 while @i1 begin select @f=CHARINDEX(@change,cont)
from tt where sid=1 —SPACE(1) set @f=@f-1 SELECT @ptrval =
TEXTPTR(cont) FROM tt WHERE sid=1 if @f0 UPDATETEXT tt.cont @ptrval @f
@len @newstr if @k=@i break else set @k=@k+1 continue end GO SELECT *
FROM tt

update tt set cont=’fd sad fdsa 涂聚文工团 可能性
缔友计算机信息技术有限公司 桔柑 ‘ where sid=1 select
datalength(‘涂聚文’) select len(‘涂聚文’) select datalength(‘sad’)
select len(‘sad’) select len(SPACE(1))

EXEC sp_dboption ‘pubs’, ‘select into/bulkcopy’, ‘true’ DECLARE
@ptrval binary(16),@i int,@k int,@f int,@change nvarchar(20),@newstr
nvarchar(20),@len int set @change=N’涂聚文’ –要替换的字符 –set
@len=datalength(@change) –用此会出错 set @len=len(@change) set
@newstr=N’涂斯博’ –替换成的字符 select @i=datalength(cont) from tt
where sid=1 set @k=1 while @i1 begin select @f=CHARINDEX(@change,cont)
from tt where sid=1 —SPACE(1) set @f=@f-1 SELECT @ptrval =
TEXTPTR(cont) FROM tt WHERE sid=1 if @f0 UPDATETEXT tt.cont @ptrval @f
@len @newstr if @k=@i break else set @k=@k+1 continue end GO

EXEC sp_dboption ‘pubs’, ‘select into/bulkcopy’, ‘true’ DECLARE
@ptrval binary(16),@i int,@k int,@f int,@change nvarchar(20),@newstr
nvarchar(20),@len int set @change=N’涂聚文’ –要替换的字符 –set
@len=datalength(@change) –用此会出错 set @len=len(@change) set
@newstr=N’涂斯博’ –替换成的字符 select @i=datalength(cont) from tt
where sid=1 set @k=1 while @i1 begin select @f=CHARINDEX(@change,cont)
from tt where sid=1 —SPACE(1) set @f=@f-1 SELECT @ptrval =
TEXTPTR(cont) FROM tt WHERE sid=1 if @f0 UPDATETEXT tt.cont @ptrval @f
@len @newstr if @k=@i break else set @k=@k+1 continue end GO

sqlserver 中ntext字段的批量替换

。在Sql Server 中,ntext/text/image
字段不允许应用replace函数替换内容;
2。通过convert字段转换,可以把ntext字段转换为varchar,然后用Relpace函数替换,不过,此方法,对于字段长度大于8000的ntext字段无效

CREATE procedure [dbo].[Proc_UpdateNTextField] @TargetTable
nvarchar(1000), –目标表名 @TargetField nvarchar(1000), –目标字段名
@PKField nvarchar(1000), –该表主键字段名 @otxt nvarchar(1000),
–需要替换的字符串 @ntxt nvarchar(1000) –替换后的字符串 as begin
declare @SqlStr nvarchar(4000) set @SqlStr = ‘ declare @txtlen int ‘
set @SqlStr = @SqlStr + ‘ set @txtlen = len(”’ + @otxt + ”’) ‘ set
@SqlStr = @SqlStr + ‘ declare @pos int ‘ set @SqlStr = @SqlStr + ‘ set
@pos = 0 ‘ set @SqlStr = @SqlStr + ‘declare curs cursor local
fast_forward for select ‘ set @SqlStr = @SqlStr + @PKField + ‘ ,
textptr(‘ + @TargetField +’) from ‘ + @TargetTable +’ where ‘ +
@TargetField + ‘ like ”%’ + @otxt +’%”’ set @SqlStr = @SqlStr + ‘
declare @ptr binary(16) ‘ set @SqlStr = @SqlStr + ‘ declare @id
char(32) ‘ set @SqlStr = @SqlStr + ‘ open curs ‘ set @SqlStr = @SqlStr

  • ‘ fetch next from curs into @id, @ptr ‘ set @SqlStr = @SqlStr + ‘
    while @@fetch_status = 0 ‘ set @SqlStr = @SqlStr + ‘ begin ‘ set
    @SqlStr = @SqlStr + ‘ select @pos= patindex(”%’ + @otxt +
    ‘%”,ProductDesc) from ProductTemp where ProductID=@id ‘ set @SqlStr =
    @SqlStr + ‘ while @pos0 ‘ set @SqlStr = @SqlStr + ‘ begin ‘

set @SqlStr = @SqlStr + ‘ set @pos=@pos-1 ‘ set @SqlStr = @SqlStr + ‘
updatetext ‘ + @TargetTable + ‘.’ +@TargetField + ‘ @ptr @pos @txtlen
”’ + @ntxt + ”’ ‘ set @SqlStr = @SqlStr + ‘ select @pos=
patindex(”%’ + @otxt + ‘%”,ProductDesc) from ProductTemp where
ProductID=@id ‘ set @SqlStr = @SqlStr + ‘ end ‘ set @SqlStr = @SqlStr

  • ‘ fetch next from curs into @id, @ptr ‘ set @SqlStr = @SqlStr + ‘
    end ‘ set @SqlStr = @SqlStr + ‘ close curs ‘ set @SqlStr = @SqlStr + ‘
    deallocate curs ‘ EXECUTE sp_executesql @SqlStr end
You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图