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