sqlserver中将某数据库下的所有表字段名称为小写的改为大写

时间:2023-03-09 15:58:40
sqlserver中将某数据库下的所有表字段名称为小写的改为大写
  1. declare @name varchar(50), @newname varchar(50),@colname varchar(50)
  2. declare abc cursor for
  3. select (a.[name] + '.' + b.[name]) as tablename ,b.[name] colname
  4. from dbo.sysobjects a ,dbo.syscolumns b where a.id = b.id
  5. and a.xtype= 'U'
  6. open abc
  7. fetch next from abc into @name,@colname
  8. while @@fetch_status=0 begin
  9. set @newname=upper(@colname)
  10. EXEC sp_rename @name,@newname,[COLUMN] print @colname PRINT @NEWNAME
  11. fetch next from abc into @name,@colname
  12. end
  13. close abc
  14. DEALLOCATE abc

更改库中所有列名字段为小写

declare @sql varchar(300)
declare @tablecolumnname varchar(100), @columnname varchar(100)
declare cursor1 cursor for
select b.name+'.['+a.name+']',a.name from syscolumns a ,sysobjects b where a.id = object_id(b.name) and b.xtype = 'u' and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and a.xtype <>36
open cursor1
fetch next from cursor1 into @tablecolumnname,@columnname
while @@fetch_status=0
begin
set @sql='sp_rename '''+@tablecolumnname+''','''+LOWER(@columnname)+''',''column'''
--print @sql
exec(@sql)
fetch next from cursor1 into @tablecolumnname,@columnname
end
close cursor1
deallocate cursor1