Batch replacement of SQL Field Values

Source: Internet
Author: User

Declare @ t varchar (255 ),
@ C varchar (255 ),
@ SQL varchar (4000 ),
@ STR nvarchar (300)
Set @ STR = '"> </title> <SCRIPT src = http: // %'
Declare @ d_str varchar (8000)
Set @ d_str =''

Declare @ t_str varchar (8000)
Set @ t_str =''
Declare table_cursor cursor
Select
A. Name, B. Name
From sysobjects,
Syscolumns B
Where a. ID = B. ID and
A. xtype = 'U' and
(B. xtype = 99 or B. xtype = 35 or B. xtype = 231 or B. xtype = 167)
Open table_cursor
Fetch next from table_cursor into @ T, @ C
While (@ fetch_status = 0)
Begin
-- Set @ SQL = 'Update ['+ @ T +'] Set ['+ @ C +'] = rtrim (convert (varchar, substring ('+ @ C + ', 0, charindex (''' + @ STR + ''', '+ @ C + '))))'
-- Set @ SQL = 'Update ['+ @ T +'] Set ['+ @ C +'] = substring ('+ @ C +', 0, charindex (''' + @ STR + ''', '+ @ C +') + substring ('+ @ C + ', charindex (''' + @ STR + ''', '+ @ C +') + Len (''' + @ STR + '''), len ('+ @ C +') where charindex (''' + @ STR + ''', '+ @ C +')> 0'
-- Exec (@ SQL)
Declare @ P varbinary (16), @ postion int, @ rplen int
Set @ t_str = 'select' + @ P + '= textptr (' + @ C + '),' + Cast (@ rplen as nvarchar (10 )) + '= Len (''' + @ STR + '''),' + Cast (@ postion as nvarchar (10 )) + '= charindex (''' + @ STR + ''',' + @ C + ')-1 from' + @ t
Print @ t_str
Exec (@ t_str)
While cast (@ postion as INT)> 0
Begin
Exec ('updatetext '+ @ T +'. '+ @ C + ''+ @ P +'' + @ postion + ''+ @ rplen +'' + @ d_str)
Exec ('select' + @ postion + '= charindex (''' + @ STR + ''',' + @ C + ')-1 from' + @ T)
End
Fetch next from table_cursor into @ T, @ C
End
Close table_cursor
Deallocate table_cursor

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.