When calling a stored procedure outside of a BEGIN…END block, I don’t need to use the EXEC or EXECUTE command; the following works:
SP_RENAME '[dbo].[TableName].[old_column]', 'new_column', 'COLUMN'
However, when I move this line inside a BEGIN…END block, it will throw a synatx error unless I use the EXEC or EXECUTE command:
IF EXISTS(SELECT * FROM sys.columns WHERE Name = N'old_column' AND Object_ID = Object_ID(N'TableName'))
BEGIN
EXEC SP_RENAME '[dbo].[TableName].[old_column]', 'new_column', 'COLUMN'
END
Any reason why?
From MSDN’s
EXECUTEArticle:Therefore, your second example throws the error because the stored procedure call is not the first statement in the batch.