我们正在使用Sybase数据库并拥有下表。
CREATE TABLE table_1
(id VARCHAR(50) NOT NULL
,data_values UNITEXT NULL
,last_modified DATETIME NULL)
我要求替换存储了UNITEXT类型的data_values列的单词。
我找到了link,但这并不能帮助我们取代这些词语。
我尝试过的查询如下。
declare @val varbinary(16)
select @val = textptr(data_values) from table_1
writetext table_1.data @val "Some text"
我必须动态生成要在data_values字段中设置的文本。文本本身将从同一列本身获取,并且必须在设置到该字段之前进行处理(几个单词更改)。
答案 0 :(得分:1)
一些背景知识:
(uni)text
的变量textptr()
函数返回varbinary(16)值,该值实际上是指向文本数据驻留在数据库中的位置的指针[在他们通常显示的手册@val = textptr(...)
中,这有点误导,因为{ {1}}不返回文本列的值,而是返回指向文本列的指针] textptr()
命令在将值写入文本列时提供了writetext
命令的替代方法; update
是记录的操作,而update
默认情况下是未记录的操作一些示例SQL(使用您的表定义):
writetext
编辑:
insert table_1 values ('1','original.old text value',getdate())
go
declare @ptr varbinary(16),
@unitext unitext,
@varchar varchar(16384)
-- obtain text pointer and unitext value for a desired row
select @ptr = textptr(data_values), -- pointer to unitext column
@unitext = data_values, -- actual unitext value
@varchar = convert(varchar(16384),data_values) -- unitext converted to character
from table_1
where id = '1'
select 'original' as 'when', @unitext as '@unitext', @varchar as '@varchar'
-- logged update of the data_values column
begin tran
update table_1
set data_values = 'update.new text value'
where id = '1'
select @unitext = data_values,
@varchar = convert(varchar(16384),data_values)
from table_1
where id = '1'
select 'after update' as 'when', @unitext as '@unitext', @varchar as '@varchar'
-- non-logged update of the data_values column
writetext table_1.data_values @ptr 'writetext.new text value'
select @unitext = data_values,
@varchar = convert(varchar(16384),data_values)
from table_1
where id = '1'
select 'after writetext' as 'when', @unitext as '@unitext', @varchar as '@varchar'
-- rollback to original value
rollback tran
select @unitext = data_values,
@varchar = convert(varchar(16384),data_values)
from table_1
where id = '1'
select 'after rollback' as 'when', @unitext as '@unitext', @varchar as '@varchar'
go
when @unitext @varchar
-----------------------------------------------------------------------
original original.old text value original.old text value
when @unitext @varchar
-----------------------------------------------------------------------
after update update.new text value update.new text value
when @unitext @varchar
-----------------------------------------------------------------------
after writetext writetext.new text value writetext.new text value
when @unitext @varchar
-----------------------------------------------------------------------
after rollback original.old text value original.old text value
变量和unitext
变量varchar
碰巧破坏了数据,则可以用varchar
替换varchar
,但最大长度需要从16384减少到8192 < / LI>