Skip to content
Advertisement

Creating a UDF(User Define Function) if is does not exist and skipping it if it exists

Hi and thanks for reading this.

I am trying to use the IF EXISTS/IF NOT EXISTS statement to check if an Object exist. Basically I want to skip it if it is there or create it if it is not there.

I have writing the code in two different ways but I get an error: Create function must be the only function in the batch. If I place GO between the statements as Illustrated below, I get another warning: Incorrect Syntax near GO.

Where am I going wrong here?

Or this:

Thanks for checking this out!

Advertisement

Answer

The easiest way to solve this is actually to delete the function if it already exists, and then re-create it:

Note the usage of the ‘object_id’ function in the above. This is actually a pretty common way to check for the existence of an object, although it is subject to certain constraints.

You can read more about it here: OBJECT_ID

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement