公司的數據庫規范SQL Server字段名稱需要大寫,最近檢查數據庫看到有些字段名稱是小寫。於是需要把小寫的字段名稱更改為大寫,先找出下面的包含小寫的SQL數據表。
SELECT c.name AS ColName, t.name AS TableName FROM sys.columns c JOIN sys.tables t ON c.object_id = t.object_id WHERE UPPER(c.[NAME]) COLLATE Latin1_General_CS_AS !=c.[NAME]
Google找到一篇文章,看到如何將表名或字段名更改為大寫,地址是
How can I change the names of all tables to uppercase?
運行下面的SQL語句,可以將所有的字段名更改為大寫。
SELECT 'EXEC sp_rename ''' + TABLE_SCHEMA + '.' + TABLE_NAME + '.' + COLUMN_NAME + ''', ''' + UPPER(COLUMN_NAME) + ''',''COLUMN''' FROM INFORMATION_SCHEMA.COLUMNS WHERE OBJECTPROPERTY(OBJECT_ID('[' + TABLE_SCHEMA + '].[' + TABLE_NAME + ']'), 'isMSShipped') = 0
