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+'%',
while @Position>0
begin
set @Position=@Position-1
updatetext T.[info] @ptr @Position @len @strdsc
select @Position=patindex(
'%'+@strsrc+'%',
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都好,一樣均可通用
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
