IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[foo_IdGetCalibrationType]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) DROP FUNCTION [dbo].[foo_IdGetCalibrationType] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[foo_IdGetCalibrationType]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) BEGIN execute dbo.sp_executesql @statement = N'CREATE FUNCTION [dbo].[foo_IdGetCalibrationType] ( @NonLinear bit ,@CalibrationRecord varchar(255) ) RETURNS int AS BEGIN declare @id int if(@NonLinear = 0) begin select @id = CalibrationTypeId from [dbo].[CalibrationType] where CalibrationType = ''Linear'' end else if(charindex(''Poly'', @CalibrationRecord) != 0) begin select @id = CalibrationTypeId from [dbo].[CalibrationType] where CalibrationType = ''Polynomial'' end else if((charindex(''IRTracc'', @CalibrationRecord) != 0) or ltrim(rtrim(@CalibrationRecord)) = space(0)) begin select @id = CalibrationTypeId from [dbo].[CalibrationType] where CalibrationType = ''IRTracc'' end return @id END ' END GO