转至繁体中文版     | 网站首页 | 文章中心 | 下载中心 | 图片中心 | 笑话频道 | 教程频道 | 会员中心 | 雁过留声 | 
最新公告:     "MinTao学以致用网"欢迎您的光临,你的支持便是我们的动力,欢迎广大网友和各界人士亲临指导,你们的一个小小的建议便是我们发展的开路石!  [MinTao  2007年9月5日]        
您现在的位置: MinTao学以致用网 >> 文章中心 >> 电子课堂 >> 数据库 >> Sql Server >> 文章正文
专题栏目
更多内容
最新推荐 更多内容
相关文章
SQL语言快速入门之一
SQL语言快速入门之二
SQL语言快速入门之三(一
SQL7.0储存过程调试
通过HTTP访问SQL Server
SQL Server的存储过程调
SQL Server安全性简介
定制PB与SQL Anywhere的
全面接触SQL语法
SQL 语法参考手册
更多内容
[MSSQLServer]Text类型的字符串替换         
[MSSQLServer]Text类型的字符串替换
作者:c_h_r 文章来源:不详 点击数: 更新时间:2007-11-14 13:08:21

ntext, text, and image data types will be removed in a future version of MicrosoftSQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead

从上文可以看到text,ntext等类型将会被ms sqlserver抛弃,取而代之的是varchar(max)等.
预计也将会取消专门针对text等类型的操作函数,例如textptr,updatetext等。

但是目前有许多现存系统仍然存在text类型的字段,因为种种原因已经不能修改数据库结构。
但是我们可以在新写的sql语句及存储过程中采用新的方法,以备将来mssql server抛弃专门针对text等类型的操作函数后修改程序的麻烦。
下面是一个简单的替换例子,

针对text类型的字符串替换:

设有表 T(id int not null,info text)
要求替换info中的''''abc''''为''''123''''
一般的存储过程会写成:
drop procedure dbo.procedure_1
go
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
create procedure dbo.procedure_1
as
declare @ptr varbinary(16)
declare @ID int
declare @Position int,@len int
declare @strsrc char(3)
declare @strdsc char(3)
set @strtmp=''''abc''''
set @strdsc=''''123''''
set @len=3
declare replace_Cursor scroll Cursor
for
select textptr([info]),id from T
for read only
open replace_Cursor
fetch next from replace_Cursor into @ptr,@ID
while @@fetch_status=0
begin
    select @Position=patindex(''''%''''+@strsrc+''''%'''',[info]) from T where id=@ID
    while @Position>0
    begin
        set @Position=@Position-1
        updatetext T.[info] @ptr @Position @len @strdsc
      select @Position=patindex(''''%''''+@strsrc+''''%'''',[info]) from T where id=@ID
    end
    fetch next from replace_Cursor into @ptr,@ID
end
close replace_Cursor
deallocate replace_Cursor
go

其中用到了text专用的函数 updatetext

现在我们改写成
drop procedure dbo.procedure_1
go

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

create procedure dbo.procedure_1
as

declare @ID int
declare @strtmp varchar(max)
declare @strsrc char(3),@strdsc char(3)
set @strsrc = ''''abc''''
set @strdsc = ''''123''''
declare replace_Cursor scroll Cursor
for
select id from testtable
--for read only
open replace_Cursor
fetch next from replace_Cursor into @ID
while @@fetch_status=0
begin
    select @strtmp = [info] from testtable where id=@ID
    select @strtmp = Replace(@strtmp,@strsrc,@strdsc)
    update T set [info] = @strtmp where id=@ID
    fetch next from replace_Cursor into @ID
end
close replace_Cursor
deallocate replace_Cursor
go

这样,无论info字段改成char,nchar,text都好,一样均可通用

文章录入:mintao    责任编辑:mintao 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007 MinTao学以致用网(www.mintao.net) Inc All Rights Reserved.
    QQ:543098146有事请Q我! QQ:261561092有事请Q我 QQ:179647303有事请Q我 MSN:min906@126.com
    站长:MinTao 信息产业部ICP备案号:鄂ICP备07500065号

    学以致用是我们学习者的至高境界和不懈追求,[MinTao学以致用网]与大家共同学习,共同进步……
    信息产业部备案
    *鄂ICP备07500065号