查询以查找和替换SQL数据库中的特定文本(所有表和所有列)

查询以查找和替换SQL数据库中的特定文本(所有表和所有列),sql,sql-server-2008,Sql,Sql Server 2008,我有一个包含+100个表的数据库,大多数表都有一个保存URL的列,但“列名”不同,URL也不同。e、 g 在一张桌子上 usa3030.Development.com/portal/mybook/chapter1/page1/line1 另一个是它的 usa3030.Development.com/portal/mybook/chapter1 以此类推,但可以肯定的是,每个URL中都有“usa3030.Development.com/portal/mybook/chapter1” 因此,我需

我有一个包含+100个表的数据库,大多数表都有一个保存URL的列,但“列名”不同,URL也不同。e、 g

在一张桌子上

usa3030.Development.com/portal/mybook/chapter1/page1/line1
另一个是它的

usa3030.Development.com/portal/mybook/chapter1
以此类推,但可以肯定的是,每个URL中都有“usa3030.Development.com/portal/mybook/chapter1”

因此,我需要一个查询,该查询将查找列数据中包含文本的每一列(我想我们称之为通配符为“*”)

并将其替换为

usa3030.Development.com/MyWorld/Chp1
回答


在这里,我想您必须完成以下步骤:

DECLARE @string varchar(max)
For each table in sys.Objects (where type='U')

      For each column of the above table

        IF EXISTS(select * from tablename where colname like '%'+@string+'%')
        update table tablename SET colnmae=REPLACE(colname,'old','new') where colname like '%'+@string+'%')
DECLARE @string varchar(max)
For each table in sys.Objects (where type='U')

      For each column of the above table

        IF EXISTS(select * from tablename where colname like '%'+@string+'%')
        update table tablename SET colnmae=REPLACE(colname,'old','new') where colname like '%'+@string+'%')