Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql server if block errors when running a stored proc inside - gives a syntax error

Tags:

sql

sql-server

I would like to have a script for deployment which is rerunable. So I check if the table is there before renaming it.

IF EXISTS ( SELECT  * FROM    sys.objects  WHERE   object_id = OBJECT_ID(N'[dbo].[Schema]')  AND type IN ( N'U' ) )  
BEGIN
sp_rename [Schema], [SchemaInfo] 
END

The error is

Incorrect syntax near 'sp_rename'.

like image 827
eiu165 Avatar asked Dec 10 '25 20:12

eiu165


1 Answers

Try:

EXEC sp_rename N'Schema', N'SchemaInfo';

IMHO you should never call a stored procedure without EXEC.

like image 165
Aaron Bertrand Avatar answered Dec 12 '25 10:12

Aaron Bertrand