编辑:使用的技术是CMS的asp.net和数据库的MSSQL。
我想将项目从开发服务器移动到实时服务器。我使用的CMS将一些URL保存为绝对路径而不是相对路径,这会破坏大量图像。
即将破解的URL分散在整个数据库的多个表中。有些属于特定领域(如“source”或“url”),但大多数只是较大文本字段的一部分(cms的html内容)。
我现在正在寻找一种简单快捷的方法来在移动网站之前/之后“修复”网址。
这些URL都具有相同的结构,因此它基本上归结为搜索替换,但是当尝试在SQL中找到部分字符串的简单搜索替换时,我被包含多个循环等的代码墙所淹没。
所以我目前的计划是导出整个数据库(如mysqldump),将其加载到文本编辑器中,进行搜索替换,保存,导入。
这对我来说似乎是对用户来说最友好/最不复杂的解决方案 - 但是我很少直接处理SQL,所以也许我忽略了一些东西?如果有更好和/或更优雅的方式,请赐教。
答案 0 :(得分:2)
希望这可以帮到你,
这里的想法是遍历所有表中的所有列(仅字符列),并使用包含UPDATE
语句的REPLACE
语句更新列。
<强> SQL:强>
Declare @sysTab table (id int identity(1,1),[object_id] int)
Insert into @sysTab
Select object_id from sys.tables Where type = 'U'
Declare @ts int = 1, @te int = (Select Count(*) From sys.tables)
Declare @cs int ,@ce int,@object_id int, @sql varchar(max)
While @ts <= @te
Begin
Set @cs = 1
Set @object_id = (Select [object_id] From @sysTab Where id = @ts)
Set @ce = ( Select Count(*)
from sys.columns C
join sys.types ty On C.system_type_id = ty.system_type_id
where ty.name in ('char','nchar','ntext','nvarchar','text','varchar','nchar')
And object_id = @object_id)
While @cs <= @ce
Begin
Set @sql = 'Update A Set A.' + (Select name from sys.columns c where c.object_id = @object_id) +' = replace(A.'+ (Select name from sys.columns c where c.object_id = @object_id) + ',''URL'',''newURL'') From' + (Select name from sys.tables t where t.object_id = @object_id) + 'A'
Exec(@sql)
Set @cs = @cs + 1
End
Set @ts = @ts + 1
End