如果列存在于表中,则重命名该列

时间:2014-07-02 14:22:29

标签: sql sql-server sql-server-2008

我正在尝试重命名列(如果存在)

GO

IF EXISTS(SELECT 1 FROM sys.columns WHERE [name] = N'MinDuration'
           AND [object_id] = OBJECT_ID(N'Category'))
BEGIN
    sp_RENAME 'Category.MinDuration', 'SingleDuration' , 'COLUMN'
END

GO

但是收到错误Incorrect syntax near sp_RENAME. Expecting CONVERSATION, DIALOG, DISTRIBUTED or TRANSACTION

如果我的列存在于表中,如何重命名列?

1 个答案:

答案 0 :(得分:11)

首先尝试exec

IF EXISTS(SELECT 1 FROM sys.columns WHERE [name] = N'MinDuration'
           AND [object_id] = OBJECT_ID(N'Category'))
BEGIN
    EXEC sp_RENAME 'Category.MinDuration', 'SingleDuration' , 'COLUMN'
END;