中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 数据库开发 > 数据库综合
模拟字符串处理函数 stuff 处理 Ntext 字段
作者:未知 时间:2004-07-16 12:12 出处:Blog 责编:chinaitpower
              摘要:暂无

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

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有