ALTER PROCEDURE [dbo].[sp_DBImportSensorsAnalog] @AnalogSensors xml ,@errorNumber int output ,@errorMessage nvarchar(250) output AS BEGIN set @errorNumber = 0 set @errorMessage = '' declare @tSensorsAnalog table ( [SerialNumber] [nvarchar](50) NOT NULL, [UserSerialNumber] [nvarchar](50) NOT NULL, [Model] [nvarchar](50) NULL, [SensorModelId] [int] NOT NULL, [Manufacturer] [nvarchar](50) NULL, [Status] [nvarchar](50) NOT NULL, [MeasurementUnit] [nvarchar](50) NOT NULL, [OffsetToleranceLow] [float] NOT NULL, [OffsetToleranceHigh] [float] NOT NULL, [eId] [nvarchar](50) NOT NULL, [Capacity] [float] NOT NULL, [Comment] [nvarchar](50) NOT NULL, [BridgeType] [smallint] NOT NULL, [BridgeLegMode] [smallint] NOT NULL, [Shunt] [smallint] NOT NULL, [Invert] [bit] NOT NULL, [UserValue1] [nvarchar](50) NULL, [UserValue2] [nvarchar](50) NULL, [UserValue3] [nvarchar](50) NULL, [FilterClass] [nvarchar](50) NOT NULL, [BridgeResistance] [float] NOT NULL, [IsoCode] [nvarchar](50) NOT NULL, [CheckOffset] [bit] NOT NULL, [SupportedExcitation] [nvarchar](50) NOT NULL, [InitialEU] [float] NULL, [CalInterval] [int] NOT NULL, [CalibrationSignal] [bit] NOT NULL, [InternalShuntResistance] [float] NOT NULL, [ExternalShuntResistance] [float] NOT NULL, [UniPolar] [bit] NOT NULL, [RangeLow] [float] NOT NULL, [RangeAve] [float] NOT NULL, [RangeHigh] [float] NOT NULL, [Created] [datetime] NOT NULL, [TimesUsed] [bigint] NOT NULL, [SensorCategory] [int] NOT NULL, [BypassFilter] [bit] NOT NULL, [CouplingMode] [smallint] NOT NULL, [Version] [int] NOT NULL, [LastModified] [datetime] NOT NULL, [ModifiedBy] [nvarchar](50) NOT NULL, [LocalOnly] [bit] NULL , [AxisNumber] [smallint] NOT NULL, [NumberOfAxes] [smallint] NOT NULL, /*[UserTags] [varbinary](max) NULL,*/ [UserTagsText] [nvarchar](4000) NULL, [DoNotUse] [bit] NOT NULL , [Broken] [bit] NOT NULL, [DiagnosticsMode] [bit] NOT NULL, [Processed] [bit] ) insert into @tSensorsAnalog (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*/ , UserTagsText , Broken , DoNotUse , DiagnosticsMode ,Processed) select t.x.value('SerialNumber[1]', 'nvarchar(50)') as SerialNumber , t.x.value('UserSerialNumber[1]', 'nvarchar(20)') as UserSerialNumber , t.x.value('Model[1]', 'nvarchar(50)') as Model ,0 as SensorModelId , t.x.value('Manufacturer[1]', 'nvarchar(50)') as Manufacturer , dbo.foo_StatusConverterToInt(t.x.value('Status[1]', 'nvarchar(50)')) as [Status] , t.x.value('MeasurementUnit[1]', 'nvarchar(50)') as MeasurementUnit , t.x.value('OffsetToleranceLow[1]', 'float') as OffsetToleranceLow , t.x.value('OffsetToleranceHigh[1]', 'float') as OffsetToleranceHigh , CASE WHEN t.x.value('Id[1]', 'varchar(50)') IS NULL THEN '' ELSE t.x.value('Id[1]', 'varchar(50)') end as eId , t.x.value('Capacity[1]', 'float') as Capacity , t.x.value('Comment[1]', 'nvarchar(50)') as Comment , dbo.foo_BridgeTypeConverterToInt(t.x.value('BridgeType[1]', 'varchar(50)')) as BridgeType , dbo.foo_BridgeLegModeConverterToInt(t.x.value('BridgeLegMode[1]', 'varchar(50)')) as BridgeLegMode , dbo.foo_ShuntConverterToInt(t.x.value('Shunt[1]', 'varchar(50)')) as Shunt , t.x.value('Invert[1]', 'bit') as Invert , t.x.value('UserValue1[1]', 'nvarchar(50)') as UserValue1 , t.x.value('UserValue2[1]', 'nvarchar(50)') as UserValue2 , t.x.value('UserValue3[1]', 'nvarchar(50)') as UserValue3 , t.x.value('FilterClass[1]', 'nvarchar(50)') as FilterClass , t.x.value('BridgeResistance[1]', 'float') as BridgeResistance , t.x.value('IsoCode[1]', 'nvarchar(50)') as IsoCode , t.x.value('CheckOffset[1]', 'bit') as CheckOffset , t.x.value('SupportedExcitation[1]', 'nvarchar(max)') as SupportedExcitation , 0 /*t.x.value('InitialEU[1]', 'float')*/ as InitialEU , t.x.value('CalInterval[1]', 'int') as CalInterval , t.x.value('CalibrationSignal[1]', 'bit') as CalibrationSignal , t.x.value('InternalShuntResistance[1]', 'float') as InternalShuntResistance , t.x.value('ExternalShuntResistance[1]', 'float') as ExternalShuntResistance , t.x.value('UniPolar[1]', 'bit') as UniPolar , t.x.value('RangeLow[1]', 'float') as RangeLow , t.x.value('RangeAve[1]', 'float') as RangeAve , t.x.value('RangeHigh[1]', 'float') as RangeHigh , t.x.value('Created[1]', 'datetime') as Created , t.x.value('TimesUsed[1]', 'bigint') as TimesUsed , t.x.value('SensorCategory[1]', 'int') as SensorCategory , t.x.value('BypassFilter[1]', 'bit') as BypassFilter , dbo.foo_CouplingModeConverterToInt(t.x.value('CouplingMode[1]', 'varchar(50)')) as CouplingMode , t.x.value('Version[1]', 'int') as [Version] , t.x.value('LastModified[1]', 'datetime') /*getdate() */ as LastModified , t.x.value('ModifiedBy[1]', 'varchar(50)') /*'DBImport' */ as ModifiedBy , t.x.value('LocalOnly[1]', 'bit') as LocalOnly , t.x.value('AxisNumber[1]', 'smallint') as AxisNumber , t.x.value('NumberOfAxes[1]', 'smallint') as NumberOfAxes /*, CASE WHEN t.x.value('UserTags[1]', 'varbinary(max)') IS NULL THEN 0 ELSE t.x.value('UserTags[1]', 'varbinary(max)') end as UserTags */ , CASE WHEN t.x.value('UserTags[1]', 'nvarchar(4000)') IS NULL THEN '' ELSE t.x.value('UserTags[1]', 'nvarchar(4000)') end as UserTagsText , t.x.value('Broken[1]', 'bit') as Broken , t.x.value('DoNotUse[1]', 'bit') as DoNotUse , CASE WHEN t.x.value('DiagnosticsMode[1]', 'bit') IS NULL THEN '' ELSE t.x.value('DiagnosticsMode[1]', 'bit') end as DiagnosticsMode ,0 from @AnalogSensors.nodes('/Sensors/SensorData') t(x) OPTION (OPTIMIZE FOR ( @AnalogSensors = NULL )) while (Select Count(*) From @tSensorsAnalog Where Processed = 0) > 0 Begin begin try begin transaction tSensorsAnalog declare @SerialNumber nvarchar(50) , @UserSerialNumber nvarchar(50) , @Model nvarchar(50) , @SensorModelId int , @Manufacturer nvarchar(50) , @Status nvarchar(50) , @MeasurementUnit nvarchar(50) , @OffsetToleranceLow float , @OffsetToleranceHigh float , @eId nvarchar(50) , @Capacity float , @Comment nvarchar(50) , @BridgeType smallint , @BridgeLegMode smallint , @Shunt smallint , @Invert bit , @UserValue1 nvarchar(50) , @UserValue2 nvarchar(50) , @UserValue3 nvarchar(50) , @FilterClass nvarchar(50) , @BridgeResistance float , @IsoCode nvarchar(50) , @CheckOffset bit , @SupportedExcitation nvarchar(50) , @InitialEU float , @CalInterval int , @CalibrationSignal bit , @InternalShuntResistance float , @ExternalShuntResistance float , @UniPolar bit , @RangeLow float , @RangeAve float , @RangeHigh float , @Created datetime , @TimesUsed bigint , @SensorCategory int , @BypassFilter bit , @CouplingMode smallint , @Version int , @LastModified datetime , @ModifiedBy nvarchar(50) , @LocalOnly bit , @AxisNumber smallint , @NumberOfAxes smallint , @UserTags varbinary(max) , @UserTagsText nvarchar(4000) , @DoNotUse bit , @Broken bit , @DiagnosticsMode bit select top 1 @SerialNumber = SerialNumber ,@UserSerialNumber = UserSerialNumber ,@Model = Model ,@SensorModelId = SensorModelId ,@Manufacturer = Manufacturer ,@Status = [Status] ,@MeasurementUnit = MeasurementUnit ,@OffsetToleranceLow = OffsetToleranceLow ,@OffsetToleranceHigh = OffsetToleranceHigh ,@eId = eId ,@Capacity = Capacity ,@Comment = Comment ,@BridgeType = BridgeType ,@BridgeLegMode = BridgeLegMode ,@Shunt = Shunt ,@Invert = Invert ,@UserValue1 = UserValue1 ,@UserValue2 = UserValue2 ,@UserValue3 = UserValue3 ,@FilterClass = FilterClass ,@BridgeResistance = BridgeResistance ,@IsoCode = IsoCode ,@CheckOffset = CheckOffset ,@SupportedExcitation = SupportedExcitation ,@InitialEU = InitialEU ,@CalInterval = CalInterval ,@CalibrationSignal = CalibrationSignal ,@InternalShuntResistance = InternalShuntResistance ,@ExternalShuntResistance = ExternalShuntResistance ,@UniPolar = UniPolar ,@RangeLow = RangeLow ,@RangeAve = RangeAve ,@RangeHigh = RangeHigh ,@Created = Created ,@TimesUsed = TimesUsed ,@SensorCategory = SensorCategory ,@BypassFilter = BypassFilter ,@CouplingMode = CouplingMode ,@Version = [Version] ,@LastModified = LastModified ,@ModifiedBy = ModifiedBy ,@LocalOnly = LocalOnly ,@AxisNumber = AxisNumber ,@NumberOfAxes = NumberOfAxes /*,@UserTags = UserTags*/ ,@UserTagsText = UserTagsText ,@DoNotUse = DoNotUse ,@Broken = Broken ,@DiagnosticsMode = DiagnosticsMode from @tSensorsAnalog where Processed = 0 declare @tagTable TABLE (Element nvarchar(4000), Processed bit) declare @tagIdTable TABLE (Id int, Processed bit) insert into @tagTable (Element, Processed) select * from dbo.foo_SplitDelimitedString(@UserTagsText, ',') /*call foo_IdGetTag to get the tag id */ while (Select Count(*) From @tagTable Where Processed = 0) > 0 Begin begin try declare @tagName nvarchar(4000) declare @tagIdTemp int select top 1 @tagName = Element from @tagTable where Processed = 0 set @tagIdTemp = dbo.foo_IdGetTag(@tagName) insert into @tagIdTable (Id, Processed) VALUES (@tagIdTemp, 0) end try begin catch set @errorNumber = error_number() set @errorMessage = error_message() rollback transaction tSensorsAnalog end catch; update @tagTable set Processed = 1 where Element = @tagName End /*store in a byte array*/ while (Select Count(*) From @tagIdTable Where Processed = 0) > 0 Begin begin try declare @tagId int declare @tagIdByte varbinary(4) select top 1 @tagId = Id from @tagIdTable where Processed = 0 --set @tagIdByte = dbo.foo_ConvertIntToBytes(@tagId) set @tagIdByte = CONVERT(VARBINARY(MAX), REVERSE(CONVERT(VARBINARY(4), @tagId))) set @UserTags = CONVERT(varbinary(max), CONCAT(@UserTags, @tagIdByte)) end try begin catch set @errorNumber = error_number() set @errorMessage = error_message() rollback transaction tSensorsAnalog end catch; update @tagIdTable set Processed = 1 where Id = @tagId End declare @new_id int exec dbo.sp_SensorsAnalogUpdateInsert @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 ,@DiagnosticsMode ,@new_id output ,@errorNumber output ,@errorMessage output update @tSensorsAnalog set Processed = 1 where SerialNumber = @SerialNumber commit transaction tSensorsAnalog end try begin catch set @errorNumber = error_number() set @errorMessage = error_message() rollback transaction tSensorsAnalog end catch; end END