模拟字符串处理函数 stuff 的存储过程,对 ntext 字段进行stuff

zjcxc 2004-07-07 09:10:35
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_stuff]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_stuff]
GO

/*--Ntext字段处理

模拟字符串处理函数 stuff
完成表中 ntext 字段的 stuff 处理

--邹建 2004.07--*/

/*--调用示例

--测试数据
create table tb(id int identity(1,1),content ntext)
insert tb select 'a;sd'
union all select 'a;sdfkjas2qasdfdfsg45yhjhdfg45645a'

--调用存储过程,将第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表示追加数据
@length int=null, --替换的长度
@str nvarchar(4000),--要插入的字符串
@where nvarchar(1000)=''--要处理的记录的条件
as
if isnull(@str,'')='' return
declare @s nvarchar(4000)
set @s='
declare @id int,@ptr varbinary(16),@start1 int

declare tb cursor local for
select id,start=datalength(['+@fdname+'])/2
from ['+@tbname+']
'+case isnull(@where,'') when '' then ''
else ' where '+@where end+'

open tb
fetch tb into @id,@start1
while @@fetch_status=0
begin
select @ptr=textptr(content)
from ['+@tbname+']
where id=@id

if @start is null or @start1<@start
updatetext ['+@tbname+'].['+@fdname+'] @ptr null null @str
else
begin
set @start1=@start-1
updatetext ['+@tbname+'].['+@fdname+'] @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
...全文
360 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
pclogic 2004-12-10
  • 打赏
  • 举报
回复
mark
newabby 2004-11-17
  • 打赏
  • 举报
回复
mark
swellyu 2004-08-22
  • 打赏
  • 举报
回复
OK!!
qing205 2004-08-03
  • 打赏
  • 举报
回复
mark
outwindows 2004-07-14
  • 打赏
  • 举报
回复
需求还是有的...
mark...
君宽 2004-07-14
  • 打赏
  • 举报
回复
太棒了,终于找到这个存储过程了,谢谢!!!!!
zheninchangjiang 2004-07-14
  • 打赏
  • 举报
回复
mark,再顶
zjcxc 2004-07-14
  • 打赏
  • 举报
回复
--调整一下,看看大家还有什么补充.

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_stuff]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_stuff]
GO

/*--Ntext字段处理

模拟字符串处理函数 stuff
完成表中 ntext 字段的 stuff 处理
注意,表中需要有列名为:id 的主键(或标识字段),数据类型为int
如果没有这个主键字段,或者是其他类型,则对应的需要修改存储过程

--邹建 2004.07--*/

/*--调用示例

--测试数据
create table tb(id int identity(1,1),content ntext)
insert tb select 'a;sd'
union all select 'a;sdfkjas2qasdfdfsg45yhjhdfg45645a'

--调用存储过程,将第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表示追加数据
@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(['+@fdname+'])/2
from ['+@tbname+']
'+case isnull(@where,'') when '' then ''
else ' where '+@where end+'

open tb
fetch tb into @id,@start1
while @@fetch_status=0
begin
select @ptr=textptr(content)
from ['+@tbname+']
where id=@id

if @start is null or @start1<@start
updatetext ['+@tbname+'].['+@fdname+'] @ptr null null @str
else
begin
set @start1=@start-1
updatetext ['+@tbname+'].['+@fdname+'] @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
770910 2004-07-14
  • 打赏
  • 举报
回复
有的
zjcxc 2004-07-14
  • 打赏
  • 举报
回复
1.if isnull(@str,'')='' return --如果需要删除某个位置的几个字符,不就实现不了吗?

这点没有考虑,那就改这句为:
if @str is null return


2.你用到了唯一字段ID,不一定所有表都有,也许有的叫fid呢?

这个不做考虑,表中必须有主键,如果连主键字段都要动态的话,得解决定义变量时,主键字段的
类型问题,虽然可以从系统表中获得,但还是麻烦,不如统一规定有个列名为id的标识字段
seekmoon 2004-07-14
  • 打赏
  • 举报
回复
good
skyboy0720 2004-07-14
  • 打赏
  • 举报
回复
哎呀,太好了,一直因为text不能象varchar处理,而郁闷!收藏!!1
evafly920 2004-07-14
  • 打赏
  • 举报
回复
GOOD
zlp321002 2004-07-14
  • 打赏
  • 举报
回复
好...Good,大师又有新作品,不得不收藏!!
i9988 2004-07-14
  • 打赏
  • 举报
回复
学习

收藏

提个意见,分太少
xhh_88 2004-07-14
  • 打赏
  • 举报
回复

1.if isnull(@str,'')='' return --如果需要删除某个位置的几个字符,不就实现不了吗?
2.你用到了唯一字段ID,不一定所有表都有,也许有的叫fid呢?
qiliu 2004-07-14
  • 打赏
  • 举报
回复
佩服
收藏了
flyincs 2004-07-13
  • 打赏
  • 举报
回复
mark
okmiddeok 2004-07-13
  • 打赏
  • 举报
回复
大师的帮顶,以后要照顾小弟呀!!!
zjcxc 2004-07-13
  • 打赏
  • 举报
回复
没人有这方面的需求?
加载更多回复(1)

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧