When creating a function dynamically, it is a best practice to perform a check if a particular function you want to create has already existed in a database. To perform a check of an existing function you can use the following EXISTS keyword. If the function has already existed then you have to drop the function first and then perform a creation of the function.
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[my_function_name]') AND type IN ( N'FN', N'IF', N'TF', N'FS', N'FT' )) DROP FUNCTION [dbo].[my_function_name] GO CREATE FUNCTION [dbo].[my_function_name]( @ID NUMERIC(18,0) ) RETURNS BIT AS BEGIN DECLARE @NoOfRecords INT = 0 SELECT @NoOfRecords = COUNT(*) FROM [dbo].ExampleTable WHERE ID = @ID IF @NoOfRecords > 0 BEGIN RETURN 1 END RETURN 0 END GO
If you need to perform an update of a function, the way to check is pretty much the same as checking if a function already exists. You can re-drop the existing function and recreate it again..