2012-04-10 68 views
1

我没有很多SQL Server 2008的经验,我想重命名我的数据库中的所有列(添加到现有名称的某个前缀)。SQL Server 2008 R2 - 如何重命名多列名称?

我想是这样的:

Foreach(column in table) 
(
    column_name = prefix+column_name 
) 

有没有办法做到这一点?

非常感谢。

+1

为什么你会永远这样做呢? – 2012-04-10 07:26:51

回答

4

您可以尝试以下解决方案:使用光标遍历sys.columns表中的记录,并为每条记录执行系统存储过程sp_rename。 我认为在重命名时可能会出现完整性问题,因此您需要注意引用这些列的其他对象(例如存储过程)。

UPDATE:MVP说:

if you have any indexes, primary keys, unique keys, or foreign keys, they need to be dropped, and then readded, after the columns have been renamed. If you don't do this in the correct order, you will get lots of nasty error message.

1

你可以尝试使用sp_RENAME。它一次只允许单列。但是你可以在你的代码上做到这一点,以循环使用每一列。希望能帮助到你。谢谢!

相关问题