如果出于什么原因要将表名转换为大写,您可以:
请注意,您必须更新光标中的 Select 以满足您的需要(选择您想要的列或表名,...)
这将重命名表:
declare @TABLE_NAME sysname, @TABLE_SCHEMA sysname
declare @TABLE sysname, @newName sysname
declare table_cursor Cursor
For Select TABLE_NAME, TABLE_SCHEMA From INFORMATION_SCHEMA.TABLES Where TABLE_NAME like 'xyz%' -- and TABLE_SCHEMA like ...
open table_cursor
Fetch Next From table_cursor Into @TABLE_NAME, @TABLE_SCHEMA;
While @@FETCH_STATUS = 0
Begin
Set @TABLE = quotename(UPPER(@TABLE_SCHEMA)) + '.' + quotename(UPPER(@TABLE_NAME))
Set @newName = UPPER(@TABLE_NAME)
print 'rename ' + @TABLE + ' to ' + @newName
-- uncomment next like if you really want to rename them
--exec sp_rename @TABLE, @newName
Fetch Next From table_cursor Into @TABLE_NAME, @TABLE_SCHEMA;
End
Close table_cursor
Deallocate table_cursor
如果要更新表 zyx 中的所有列 xyz,可以使用以下命令:
declare @TABLE_NAME sysname, @TABLE_SCHEMA sysname, @COLUMN_NAME sysname
declare @TABLE sysname, @sql nvarchar(max)
declare table_cursor Cursor
For Select TABLE_NAME, TABLE_SCHEMA, COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS
Where COLUMN_NAME like 'xxx' -- and data_type '' ... and TABLE_NAME like 'xyz%' ... and TABLE_SCHEMA like ...
open table_cursor
Fetch Next From table_cursor Into @TABLE_NAME, @TABLE_SCHEMA, @COLUMN_NAME;
While @@FETCH_STATUS = 0
Begin
Set @TABLE = quotename(@TABLE_SCHEMA) + '.' + quotename(@TABLE_NAME)
set @sql = 'Update ' + @TABLE + ' set ' + @COLUMN_NAME + ' = UPPER(' + @COLUMN_NAME + ')'
print @sql
-- uncomment next like if you really want to execute them
--exec sp_executesql @sql
Fetch Next From table_cursor Into @TABLE_NAME, @TABLE_SCHEMA, @COLUMN_NAME;
End
Close table_cursor
Deallocate table_cursor