批量更新text字段值的内容

zkfjw 2005-12-24 02:23:49
批量更新text字段值的内容
我有一个表,想把按条件搜索出来的text字段的值在原值上追加部分内容,但我使用语句
update table set field = field + 'string' where ……
进行批量更新时说是text字段内容不允许,如果用convert来转换成字符串的话又有最大限制为8000个字符,如果用readtext和writetext的话又每次只能操作一条记录,效率太低,不知各位大虾有什么好主意能解决这个问题,有谁能帮我写成存储过程也行,我对存储过程不太了解,写了一天多也没写出来,不过如果写存储过程需要把表名、列名、追回部分的值还有查询条件做为参数传到存储过程中。请走过的路过的多进来看看,站一分钟有一钟的好处,停一分钟有一分钟的收获…………
...全文
238 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
funsuzhou 2005-12-26
  • 打赏
  • 举报
回复
--用游标试试:
--假设表的主键为colA和colB
CREATE PROCEDURE sp_AddToText AS
declare cur_GetRecords cursor local for
select colA,colB from 表 where ......
declare @ptrval binary(16)
declare @colA ...... --colA的类型
declare @colB ...... --colB的类型
Open cur_GetRecords
Fetch next from cur_GetRecords into @colA,@colB
while @@Fetch_Status=0
begin
select @ptrval=textptr(text字段) from 表 where colA=@colA and colB=@colB
updatetext 表.field @ptrval null 0 '你要添加的文本'
Fetch next from cur_GetRecords into @colA,@colB
end
Close cur_GetRecords
Deallocate cur_GetRecords
GO
lsqkeke 2005-12-26
  • 打赏
  • 举报
回复
我测试的表结构为:
create table pcc(field text)
lsqkeke 2005-12-26
  • 打赏
  • 举报
回复

--写的存储过程
create proc te
as
declare @p binary(16)
declare tb cursor local
for
select textptr(field) from pcc
open tb
fetch tb into @p
while @@fetch_status=0
begin
if textvalid('pcc.field',@p)=1
updatetext pcc.field @p null 0 'string'
fetch tb into @p
end

--执行存储过程,得结果
exec te
select * from pcc
lsqkeke 2005-12-26
  • 打赏
  • 举报
回复
我怎么写出来的存储过程发不出去,说什么不要发表非法言论 郁闷啊
新鲜鱼排 2005-12-26
  • 打赏
  • 举报
回复
感觉应该不困难吧。
xueguang 2005-12-26
  • 打赏
  • 举报
回复
--通过一个临时表处理一下
declare @id int
declare @ptrval binary(16)
set @id=1
select id=identity(int,1,1),field into #1 from [table] where ...

while @id<=(select max(id) from #1) begin
select @ptrval=textptr(field) from #1 where id=@id
updatetext #1.field @ptrval null 0 'string'
set @id=@id+1
end

delete [table] where ...
insert [table] select * from #1
zkfjw 2005-12-26
  • 打赏
  • 举报
回复
在存储过程中定义text或ntext时提示数据类型无效
zkfjw 2005-12-25
  • 打赏
  • 举报
回复
表名和字段名可以做参数传进来吗?
浩方软件HFWMS 2005-12-24
  • 打赏
  • 举报
回复


declare @ss ntext
select @ss=field from table where .....
set @ss=@ss+'string'
update table set field = @ss + 'string' where ……
浩方软件HFWMS 2005-12-24
  • 打赏
  • 举报
回复
先读出来再写进去看看或者是先删除后insert
浩方软件HFWMS 2005-12-24
  • 打赏
  • 举报
回复
sf

34,590

社区成员

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

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