日期:2014-05-17  浏览次数:20657 次

请教一个追加内容的语句
表名m_data,字段m_name,m_des.

如果m_data=我爱我家,就把对应的m_des这里字段里在补充这段内容<div class="name">我爱我家已经补充</div>
请问怎么实现?

------解决方案--------------------
引用:
引用:SQL code
?



123456

update m_data set m_des='<div class="name">'+m_des+'</div>'where m_name='我爱我家'  --是这么个意思么??
差不多就是这个意思,可是我的字符类型是TXT,这样过不去啊



--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


--text字段的替换处理

--创建数据测试环境
create table test(id varchar(3),txt text)
insert into test
select '001','A*B'
go

--定义替换的字符串
declare @s_str varchar(8000),@d_str varchar(8000)
select @s_str='*'         --要替换的字符串
        ,@d_str='+'                --替换成的字符串

--字符串替换处理
declare @p varbinary(16),@postion int,@rplen int
select @p=textptr(txt)
        ,@rplen=len(@s_str)
        ,@postion=charindex(@s_str,txt)-1
from test where id='001'

while @postion>0
begin
        updatetext test.txt @p @postion @rplen @d_str
        select @postion=charindex(@s_str,txt)-1 from test
end

--显示结果
select * from test

go
--删除数据测试环境
drop table test

--text字段的添加处理存储过程--全表

--创建测试表
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
        --字符串添加处理