• Hi a.grigoriu,

    If you want to use the script for a single database, then create the below sp on that database and run.This will be a bit faster approach.

    ALTER PROCEDURE [dbo].[usp_ReplaceStringForSingleDB]

    (

    @FindString varchar(100),

    @ReplaceString varchar(100)

    )

    AS

    BEGIN

    SET NOCOUNT ON

    CREATE TABLE #FilteredTables(SchemaName varchar(255),TableName varchar(255),ColumnName varchar(128))

    DECLARE @SchemaName nvarchar(255), @TableName nvarchar(256), @ColumnName nvarchar(128),@cmd nvarchar(4000),@ErrorMsg varchar(4000),

    @WhrFindString varchar(100),@TableId int

    SET @TableName = ''

    SET @WhrFindString = QUOTENAME('%' + @FindString + '%','''')

    SET @cmd = 0

    -- Get all user tables

    DECLARE cur_Tables CURSOR FOR

    SELECT QUOTENAME(Schema_name(schema_id)),QUOTENAME(name), object_id FROM sys.objects WHERE type = 'U' and is_ms_shipped = 0

    OPEN cur_Tables

    FETCH NEXT FROM cur_Tables INTO @SchemaName, @TableName, @TableId

    WHILE (@@FETCH_STATUS = 0)

    BEGIN

    -- Get columns for table

    DECLARE cur_Columns CURSOR FOR

    SELECT QUOTENAME(name) FROM sys.columns WHERE object_id = @TableId AND system_type_id IN (167, 175, 231, 239, 99, 35)

    OPEN cur_Columns

    FETCH NEXT FROM cur_Columns INTO @ColumnName

    WHILE (@@FETCH_STATUS = 0)

    BEGIN

    -- Get only those tables and the particular column for which the match has found and inserts these details in temp table.

    SET @cmd = 'IF EXISTS (SELECT * FROM ' +@SchemaName+'.'+ @TableName + ' WHERE ' + @ColumnName + ' LIKE ''%' + @FindString + '%'') insert into #FilteredTables select ''' +@SchemaName+''','''+ @TableName + ''', ''' + @ColumnName + ''''

    EXEC(@cmd)

    --print @cmd

    FETCH NEXT FROM cur_Columns INTO @ColumnName

    END

    CLOSE cur_Columns

    DEALLOCATE cur_Columns

    FETCH NEXT FROM cur_Tables INTO @SchemaName, @TableName, @TableId

    END

    CLOSE cur_Tables

    DEALLOCATE cur_Tables

    set @cmd = ''

    --- Cursor for creating and runing update statements for the tables filtered above.

    DECLARE cur_Replace CURSOR FOR

    SELECT SchemaName, TableName,ColumnName FROM #FilteredTables

    OPEN cur_Replace

    FETCH NEXT FROM cur_Replace INTO @SchemaName, @TableName,@ColumnName

    WHILE (@@FETCH_STATUS = 0)

    BEGIN

    SET @cmd = 'UPDATE ' + @SchemaName+'.'+@TableName +

    ' SET ' + @ColumnName

    + ' = REPLACE(' + @ColumnName + ', '

    + QUOTENAME(@FindString, '''') + ', ' + QUOTENAME(@ReplaceString, '''') +

    ') WHERE ' + @ColumnName + ' LIKE ' + @WhrFindString

    print @cmd

    FETCH NEXT FROM cur_Replace INTO @SchemaName, @TableName,@ColumnName

    END

    CLOSE cur_Replace

    DEALLOCATE cur_Replace

    END