[MSSQLServer]Text类型的字符串替换
2005-08-02 11:08
309 查看
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都好,一样均可通用
从上文可以看到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都好,一样均可通用
相关文章推荐
- SQL Server TEXT类型字段字符串替换示例处理脚本
- SQL Server TEXT类型字段字符串替换示例处理脚本
- TextView中使用一个字符串替换字符串str中start到end的子字符串
- MSSQLSERVER数据库- 字符串分割函数返回类型表
- SqlServer替换text类型数据解决方案
- textfield/textview的子类,实现功能:几种不同类型的最大输入字符串字数限制,剩余可输入字符数,emoji表情过滤
- Linux批量替换某种类型文件中的字符串-sed和grep命令使用
- getkeynametext(键盘消息类型,指针,长度)返回字符串
- 批量替换ntext字段内容,@textA为要替换的字符串,@textB为替换后的字符串
- sql server text类型替换
- 在SQL中参数值超过8000(TEXT类型)的处理方法,以"逗号"分隔的字符串
- AJAX第一步:AJAX接收返回类型为text/html的字符串数据
- Sqlserver 如何修改替换text,ntext类型字段
- TEXT字段类型比较,查找,替换
- MySQL数据库替换longtext类型字段的某个词
- 网上转来的,sql server设置同步后,text类型字段只能保存长度少于65536的字符串
- SQL Server中TEXT类型字段值在数据库中追加字符串方法
- Sql Server关于text类型的替换
- C++中 替换string类型字符串中汉字的问题
- (7)字符串类型:varchar、char、text、enum和set(不常用)