SQL ntext字段應(yīng)該如何替換呢?可以通過(guò)convert字段轉(zhuǎn)換,把SQL ntext字段轉(zhuǎn)換為varchar(8000),然后用Relpace函數(shù)替換,不過(guò),此方法,對(duì)于字段長(zhǎng)度大于8000的ntext字段無(wú)效,下面這種方法正好可以解決這個(gè)問(wèn)題,供您參考。
- CREATE procedure [dbo].[Proc_UpdateNTextField]
-
- @TargetTable nvarchar(1000), --目標(biāo)表名
- @TargetField nvarchar(1000), --目標(biāo)字段名
- @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 @pos>0 '
- 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
該文章在 2011/5/4 15:47:46 編輯過(guò)