title: Creating a UDF(User Define Function) if is does not exist and skipping it if it exists
body: <p>Hi and thanks for reading this.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Where am I going wrong here?</p>
  
  <pre><code>IF NOT EXISTS
  (select * from Information_schema.Routines where SPECIFIC_SCHEMA='dbo' 
  AND SPECIFIC_NAME = 'FMT_PHONE_NBR' AND Routine_Type='FUNCTION')
  
  /*CREATE FUNCTION TO FORMAT PHONE NUMBERS*/
  CREATE FUNCTION [dbo].[FMT_PHONE_NBR](@phoneNumber VARCHAR(12))
  RETURNS VARCHAR(12)
  AS
  BEGIN
      RETURN SUBSTRING(@phoneNumber, 1, 3) + '-' + 
             SUBSTRING(@phoneNumber, 4, 3) + '-' + 
             SUBSTRING(@phoneNumber, 7, 4)
  END
  
  GO
  </code></pre>
  
  <p>Or this:</p>
  
  <pre><code>IF NOT EXISTS
  (SELECT name FROM sys.objects WHERE name = 'dbo.FMT_PHONE_NBR')
  
  GO
  
  /*CREATE FUNCTION TO FORMAT PHONE NUMBERS*/
  CREATE FUNCTION [dbo].[FMT_PHONE_NBR](@phoneNumber VARCHAR(12))
  RETURNS VARCHAR(12)
  AS
  BEGIN
      RETURN SUBSTRING(@phoneNumber, 1, 3) + '-' + 
             SUBSTRING(@phoneNumber, 4, 3) + '-' + 
             SUBSTRING(@phoneNumber, 7, 4)
  END
  
  GO
  </code></pre>
  
  <p>Thanks for checking this out!</p>