/*--ntext字段處理
模擬字符串處理函數(shù) stuff
完成表中 ntext 字段的 stuff 處理
注意,表中需要有列名為:id 的主鍵(或標識字段),數(shù)據(jù)類型為int
如果沒有這個主鍵字段,或者是其他類型,則對應(yīng)的需要修改存儲過程
--鄒建 2004.07--*/
/*--調(diào)用示例
--測試數(shù)據(jù)
create table tb(id int identity(1,1),content ntext)
insert tb select 'a;sd'
union all select 'a;sdfkjas2qasdfdfsg45yhjhdfg45645a'
--調(diào)用存儲過程,將第8~9的字符替換成'中國'
exec p_stuff 'tb','content',8,2,'中國',''
select * from tb
drop table tb
--*/
create proc p_stuff
@tbname sysname,--要處理的表名
@fdname sysname,--text/ntext字段名
@start int=null,--開始位置,null表示追加數(shù)據(jù)
@length int=null,--替換的長度
@str nvarchar(4000),--要插入的字符串
@where nvarchar(1000)=''--要處理的記錄的條件
as
if @str is null return
declare @s nvarchar(4000)
set @s='
declare @id int,@ptr varbinary(16),@start1 int
declare tb cursor local for
select id,start=datalength(['[email protected]+'])/2
from ['[email protected]+']
'+case isnull(@where,'') when '' then ''
else ' where '[email protected] end+'
open tb
fetch tb into @id,@start1
while @@fetch_status=0
begin
select @ptr=textptr(content)
from ['[email protected]+']
where [email protected]
if @start is null or @start1<@start
updatetext ['[email protected]+'].['[email protected]+'] @ptr null null @str
else
begin
set @[email protected]
updatetext ['[email protected]+'].['[email protected]+'] @ptr @start1 @length @str
end
fetch tb into @id,@start1
end
close tb
deallocate tb
'
exec sp_executesql @s
,n'@start int,@length int,@str nvarchar(4000)'
,@start,@length,@str
go
新聞熱點
疑難解答
圖片精選