下面的脚本显示了从小写转换的数据的示例查询,但它只更改了表中一列的数据。
Use MYF601T
Go
UPDATE ROAD_LINE
SET NAM = UPPER(NAM)
但是,我尝试编写的以下脚本是转换所有表上的所有列,但生成的结果有错误。
Use MYF601T
Go
UPDATE INFORMATION_SCHEMA.TABLES
SET INFORMATION_SCHEMA.TABLES.TABLE_SCHEMA = UPPER(INFORMATION_SCHEMA.TABLES.TABLE_SCHEMA
如何对内部的所有表和所有列执行此操作?
答案 0 :(得分:0)
如果由于其他原因您想将表名转换为大写,您可以:
请注意,您必须更新光标中的选择以满足您的需要(选择您想要的列或表名称,...)
这将重命名表格:
/
+-- example.ipynb
+-- img
+-- picture.png
如果要更新表zyx中的所有列xyz,可以使用:
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
答案 1 :(得分: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);