重命名 MS SQL Server 2005 中的列

发布于 2024-07-30 14:39:38 字数 67 浏览 1 评论 0原文

使用 SQL(MS SQL Server 2005 变体)重命名表列时的最佳实践是什么? 这假设列中有必须保留的数据。

What is the best practice when it comes to renaming a table column using SQL (MS SQL Server 2005 variant)? This assumes that there is data in the column that must be preserved.

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

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

发布评论

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

评论(2

半仙 2024-08-06 14:39:38

您必须使用存储过程来重命名列。 以下操作会将您的列从“oldColumnName”重命名为“newColumnName”,而不影响任何数据。

EXEC sp_rename 'tableName.[oldColumnName]', 'newColumnName', 'COLUMN'

显然,您必须手动更新使用旧名称的任何代码/存储过程/SQL。

You have to use a stored proc to rename a column. The following will rename your column from 'oldColumnName' to 'newColumnName' without affecting any data.

EXEC sp_rename 'tableName.[oldColumnName]', 'newColumnName', 'COLUMN'

Obviously you'll have to update any code / stored procs / SQL that uses the old name manually.

时常饿 2024-08-06 14:39:38

我今天遇到了同样的问题,解决方案是杀死数据库上的所有进程,因为进程锁定了事务。 我执行了过程sp_rename,但问题没有解决。 所以我杀死了数据库中的进程并且进程正常工作。

USE MASTER
GO

--Kill all the connections opened in database.
DECLARE @dbname sysname
SET @dbname = 'database_name'

DECLARE @spid int
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname)
WHILE @spid IS NOT NULL
BEGIN
EXECUTE ('KILL ' + @spid)
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname) AND spid > @spid
END


SELECT request_session_id
FROM   sys.dm_tran_locks
WHERE  resource_database_id = DB_ID('database_name') 

I had the same problem today, and the solution was kill all processes on the database, cause the processes was locked the transactions. I was executed the procedure sp_rename, but the problem was not resolved. So i was kill the processes in the database and the proc works.

USE MASTER
GO

--Kill all the connections opened in database.
DECLARE @dbname sysname
SET @dbname = 'database_name'

DECLARE @spid int
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname)
WHILE @spid IS NOT NULL
BEGIN
EXECUTE ('KILL ' + @spid)
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname) AND spid > @spid
END


SELECT request_session_id
FROM   sys.dm_tran_locks
WHERE  resource_database_id = DB_ID('database_name') 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文