在多个表中查找和替换部分字符串

时间:2014-07-24 12:15:21

标签: sql-server replace orchardcms batch-rename

我想将运行Orchard CMS的网站从lokal服务器移动到实时服务器。问题是Orchard使用了相对和绝对URL的奇怪混合。

目前的“baseurl”是http://localhost:12345/OrchardLocal/

新的“baseurl”将为http://www.mywebsite.com/content/

问题在于,当您通过Orchard编辑器将图像等媒体插入页面时,图像src网址将采用~/OrchardLocal/Media/...格式,因此在将网站移动到实时服务器后,所有这些网址都将导致404

我现在正在寻找一种方法来“批量替换”匹配/OrchardLocal//content/的每个部分字符串。

我在使用MSSQL方面经验很少,我只能猜测,在我的IDE中搜索替换可能会有点复杂......

在Orchard后端手动查找和更改所有损坏的链接需要永远,所以任何帮助都会受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

您需要使用嵌套循环查找所有表,然后查找每个表的所有列。 我修改了一些我必须满足你需求的东西:

   /*
Author: sqiller
Description: Searches for a value to replace in all columns from all tables
USE: EXEC dbo.usp_Update_AllTAbles 'work', 'sqiller', 1
@search = Value to look for Replace
@newvalue = the value that will replace @search
@Test = If set to 1, it will only PRINT the UPDATE statement instead of EXEC, useful to see
        what is going to update before.
*/
CREATE PROCEDURE dbo.usp_Update_AllTAbles(
@search varchar(100),
@newvalue varchar(100),
@Test bit)
AS
BEGIN

            IF NOT EXISTS (select 1 from INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Tables_to_Update')
            BEGIN
                    CREATE TABLE dbo.Tables_to_Update(
                    Table_name varchar(100),
                    Column_name varchar(100),
                    recordsToUpdate int
                    )
            END
            DECLARE @table varchar(100)
            DECLARE @column varchar(100)
            DECLARE @SQL varchar(max)

            SELECT TABLE_SCHEMA+'.'+TABLE_NAME as Table_Name, 0 as Processed INTO #tables from information_schema.tables WHERE TABLE_TYPE != 'VIEW'

            WHILE EXISTS (select * from #tables where processed = 0)
            BEGIN
                SELECT top 1 @table = table_name from #tables where processed = 0

                SELECT column_name, 0 as Processed INTO #columns from information_schema.columns where TABLE_SCHEMA+'.'+TABLE_NAME = @table


                WHILE EXISTS (SELECT * from #columns where processed = 0)
                BEGIN
                    SELECT top 1 @column = COLUMN_NAME from #columns where processed = 0

                    SET @SQL = 'INSERT INTO Tables_to_Update
                                select '''+ @table +''', '''+ @column +''', count(*) from '+@table+ ' where '+ @column +' like ''%'+ @search +'%'''
                    EXEC(@SQL)

                    IF EXISTS (SELECT * FROM Tables_to_Update WHERE Table_name = @table)
                    BEGIN
                        SET @SQL = 'UPDATE '+ @table + ' SET '+ @column + ' = REPLACE('''+@column+''','''+@search+''','''+ @newvalue +''')  WHERE '+ @column + ' like ''%'+@search+'%'''
                        --UPDATE HERE
                        IF (@Test = 1)
                        BEGIN   
                            PRINT @SQL
                        END
                        ELSE
                        BEGIN
                            EXEC(@SQL)
                        END
                    END

                    UPDATE #columns SET Processed = 1 where COLUMN_NAME = @column
                END

                DROP TABLE #columns

                UPDATE #tables SET Processed = 1 where table_name = @table
            END

            SELECT * FROM Tables_to_Update where recordsToUpdate > 0

END