Replace data in the current database in batches

Source: Internet
Author: User
Replace data in the current database in batches
---- Normal format
DECLARE @ t varchar (255), @ c varchar (255) -- DECLARE two variables
DECLARE Table_Cursor cursor for -- DECLARE a CURSOR
Select a. name, B. name
FROM sysobjects a, syscolumns B -- System Object table and field table
Where. id = B. id AND. xtype = 'U' AND (B. xtype = 99 or B. xtype = 35 or B. xtype = 231 or B. xtype = 167) -- 99 text 35 ntext -- 231 nvarchar -- 167 varchar
OPEN Table_Cursor fetch next -- OPEN the cursor
FROM Table_Cursor INTO @ T, @ c while (@ FETCH_STATUS = 0)
BEGIN
EXEC ('
Update ['+ @ T +'] set ['+ @ C +'] = replace (['+ @ C +'], ''here is the data to be replaced '', ''here is the new data '')
')
Fetch next from Table_Cursor INTO @ T, @ c end -- get the record
CLOSE Table_Cursor -- CLOSE the cursor
DEALLOCATE Table_Cursor -- release cursor

---- Ntext, text format
DECLARE @ t varchar (255), @ c varchar (255) -- DECLARE two variables
DECLARE Table_Cursor cursor for -- DECLARE a CURSOR
Select a. name, B. name
FROM sysobjects a, syscolumns B -- System Object table and field table
Where. id = B. id AND. xtype = 'U' AND (B. xtype = 99 or B. xtype = 35 or B. xtype = 231 or B. xtype = 167) -- 99 text 35 ntext -- 231 nvarchar -- 167 varchar
OPEN Table_Cursor fetch next -- OPEN the cursor
FROM Table_Cursor INTO @ T, @ c while (@ FETCH_STATUS = 0)
BEGIN
EXEC ('
Update ['+ @ T +'] set ['+ @ C +'] = replace (cast (['+ @ C +'] as varchar (8000 )), ''here is the data to be replaced '', ''here is the new data '')
')
Fetch next from Table_Cursor INTO @ T, @ c end -- get the record
CLOSE Table_Cursor -- CLOSE the cursor
DEALLOCATE Table_Cursor -- release 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.