在 text 或 ntext 数据类型上替换 REPLACE
我需要更新/替换 datatable.column 中的数据.该表有一个名为 Content
的字段.我正在使用 REPLACE
函数.由于列数据类型是 NTEXT
,SQL Server 不允许我使用 REPLACE
函数.
I need to update/replace the data in datatable.column. The table has a field named Content
. I'm using the REPLACE
function. Since the column datatype is NTEXT
, SQL Server doesn't allow me to use the REPLACE
function.
我无法更改数据类型,因为此数据库是 3rd 方软件表.更改数据类型将导致应用程序失败.
I can't change the datatype because this database is 3rd party software table. Changing the datatype will cause the application to fail.
UPDATE [CMS_DB_test].[dbo].[cms_HtmlText]
SET Content = REPLACE(Content,'ABC','DEF')
WHERE Content LIKE '%ABC%'
我收到此错误:
消息 8116,级别 16,状态 1,第 1 行参数数据类型 ntext 对于替换函数的参数 1 无效.
Msg 8116, Level 16, State 1, Line 1 Argument data type ntext is invalid for argument 1 of replace function.
- 我可以用 T-SQL 解决这个问题吗?有人有如何阅读和循环的示例吗?
- 由于这是一次性转换,也许我可以更改为另一种类型,但我担心我会弄乱数据.
有一个主键字段:name: ID - integer - 它是一个身份....所以我也需要考虑这个.也许将 Identity 设置为 N 临时.
There is a primary key field: name: ID - integer - it's an identity.... So I need to think about this too. Maybe set the Identity to N temporary.
请教如何实现REPLACE功能?
Please advise on how to achieve the REPLACE function?
大约3000 条语句需要使用新解决方案进行更新.
Approx. 3000 statements need to be updated with a new solution.
推荐答案
IF您的数据不会溢出 4000 个字符并且您使用的是 SQL Server 2000 或兼容性8 级或 SQL Server 2000:
IF your data won't overflow 4000 characters AND you're on SQL Server 2000 or compatibility level of 8 or SQL Server 2000:
UPDATE [CMS_DB_test].[dbo].[cms_HtmlText]
SET Content = CAST(REPLACE(CAST(Content as NVarchar(4000)),'ABC','DEF') AS NText)
WHERE Content LIKE '%ABC%'
对于 SQL Server 2005+:
For SQL Server 2005+:
UPDATE [CMS_DB_test].[dbo].[cms_HtmlText]
SET Content = CAST(REPLACE(CAST(Content as NVarchar(MAX)),'ABC','DEF') AS NText)
WHERE Content LIKE '%ABC%'
相关文章