【求助,关于MSSQL中的数据操作?】
UPDATETEXT和WRITETEXT分别用来更新、替换和重写ntext、text、image字段的值。
对于varchar和nvarchar等字符型字段我们可以使用:
UPDATE 表名 SET 字段名=字段名+'追加字符串'
方式更新字段。这条语句却不能用来更新ntext、text以及image字段,因为它们存储的是指针而并不是实际内容。
--ntext字段的插入处理
--创建数据测试环境
create table tblTest(id varchar(3),txt ntext)
insert into tblTest
select '001','你好,忧脚本'
go
--定义插入的字符串
declare @add_str varchar(100)
select @add_str='无' --要插入的字符串
--字符串插入处理
declare @position varbinary(16),@txtPos int
select @position=textptr(txt) --position为列txt的文本指针值,注意testptr函数
,@txtPos=charindex(',',txt) --插入的位置,null 加在尾部,0 加在首部,其他值则为指定位置
from tblTest where id='001'
if @txtPos>0
begin
updatetext tblTest.txt
@position @txtPos 0 @add_str
select * from tblTest
end