我正在尝试更改与其列名或表无关的特定值。
例如它可以出现在:
column `name` in the table `users`
而且在:
column `hi` in the table `jokes`
还有很多不为人知的地方。如何对数据库中的所有 sql 数据运行循环来更改它?
我正在使用 PHPMyAdmin
正如你所说的 PHPMyAdmin 我想你正在使用 MySQL。
看:
还有一个 PHP 脚本可以做到这一点:
对于 SQL server 2000:我写了这个脚本
-- full search and replace version sql server 2000
SET NOCOUNT ON
DECLARE @columnname varchar(255), @tablename varchar(255), @sql varchar(8000), @oldvalue varchar(1000), @newvalue varchar(1000)
SET @oldvalue = 'OldText' -- text to replace
SET @newvalue = 'NewText' -- text to replace by
DECLARE table_cursor CURSOR FOR
select name from sysobjects where xtype='U' AND [name] LIKE 'MyTablePrefix_%' -- your table prefix
OPEN table_cursor
FETCH NEXT FROM table_cursor
INTO @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'Updating Table: ' + @tablename + '...'
DECLARE column_cursor CURSOR FOR
SELECT syscolumns.name AS column_name
FROM sysobjects INNER JOIN
syscolumns ON sysobjects.id = syscolumns.id INNER JOIN
systypes ON syscolumns.xtype = systypes.xtype
WHERE (sysobjects.xtype = 'U')
AND systypes.[name] IN ('varchar','nvarchar','text','ntext')
AND sysobjects.name = @tablename
ORDER BY sysobjects.name, syscolumns.colid
OPEN column_cursor
FETCH NEXT FROM column_cursor INTO @columnname
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'Updating Column: ' + @columnname + '...'
SET @SQL = 'UPDATE ' + @tablename
SET @SQL = @SQL + ' SET ' + @columnname + ' = Replace(CAST(' + @columnname + ' AS nvarchar(4000)),'''+ @oldvalue + ''',''' + @newvalue + ''')'
--PRINT @sql
EXECUTE(@SQL)
FETCH NEXT FROM column_cursor INTO @columnname
END
CLOSE column_cursor
DEALLOCATE column_cursor
FETCH NEXT FROM table_cursor
INTO @tablename
END
CLOSE table_cursor
DEALLOCATE table_cursor
在夜间运行它,因为在大中型数据库上运行需要相当长的时间......