How to change all column name to UPPER CASE for all the tables in one database of MS SQL Server?

cfh9epnr  于 2023-06-21  发布在  SQL Server
关注(0)|答案(5)|浏览(132)

is there any sql statement used to change all column name to UPPER CASE for all tables in database? MS SQL Server.

I got a sql to do that, but not sure whether it`s correct.

  1. run SQL below
select 'exec sp_rename '''+b.name+'.'+a.name+''','''+UPPER(a.name)+''',''column'''
from syscolumns a, sysobjects b 
where a.id=b.id and b.type='U' 
order by b.name
  1. copy and execute the result above
ttisahbt

ttisahbt1#

If you are upgrading an application from SQL Server 2000 to a later edition, and you are struggeling with SQL Server case sensitivity, I would suggest you look into the SQL Server 2000 compatibility setting before you do drastic changes to the database.

In SQL Server 2008 Management Studio

  1. Right-click the database and select properties in the context menu
  2. Go to the Options page
  3. In the third dropdown from the top. select Compatibility Level: SQL Server 2000

At least that is time consuming.

Edit: Since it appears that OP is upgrading his database from SQL Server 2005 to a "new" database on SQL Server 2005, the above strategy might not be optimal.

brjng4g3

brjng4g32#

I don't believe there is one command to do this.

However you should be able to write a query which does this, using 1 or 2 cursors and a query like:

select t.name As TableName, c.Column_Name
from sys.tables t
INNER JOIN information_schema.columns c ON c.Table_Name = t.Name
ORDER BY t.name

This should return all table and columns in your database.

Then use:

sp_RENAME 'TableName.[OldColumnName]' , '[NewColumnName]', 'COLUMN'

To rename each column.

mwkjh3gx

mwkjh3gx3#

Short answer - no.

If you need to do this (and many studies suggest that all upper case names detract from readability), you'll have to generate new tables with these upper case names, copy the data from the old to the new table, drop the old tables, rename the new tables, and re-establish all of the foreign key relationships.

Is there a good reason to do this?

r9f1avp5

r9f1avp54#

Extending Bravax answer, This will give you a list of commands to execute

select 'sp_RENAME ''' + t.name + '.' + QUOTENAME(c.Column_Name) + ''', ''' + UPPER(c.Column_Name) + ''', ''COLUMN'';\n go' As Command
from sys.tables t
INNER JOIN information_schema.columns c ON c.Table_Name = t.Name
ORDER BY t.name

You might need to add 'go' in-between lines if you are running as a bulk

afdcj2ne

afdcj2ne5#

DECLARE @TableName VARCHAR(256) -- table name 
DECLARE @ColumnName VARCHAR(256) -- column name 
DECLARE @sqlstring nvarchar(512) -- sql to execute

DECLARE db_cursor CURSOR FOR 
select t.name, c.Column_Name
from sys.tables t
INNER JOIN information_schema.columns c ON c.Table_Name = t.Name
ORDER BY t.name

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @TableName,  @ColumnName

WHILE @@FETCH_STATUS = 0  
BEGIN  
      set @sqlstring = N'sp_rename '''+ @TableName + '.' + QUOTENAME(@ColumnName) + ''', ''' + UPPER(@ColumnName) + ''', ''COLUMN''';

      EXECUTE sp_executesql @sqlstring;

      FETCH NEXT FROM db_cursor INTO @TableName,  @ColumnName
END 

CLOSE db_cursor  
DEALLOCATE db_cursor

相关问题