Here is an easy way to generate the sp_rename
commands using just T-SQL, but this doesn't exactly match your naming definition. Getting the first letter after any underscore to be upper cased will require much more elaborate handling (e.g. 26 nested replace(name, '_a', '_A')
calls), but perhaps this is a useful start:
SELECT 'EXEC sp_rename '''
+ QUOTENAME(OBJECT_SCHEMA_NAME([object_id]))
+ '.' + QUOTENAME(OBJECT_NAME([object_id]))
+ '.' + QUOTENAME([name]) + ''', '
+ ''''
+ QUOTENAME(UPPER(LEFT([name],1))+LOWER(SUBSTRING([name], 2, LEN([name]))))
+ ''', ''COLUMN'';'
FROM sys.columns
WHERE [object_id] = OBJECT_ID('dbo.Outlet');
Run that script in an SSMS query window, then copy the output and run it in a new window.
Okay, here is the version that does the nested REPLACE()
calls. It's not pretty, but it works:
USE tempdb;
GO
SET NOCOUNT ON;
GO
CREATE TABLE dbo.Outlet
(
[OUTLET_KEY] [varchar](10) NOT NULL,
[OUTLET] [varchar](8) NULL,
[CITY_CODE] [varchar](4) NULL,
[OUTLET_NAME] [varchar](25) NULL,
[COUNTRY_CODE] [varchar](3) NULL,
[EXTENDED_CATEGORY_CODE] [varchar](2) NULL,
[PHONE_NUMBER] [varchar](17) NULL,
) ON [PRIMARY];
GO
SELECT 'EXEC sp_rename '''
+ QUOTENAME(OBJECT_SCHEMA_NAME([object_id]))
+ '.' + QUOTENAME(OBJECT_NAME([object_id]))
+ '.' + QUOTENAME([name]) + ''', '
+ '''' + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
QUOTENAME(UPPER(LEFT([name],1))+LOWER(SUBSTRING([name], 2, LEN([name]))))
, '_a', '_A'), '_b', '_B'), '_c', '_C'), '_d', '_D'), '_e', '_E')
, '_f', '_F'), '_g', '_G'), '_h', '_H'), '_i', '_I'), '_j', '_J')
, '_k', '_K'), '_l', '_L'), '_m', '_M'), '_n', '_N'), '_o', '_O')
, '_p', '_P'), '_q', '_Q'), '_r', '_R'), '_s', '_S'), '_t', '_T')
, '_u', '_U'), '_v', '_V'), '_w', '_W'), '_x', '_X'), '_y', '_Y'), '_z', '_Z')
+ ''',' + '''COLUMN'';'
FROM sys.columns
WHERE [object_id] = OBJECT_ID('dbo.Outlet');
GO
DROP TABLE dbo.Outlet;