0

下面的脚本显示了一个对已从小写转换的数据的示例查询,但它只更改了表中一列的数据。

Use MYF601T
Go

UPDATE ROAD_LINE
SET NAM = UPPER(NAM)

但是,我正在尝试编写的以下脚本是对所有表的所有列进行 all 转换,但生成的结果有错误。

Use MYF601T
Go

UPDATE INFORMATION_SCHEMA.TABLES
SET INFORMATION_SCHEMA.TABLES.TABLE_SCHEMA = UPPER(INFORMATION_SCHEMA.TABLES.TABLE_SCHEMA

如何对里面的所有表和所有列执行此操作?

4

2 回答 2

0

如果出于什么原因要将表名转换为大写,您可以:

  • 使用将选择要重命名的表的游标
  • 循环遍历表列表
  • 使用sp_rename重命名或更新它

请注意,您必须更新光标中的 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
于 2015-09-03T08:52:02.863 回答
0

使用动态查询将所有列内容更新为upper大小写。

询问

declare @query varchar(max)

select @query =
stuff
(
    (
        select ';update ' + table_name + ' ' +
        'set ' + column_name + ' = upper(' + column_name + ')'
        from information_schema.columns
        where table_name = 'ROAD_LINE'
        order by table_name,column_name
        for xml path('')
     )
, 1, 1, '')

execute(@query);
于 2015-09-03T10:53:14.257 回答