日期:2014-05-18 浏览次数:20806 次
create table tbtext(id int,name text) insert tbtext select 1,'a' DECLARE @ptrval binary(16),@len int SELECT @ptrval = TEXTPTR(name) FROM tbtext where id=1 UPDATETEXT tbtext.name @ptrval 1 null 'b'; select * from tbtext
------解决方案--------------------
麻烦大了,下面引用邹建写的:
--下面的代码演示, 如果把两个 text 列相加, 更新到其中一个 text 列中 USE tempdb GO -- 测试环境 CREATE TABLE ta(id int identity(1, 1), col1 text) CREATE TABLE tb(id int identity(1, 1), col1 text) INSERT ta SELECT REPLICATE('abc', 8000) INSERT tb SELECT REPLICATE('cba', 8000) -- show insert data length SELECT [ta.col1] = DATALENGTH(col1) FROM ta SELECT [tb.col1] = DATALENGTH(col1) FROM tb GO -- 更新 ta, 让 ta.col1 = ta.col1 + ' ' + tb.col1 -- 条件: ta.id = 1, tb.id = 1 DECLARE @p1 binary(16), @p2 binary(16) SELECT @p1 = TEXTPTR(col1) FROM ta (NOLOCK) WHERE id = 1 IF TEXTVALID('ta.col1', @p1) = 0 BEGIN RAISERROR('ta.col1 invalid', 16, 1) RETURN END SELECT @p2 = TEXTPTR(col1) FROM tb (NOLOCK) WHERE id = 1 IF TEXTVALID('tb.col1', @p2) = 0 BEGIN RAISERROR('tb.col1 invalid', 16, 1) RETURN END -- insert space split UPDATETEXT ta.col1 @p1 NULL 0 '-' --insert tb.col1 UPDATETEXT ta.col1 @p1 NULL 0 tb.col1 @p2 GO -- show update result SELECT [ta.col1] = SUBSTRING(col1, 1, 10) + '...' + SUBSTRING(col1, 15988, 100), [ta.col1_length]= DATALENGTH(col1) FROM ta WHERE id = 1 GO DROP TABLE ta, tb
------解决方案--------------------
学习了。。。
------解决方案--------------------
if object_id('tbtext') is not null drop table tbtext go create table tbtext(id int,name text) insert tbtext select 1,'a' union all select 2,'cc' DECLARE @ptrval binary(16),@len int SELECT @ptrval = TEXTPTR(name) ,@len=DATALENGTH(name) FROM tbtext where id=2 UPDATETEXT tbtext.name @ptrval @len 0 'b'; select id,convert(varchar(max),name) name from tbtext /* id name ----------- ---------------------------------------------------------------------------------------------------------------- 1 a 2 ccb (2