如何用SQL语句添加TEXT 字段的内容?????

zqy29 2003-12-06 04:17:59
我表里有一个字段是TEXT 类型的我类似以下语名增加TEXT 字段的内容请问该怎么写?


SELECT ‘ABC’ + 字段名 FROM 表名

当然这句是不对的,请问该如何改正?????
...全文
961 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2003-12-06
  • 打赏
  • 举报
回复
如果只是查询,是不能直接相加的,只能将text字段截取一部分来显示.

就是:victorycyz(中海,干活去,别在CSDN玩耍!) 的
victorycyz 2003-12-06
  • 打赏
  • 举报
回复


如果要更新字段:

update tablename set text字段名='abc'+cast(text字段名 as varchar(8000)) where ...
victorycyz 2003-12-06
  • 打赏
  • 举报
回复

SET TEXTSIZE 8000
SELECT 'abc'+cast(text字段名 as varchar(8000)) as A
FROM tablename T
wzh1215 2003-12-06
  • 打赏
  • 举报
回复
如果要换text字段中的字符串:如:'fag'
create table #qq(aa text)
insert #qq values('asdfag')
declare @ptrvar binary(16)
declare @sql varchar(10)
declare @insertpos int
declare @deletelen int
set @sql='fag'
select @ptrvar=textptr(aa),@insertpos=(patindex('%fag%',aa)),@deletelen=len(@sql) from #qq
updatetext #qq.aa @ptrvar @insertpos @deletelen with log '33333333333'
select * from #qq
zjcxc 元老 2003-12-06
  • 打赏
  • 举报
回复 1
text字段如果要替换/添加其值,必须用updatetext来实现.

上面的三个例子.
第一个是对指定记录的字段添加内容

第二个例子是对整个表的字段添加内容:用存储过程的方法实现

第二个例子是对整个表的字段添加内容:直接在查询分析器中处理
zjcxc 元老 2003-12-06
  • 打赏
  • 举报
回复
--text字段增加处理,表中的每个字段都增加

--创建测试表
create table test(id varchar(3),detail text)
insert into test
select '001','A*B'
union all select '002','AA*BB'

--定义添加的的字符串
declare @s_str varchar(8000),@postion int
select @s_str='*C' --要添加的字符串
,@postion=null --追加的位置,null 加在尾部,0 加在首部,其他值则加在指定位置

--定义游标,循环处理数据
declare @id varchar(3)
declare #tb cursor for select id from test
open #tb
fetch next from #tb into @id
while @@fetch_status=0
begin
--字符串添加处理
declare @p varbinary(16)
select @p=textptr(detail) from test where id=@id
updatetext test.detail @p @postion 0 @s_str
fetch next from #tb into @id
end
close #tb
deallocate #tb

--显示处理结果
select * from test
go

--删除测试表
drop table test

zjcxc 元老 2003-12-06
  • 打赏
  • 举报
回复
--下面是数据测试

--创建测试表
create table [user](uid int,UserLog text)
create table [order](uid int,state bit)

insert into [user]
select 1,'a'
union all select 2,'b'
union all select 3,'c'

insert into [order]
select 1,1
union all select 2,0
union all select 3,1
go

--处理的存储过程
CREATE PROCEDURE spUpdateUserLog
@StrLog text,
@State int
AS
--定义游标,循环处理数据
declare @uid int
declare #tb cursor for select a.uid from [user] a join [order] b on a.uid=b.uid
where state=@state

open #tb
fetch next from #tb into @uid
while @@fetch_status=0
begin
--字符串添加处理
declare @p varbinary(16)
select @p=textptr(UserLog) from [user] where uid=@uid
updatetext [user].UserLog @p null 0 @StrLog
fetch next from #tb into @uid
end
close #tb
deallocate #tb
go

--调用示例:
exec spUpdateUserLog '123',1

--显示处理结果
select * from [user]

go

--删除测试环境
drop table [user],[order]
drop proc spUpdateUserLog

/*--测试结果

uid UserLog
----------- ----------
1 a123
2 b
3 c123

(所影响的行数为 3 行)
--*/
zjcxc 元老 2003-12-06
  • 打赏
  • 举报
回复
--text字段增加处理

--创建测试表
create table test(id varchar(3),detail text)
insert into test
select '001','A*B'

--定义添加的的字符串
declare @s_str varchar(8000),@postion int
select @s_str='*C' --要添加的字符串
,@postion=null --追加的位置,null 加在尾部,0 加在首部,其他值则加在指定位置

--字符串添加处理
declare @p varbinary(16)
select @p=textptr(detail) from test where id='001'
updatetext test.detail @p @postion 0 @s_str

--显示处理结果
select * from test
go

--删除测试表
drop table test

hiyo 2003-12-06
  • 打赏
  • 举报
回复
使用如下语句试试:
SELECT ‘ABC’ + 字段名 as 别名 FROM 表名
lynx1111 2003-12-06
  • 打赏
  • 举报
回复
SELECT ‘ABC’ + cast(字段名 as varchar(8000)) FROM 表名
wzh1215 2003-12-06
  • 打赏
  • 举报
回复

create table #qq(aa text)
insert #qq values('asdfag')
select * from #qq
update #qq
set aa= 'asdf'+convert(varchar(1000),aa)

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧