日期:2014-05-18 浏览次数:20747 次
EXEC SP_MSFOREACHTABLE 'UPDATE ? SET [NAME]='目标值''
------解决方案--------------------
也可以拼语句,不过要写的比上面多,你要的话可以写给你
------解决方案--------------------
以下语句可以查到是哪几张表中有这个字段
SELECT a.name AS Table_Name, b.name AS Column_Name, c.name AS Variable_Type, b.xtype, b.xusertype, b.length FROM sys.sysobjects AS a WITH (nolock) INNER JOIN sys.syscolumns AS b WITH (nolock) ON b.id = a.id LEFT OUTER JOIN sys.systypes AS c WITH (nolock) ON b.xtype = c.xtype AND b.xusertype = c.xusertype WHERE (a.type = 'U') and b.name='你要找的列名'
------解决方案--------------------
EXEC sp_MSforeachtable @whereand='and exists(select * from syscolumns where id = o.id and name = ''name'')',
@replacechar='*',
@precommand='print ''Updating .....''',
@command1='print ''*'' update * set name = ''&*%^%##''',
@postcommand= 'print ''Complete Update !'''
------解决方案--------------------
EXEC sp_MSforeachtable @whereand='and exists(select * from syscolumns where id = o.id and name = ''name'')', @replacechar='*', @precommand='print ''Updating .....''', @command1='print ''*'' update * set name = ''&*%^%##''', @postcommand= 'print ''Complete Update !'''
------解决方案--------------------
把以下语句的执行结果Copy出来执行一次就能达到你的要求了
SELECT a.name AS Table_Name, b.name AS Column_Name, c.name AS Variable_Type, b.xtype, b.xusertype, b.length into #temp FROM sys.sysobjects AS a WITH (nolock) INNER JOIN sys.syscolumns AS b WITH (nolock) ON b.id = a.id LEFT OUTER JOIN sys.systypes AS c WITH (nolock) ON b.xtype = c.xtype AND b.xusertype = c.xusertype WHERE (a.type = 'U') and b.name='你要找的列名' select 'update '+Table_Name+' set '+Column_Name+'=''字段要改成的值''' from #temp
------解决方案--------------------
学习了