Batch replacement of ntext fields in sqlserver (updatetext usage)

Source: Internet
Author: User

I. Problem description:
1. In SQL Server, the ntext/text/image field cannot be replaced by the replace function;
2. By convert field conversion, you can convert the ntext field to varchar (8000) and replace it with the Relpace function. However, this method is not applicable to ntext fields with a field length greater than 8000.
Ii. Problem Solving
The code for sorting out common stored procedures is as follows:
Copy codeThe Code is as follows:
CREATE procedure [dbo]. [Proc_UpdateNTextField]
@ TargetTable nvarchar (1000), -- target table name
@ TargetField nvarchar (1000), -- target field name
@ PKField nvarchar (1000), -- Name of the primary key field of the table
@ Otxt nvarchar (1000), -- string to be replaced
@ Ntxt nvarchar (1000) -- replaced string
As
Begin
Declare @ SqlStr nvarchar (4000)
Set @ SqlStr = 'Clare @ txtlen int'
Set @ SqlStr = @ SqlStr + 'set @ txtlen = len (''' + @ otxt + ''')'
Set @ SqlStr = @ SqlStr + 'Clare @ pos int'
Set @ SqlStr = @ SqlStr + 'set @ pos = 0'
Set @ SqlStr = @ SqlStr + 'maid cursor local fast_forward for select'
Set @ SqlStr = @ SqlStr + @ PKField + ', textptr (' + @ TargetField + ') from '+ @ TargetTable + 'where' + @ TargetField +' like ''% '+ @ otxt +' % '''
Set @ SqlStr = @ SqlStr + 'Clare @ ptr binary (16 )'
Set @ SqlStr = @ SqlStr + 'Clare @ id char (32 )'
Set @ SqlStr = @ SqlStr + 'open curs'
Set @ SqlStr = @ SqlStr + 'fetch next from curs into @ id, @ ptr'
Set @ SqlStr = @ SqlStr + 'while @ fetch_status = 0'
Set @ SqlStr = @ SqlStr + 'begin'
Set @ SqlStr = @ SqlStr + 'select @ pos = patindex (''% '+ @ otxt +' %'', ProductDesc) from ProductTemp where ProductID = @ id'
Set @ SqlStr = @ SqlStr + 'while @ pos> 0'
Set @ SqlStr = @ SqlStr + 'begin'

Set @ SqlStr = @ SqlStr + 'set @ pos = @ pos-1'
Set @ SqlStr = @ SqlStr + 'updatetext '+ @ TargetTable +'. '+ @ TargetField +' @ ptr @ pos @ txtlen ''' + @ ntxt + ''''
Set @ SqlStr = @ SqlStr + 'select @ pos = patindex (''% '+ @ otxt +' %'', ProductDesc) from ProductTemp where ProductID = @ id'
Set @ SqlStr = @ SqlStr + 'end'
Set @ SqlStr = @ SqlStr + 'fetch next from curs into @ id, @ ptr'
Set @ SqlStr = @ SqlStr + 'end'
Set @ SqlStr = @ SqlStr + 'close curs'
Set @ SqlStr = @ SqlStr + 'destlocate curs'
EXECUTE sp_executesql @ SqlStr
End

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.