[求助]如何更新表中TEXT类型字段的值
<p><font face="Verdana">两个TEXT字段的内容相加</font></p><p><font face="Verdana">用WRITETEXT只能覆盖数据,</font></p>
<p><font face="Verdana">updatetext 则不存在!!</font></p>
<p> </p> <p>CSDN上看来的东东:</p>
<p>Create Table #Text_Temp(cDoAble text,cStatus Text,cEffect Text,cVote Text) <br/> Insert into #Text_Temp (cDoAble,cStatus,cEffect,cVote) Values('','1','2','3') <br/> Insert into #Text_Temp (cDoAble,cStatus,cEffect,cVote) Values('','A','B','C') <br/> Insert into #Text_Temp (cDoAble,cStatus,cEffect,cVote) Values('','I','II','III') <br/> Insert into #Text_Temp (cDoAble,cStatus,cEffect,cVote) Values('','!','@','#') <br/> <br/> --------想实现如下的语句的效果----------------------------------------- <br/> -- UPdate cDoAble With '[现状分析]'+ CHAR(13)+ <br/> -- +" "+Rtrim( cStatus )+CHAR(13)+'[目标效果分析]'+CHAR(13)+ <br/> -- +" "+Rtrim( cEffect )+CHAR(13)+'[建议及对策]'+CHAR(13)+ <br/> -- +" "+Rtrim( cVote ) </p>
<p> </p>
<p>A:</p>
<p>--如果各字段内容实际长度未超过8000,可用转换实现,如: <br/> ....................... <br/> declare @DoAble varchar(8000) <br/> Select @DoAble=isNULL(@DoAble,'')+'[现状分析]'+ CHAR(13)+ <br/> +' '+Rtrim(cast(cStatus as varchar))+CHAR(13)+'[目标效果分析]'+CHAR(13)+ <br/> +' '+Rtrim(cast(cEffect as varchar))+CHAR(13)+'[建议及对策]'+CHAR(13)+ <br/> +' '+Rtrim(cast(cVote as varchar)) <br/> from #Text_Temp <br/> print @DoAble</p>
<p>B:</p>下面的代码演示, 如果把两个 text 列相加, 更新到其中一个 text 列中 <br/> USE tempdb <br/> GO <br/> <br/> -- 测试环境 <br/> CREATE TABLE ta(id int identity(1, 1), col1 text) <br/> CREATE TABLE tb(id int identity(1, 1), col1 text) <br/> INSERT ta SELECT REPLICATE('abc', 8000) <br/> INSERT tb SELECT REPLICATE('cba', 8000) <br/> <br/> -- show insert data length <br/> SELECT = DATALENGTH(col1) FROM ta <br/> SELECT = DATALENGTH(col1) FROM tb <br/> GO <br/> <br/> -- 更新 ta, 让 ta.col1 = ta.col1 + ' ' + tb.col1 <br/> -- 条件: ta.id = 1, tb.id = 1 <br/> DECLARE @p1 binary(16), @p2 binary(16) <br/> <br/> SELECT @p1 = TEXTPTR(col1) FROM ta (NOLOCK) <br/> WHERE id = 1 <br/> IF TEXTVALID('ta.col1', @p1) = 0 <br/> BEGIN <br/> RAISERROR('ta.col1 invalid', 16, 1) <br/> RETURN <br/> END <br/> <br/> SELECT @p2 = TEXTPTR(col1) FROM tb (NOLOCK) <br/> WHERE id = 1 <br/> IF TEXTVALID('tb.col1', @p2) = 0 <br/> BEGIN <br/> RAISERROR('tb.col1 invalid', 16, 1) <br/> RETURN <br/> END <br/> <br/> -- insert space split <br/> UPDATETEXT ta.col1 @p1 NULL 0 '-' <br/> <br/> --insert tb.col1 <br/> UPDATETEXT ta.col1 @p1 NULL 0 tb.col1 @p2 <br/> GO <br/> <br/> -- show update result <br/> SELECT = SUBSTRING(col1, 1, 10) + '...' + SUBSTRING(col1, 15988, 100), <br/> = DATALENGTH(col1) <br/> FROM ta <br/> WHERE id = 1 <br/> GO <br/> <br/> DROP TABLE ta, tb <br/>
<p> </p> <p>在SYBASE环境中可以实现吗,我的text字符超过8000的,</p>
<p>sybase中UPDATETEXT 可用吗,</p>
<p> </p>
<p> </p>
页:
[1]