如何将 MS SQL Server 一个数据库中的所有表的所有列名更改为大写?

发布于 2024-10-21 13:26:31 字数 321 浏览 5 评论 0原文

是否有任何sql语句用于将数据库中所有表的所有列名更改为大写?微软 SQL 服务器。

我有一个sql来做到这一点,但不确定它是否正确。

  1. 运行下面的SQL

    select 'exec sp_rename '''+b.name+'.'+a.name+''','''+UPPER(a.name)+''',''列'''
    来自 syscolumns a、sysobjects b 
    其中 a.id=b.id 和 b.type='U' 
    按 b.name 排序
    
  2. 复制并执行上面的结果

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
    
  2. copy and execute the result above

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

没企图 2024-10-28 13:26:31

如果您要将应用程序从 SQL Server 2000 升级到更高版本,并且您在 SQL Server 区分大小写方面遇到困难,我建议您在对 SQL Server 2000 进行重大更改之前先查看一下 SQL Server 2000 兼容性设置数据库。

在 SQL Server 2008 Management Studio 中,

  1. 右键单击数据库,然后在上下文菜单中选择 properties
  2. 转至
  3. 顶部第三个下拉列表中的 Options 页面。选择兼容性级别:SQL Server 2000

至少这很耗时。

编辑:由于 OP 似乎正在将其数据库从 SQL Server 2005 升级到 SQL Server 2005 上的“新”数据库,因此上述策略可能不是最佳的。

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.

猫弦 2024-10-28 13:26:31

我不相信有一个命令可以做到这一点。

但是,您应该能够编写一个查询来执行此操作,使用 1 或 2 个游标和如下查询:

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 

这应该返回数据库中的所有表和列。

然后使用:

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

重命名每列。

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.

孤星 2024-10-28 13:26:31

简短的回答 - 不。

如果您需要这样做(许多研究表明所有大写名称都会降低可读性),您必须使用这些大写名称生成新表,将数据从旧表复制到新表,删除旧表,重命名新表,并重新建立所有外键关系。

这样做有充分的理由吗?

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?

々眼睛长脚气 2024-10-28 13:26:31

扩展 Bravax 答案,这将为您提供要执行的命令列表,

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 

如果您批量运行,您可能需要在行之间添加“go”

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

瑕疵 2024-10-28 13:26:31
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文