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