IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[foo_SensorsAnalog]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) DROP FUNCTION [dbo].[foo_SensorsAnalog] 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_SensorsAnalog]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) BEGIN execute dbo.sp_executesql @statement = N'CREATE FUNCTION [dbo].[foo_SensorsAnalog] ( ) RETURNS xml AS BEGIN RETURN isnull((SELECT [Id] ,[SerialNumber] ,[UserSerialNumber] ,[Model] ,[SensorModelId] ,[Manufacturer] ,[Status] ,[MeasurementUnit] ,[OffsetToleranceLow] ,[OffsetToleranceHigh] ,[eId] ,[Capacity] ,[Comment] ,[BridgeType] ,[BridgeLegMode] ,[Shunt] ,[Invert] ,[UserValue1] ,[UserValue2] ,[UserValue3] ,[FilterClass] ,[BridgeResistance] ,[IsoCode] ,[CheckOffset] ,[SupportedExcitation] ,[InitialEU] ,[CalInterval] ,[CalibrationSignal] ,[InternalShuntResistance] ,[ExternalShuntResistance] ,[UniPolar] ,[RangeLow] ,[RangeAve] ,[RangeHigh] ,[Created] ,[TimesUsed] ,[SensorCategory] ,[BypassFilter] ,[CouplingMode] ,[Version] ,[LastModified] ,[ModifiedBy] ,[LocalOnly] ,[AxisNumber] ,[NumberOfAxes] ,[UserTags] ,[DoNotUse] ,[Broken] FROM [dbo].[SensorsAnalog] Sensor for xml auto, BINARY BASE64), '''') END ' END GO