如何更新 SQL Server 2000 中的 text 或 ntext 字段
所以我需要更新一个文本字段。 当下面使用 UPDATE 语句或 WRITETEXT 语句时,
CREATE TABLE MyTable (IDField int, MyField text)
INSERT INTO MyTable (IDField) SELECT 1
DECLARE @Data1 varchar(8000), @Data2 varchar(8000), @ptrval binary(16)
SELECT @Data1 = REPLICATE('1',8000)
SELECT @Data2 = REPLICATE('2',8000)
-- this sets MyField to string of only 8000 characters
UPDATE MyTable SET MyField = @Data1 + @Data2 WHERE IDField = 1
SELECT @ptrval = TEXTPTR(MyField )
FROM MyTable
WHERE IDField = 1
-- this causes an error: Incorrect syntax near '+'.
--WRITETEXT MyTable.MyField @ptrval @Data1 + @Data2
当局部变量不能为 TEXT 类型时,我该如何执行此操作? (如果我有 SSQL Server 2005,我会使用 varchar(max) - 但我不会)
So I need to update a text field. Neither the UPDATE statement or the WRITETEXT statement work when used below
CREATE TABLE MyTable (IDField int, MyField text)
INSERT INTO MyTable (IDField) SELECT 1
DECLARE @Data1 varchar(8000), @Data2 varchar(8000), @ptrval binary(16)
SELECT @Data1 = REPLICATE('1',8000)
SELECT @Data2 = REPLICATE('2',8000)
-- this sets MyField to string of only 8000 characters
UPDATE MyTable SET MyField = @Data1 + @Data2 WHERE IDField = 1
SELECT @ptrval = TEXTPTR(MyField )
FROM MyTable
WHERE IDField = 1
-- this causes an error: Incorrect syntax near '+'.
--WRITETEXT MyTable.MyField @ptrval @Data1 + @Data2
How am I supposed to do this when local variables cannot be of type TEXT? (If I had SSQL Server 2005 I would use varchar(max) - but I don't)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用 UPDATETEXT 代替
插入偏移量是从零开始的,因此 8000 应写入第 8001 个字符。
删除偏移量为 null,因为 NULL 值将删除从 insert_offset 位置到现有文本末尾的所有数据。
参考: http://msdn.microsoft.com/en-us/library/ms189466 .aspx
不要忘记 nvarchar(您应该与 ntext 字段一起使用)的最大容量是您正在使用的 varchar 字段的一半,因此在这种情况下您的块大小需要减少到 4000。
Try using UPDATETEXT instead
The insert offset is zero based so 8000 should write into the 8001st character.
The delete offset is null as a value of NULL deletes all data from the insert_offset position to the end of the existing text.
Ref: http://msdn.microsoft.com/en-us/library/ms189466.aspx
Do not forget nvarchar (which you should use with ntext field) have a maximum capacity of half the varchar fields that you are using so your block sizes need to be reduced to 4000 in that case.
这些值的长度实际上会有所不同,所以我明天会这样尝试:
上面的方法有效,但我必须先计算长度:
不知道为什么不能在需要参数的地方使用像 LEN() 这样的函数。
the values will actually vary in length so I will try it like this tomorrow:
the above worked but I had to calculate the length first:
not sure why you can't use a function like LEN() where a parameter is expected.
我在这个问题上遇到了困难。
我试图将长字符串(实际上是富文本框内容)保存到 ntext 字段。
事实证明,解决方案相当简单。
注意:strQuestionQUESTION 约为 3000 个字符或格式化代码和文本。 “Num”只是“Test”数据库中的一个整数字段,其中还包含 ntext 字段名称“Text”
I had a hard time with this one.
I was trying to save long strings (actually rich text box contents) to a ntext feild.
The solution turned out to be fairly simple.
Note: strQuestionQUESTION was about 3000 characters or formatting code and text. "Num" is just an integer field in the "Test" database which also contain the ntext field name "Text"