I'm using sql server 2012:
I would like to script out creation of an object(if it doesn't exist first), and then alter it.
I'm trying to avoid the usual drop/create as it would impact stats. It also has to do with how we're scripting out changes to the DB.
What I have:
Go If object_id(N'My_Function', N'FN') IS NULL Begin Create Function My_Function ( @SomeParameter int ) Returns int As Begin -- Intentionally empty End End Go -- A header/comments Alter Function My_Function ( @SomeParameter int ) Returns int As Begin Declare @returnValue int -- A bunch of sql code Return @returnValue End Go
The problem I'm having is "Create" must be first in the script(or first after a "GO"), but I can't put a "GO" inside an "If" conditional. Is there a way to conditionally create something if it doesn't exist, and then alter it later?