日期:2014-05-18 浏览次数:20650 次
declare @sql varchar(1000), --sql语句 @nCol int --你要改的列 set @nCol=2 --假设你要改第2列 set @sql='update #TmpA set ' + col_name(N'#TmpA',@nCol) + '=replace(' + col_name(N'#TmpA',@nCol) + ', ''e'', ''f'')' exec(@sql)
------解决方案--------------------
替换列名用sp_rename 函数
use tempdb declare @columnNameOld varchar(100),@columnNameNew varchar(100) select @columnNameOld = name from syscolumns where name like '%e%' and id = object_id('#TmpA') select @columnNameNew = replace(@columnNameOld,'e','f') select @columnNameOld = '#TmpA.' + @columnNameOld EXECUTE sp_rename @columnNameOld, @columnNameNew, 'COLUMN'
------解决方案--------------------
USE tempdb
EXECUTE sp_rename '#TmpA.colfield1', 'colfifld1', 'COLUMN'
------解决方案--------------------
临时表的字段不能修改,因为临时表都是存储在msdn库里面,而不是存储在sysobjects和syscolumns等系统表里面
------解决方案--------------------
--如果是固定表: create table LastA (English int, Chinese int,USA int) insert LastA select 1,2,3 select * from LastA /* English Chinese USA ----------- ----------- ----------- 1 2 3 */ declare @sql varchar(8000) select @sql=isnull(@sql+char(10),'')+'exec sp_rename ''LastA.'+name+''','''+replace(name,'e','f')+''',''column''' from syscolumns where id=object_id('LastA') and charindex('e',name)>0 exec (@sql) select * from LastA /* fnglish Chinfsf USA ----------- ----------- ----------- 1 2 3 */ drop table LastA