如何更新NTEXT字段值

abcd_net 2003-11-24 11:26:50
需要将NTEXT类型字段中的某字符全部替换,在SQLServer中怎样实现?
谢谢
...全文
76 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
CrazyFor 2003-11-25
  • 打赏
  • 举报
回复
update tabel set textCol ='a...............'
zjcxc 元老 2003-11-25
  • 打赏
  • 举报
回复
--text/ntext字段的替换处理--全表替换

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

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


--定义游标,循环处理数据
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),@postion int,@rplen int
select @p=textptr(txt)
,@rplen=len(@s_str)
,@postion=charindex(@s_str,txt)-1
from test where id=@id

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

fetch next from #tb into @id
end
close #tb
deallocate #tb

--显示结果
select * from test

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

pengdali 2003-11-24
  • 打赏
  • 举报
回复
text字段的替换处理
http://expert.csdn.net/Expert/topic/2236/2236811.xml?temp=2.662295E-
pengdali 2003-11-24
  • 打赏
  • 举报
回复
/*

id content
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 asdfsdfasdfsadfsdjflkAAAAAsdjflkasjdf;lasdjf;lafsd
2 fdsgdsfgdfgsdfghrtjtyjkhkhjkhjljkAAAAAljk;jk;kl;kl;kl;

(所影响的行数为 2 行)

*/
pengdali 2003-11-24
  • 打赏
  • 举报
回复
举例:

create table test(id int identity(1,1),content ntext)
insert test values(N'asdfsdfasdfsadfsdjflk大力sdjflkasjdf;lasdjf;lafsd')
insert test values(N'fdsgdsfgdfgsdfghrtjtyjkhkhjkhjljk大力ljk;jk;kl;kl;kl;')
go

create proc 替换
@s_str nvarchar(100),
@d_str nvarchar(100)
as
declare @id int
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),@postion int,@rplen int
select @p=textptr(CONTENT),@rplen=len(@s_str),@postion=charindex(@s_str,CONTENT)-1 from test where id=@id
while @postion>0
begin
updatetext test.CONTENT @p @postion @rplen @d_str
select @postion=charindex(@s_str,content)-1 from test where id=@id
end
fetch next from #tb into @id
end
close #tb
deallocate #tb
go

exec 替换 N'大力',N'AAAAA'

go

select * from test

go
drop table test
friendliu 2003-11-24
  • 打赏
  • 举报
回复
*********************************************************************

用Text类型:

READTEXT
UPDATETEXT
WRITETEXT
--------------------
Transact-SQL 参考


READTEXT
读取 text、ntext 或 image 列中的 text、ntext 或 image 值,从指定的偏移量开始读取指定的字节数。

语法
READTEXT { table.column text_ptr offset size } [ HOLDLOCK ]

参数
table.column

是从中读取的表和列的名称。表名和列名必须符合标识符的规则。必须指定表名和列名,不过可以选择是否指定数据库名称和所有者名称。

text_ptr

有效文本指针。text_ptr 必须是 binary(16)。

offset

开始读取 text、image 或 ntext 数据之前跳过的字节数(使用 text 或 image 数据类型时)或字符数(使用 ntext 数据类型时)。使用 ntext 数据类型时,offset 是在开始读取数据前跳过的字符数。使用 text 或 image 数据类型时,offset 是在开始读取数据前跳过的字节数。

size

是要读取数据的字节数(使用 text 或 image 数据类型时)或字符数(使用 ntext 数据类型时)。如果 size 是 0,则表示读取了 4 KB 字节的数据。

HOLDLOCK

使文本值一直锁定到事务结束。其他用户可以读取该值,但是不能对其进行修改。

注释
使用 TEXTPTR 函数获得有效的 text_ptr 值。如果返回不止一行,TEXTPTR 将返回指向指定行中的 text、ntext 或 image 列的指针,或返回指向查询所返回的最后一行中的 text、ntext 或 image 列的指针。由于 TEXTPTR 返回 16 字节的二进制字符串,所以最好声明一个控制文本指针的局部变量,然后在 READTEXT 中使用该变量。有关声明局部变量的更多信息,请参见 DECLARE @local_variable。

在 SQL Server 2000 中可能存在无效的文本指针。有关 text in row 选项的更多信息,请参见 sp_tableoption。有关如何使文本指针无效的更多信息,请参见 sp_invalidate_textptr。

如果 @@TEXTSIZE 函数的值小于为 READTEXT 指定的大小,它将替代为 READTEXT 指定的大小。@@TEXTSIZE 函数是对由 SET TEXTSIZE 语句设置的返回数据字节数的限制。有关如何设置 TEXTSIZE 会话设置的更多信息,请参见 SET TEXTSIZE。

权限
READTEXT 权限默认授予对指定的表具有 SELECT 权限的用户。这些权限可在传递 SELECT 权限时传递。

示例
下例读取 pub_info 表中 pr_info 列的第 2 个至第 26 个字符。

USE pubs
GO
DECLARE @ptrval varbinary(16)
SELECT @ptrval = TEXTPTR(pr_info)
FROM pub_info pr INNER JOIN publishers p
ON pr.pub_id = p.pub_id
AND p.pub_name = 'New Moon Books'
READTEXT pub_info.pr_info @ptrval 1 25
GO


请参见

@@TEXTSIZE

UPDATETEXT

WRITETEXT

©1988-2000 Microsoft Corporation。保留所有权利。
friendliu 2003-11-24
  • 打赏
  • 举报
回复
、建立过程
CREATE PROCEDURE sp_textcopy (
@srvname varchar (30),
@login varchar (30),
@password varchar (30),
@dbname varchar (30),
@tbname varchar (30),
@colname varchar (30),
@filename varchar (30),
@whereclause varchar (40),
@direction char(1))
AS
DECLARE @exec_str varchar (255)
SELECT @exec_str =
'textcopy /S ' + @srvname +
' /U ' + @login +
' /P ' + @password +
' /D ' + @dbname +
' /T ' + @tbname +
' /C ' + @colname +
' /W "' + @whereclause +
'" /F ' + @filename +
' /' + @direction
EXEC master..xp_cmdshell @exec_str

2、建表和初始化数据
create table 表名 (编号 int,image列名 image)
go
insert 表名 values(1,0x)
insert 表名 values(2,0x)
go

3、读入
sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','I' --注意条件是 编号=1

sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','I' --注意条件是 编号=2

go

4、读出成文件
sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','O' --注意条件是 编号=1

sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','O' --注意条件是 编号=2
go

、建立过程
CREATE PROCEDURE sp_textcopy (
@srvname varchar (30),
@login varchar (30),
@password varchar (30),
@dbname varchar (30),
@tbname varchar (30),
@colname varchar (30),
@filename varchar (30),
@whereclause varchar (40),
@direction char(1))
AS
DECLARE @exec_str varchar (255)
SELECT @exec_str =
'textcopy /S ' + @srvname +
' /U ' + @login +
' /P ' + @password +
' /D ' + @dbname +
' /T ' + @tbname +
' /C ' + @colname +
' /W "' + @whereclause +
'" /F ' + @filename +
' /' + @direction
EXEC master..xp_cmdshell @exec_str

2、建表和初始化数据
create table 表名 (编号 int,image列名 image)
go
insert 表名 values(1,0x)
insert 表名 values(2,0x)
go

3、读入
sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','I' --注意条件是 编号=1

sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','I' --注意条件是 编号=2

go

4、读出成文件
sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','O' --注意条件是 编号=1

sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','O' --注意条件是 编号=2
go

34,594

社区成员

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

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