ALTER PROCEDURE [dbo].[sp_DBImportSensorModel] @SensorModel xml ,@errorNumber int output ,@errorMessage nvarchar(250) output AS BEGIN set @errorMessage = space(0) set @errorNumber = 0 declare @tSensorModel table ([recId] [int] IDENTITY(1,1), [Model] [nvarchar](50) NOT NULL, [Manufacturer] [nvarchar](50) NOT NULL, [UserPartNumber] [nvarchar](50) NOT NULL, [Capacity] [float] NOT NULL, [OffsetToleranceLow] [float] NOT NULL, [OffsetToleranceHigh] [float] NOT NULL, [MeasurementUnit] [nvarchar](50) NOT NULL, [Bridge] [smallint] NOT NULL, [Shunt] [smallint] NOT NULL, [BridgeResistance] [float] NOT NULL, [FilterClass] [nvarchar](50) NOT NULL, [UniPolar] [bit] NOT NULL, [IgnoreRange] [bit] NOT NULL, [CouplingMode] [smallint] NOT NULL, [Version] [int] NOT NULL, [RangeLow] [float] NOT NULL, [RangeAve] [float] NOT NULL, [RangeHigh] [float] NOT NULL, [LastModified] [datetime] NOT NULL, [ModifiedBy] [nvarchar](50) NOT NULL, [LocalOnly] [bit] NOT NULL, [NumberOfAxes] [smallint] NOT NULL, [CalInterval] [int] NOT NULL, [AxisNumber] [smallint] NOT NULL, [Polarity] [nvarchar](10) NOT NULL, [Invert] [bit] NOT NULL, [CheckOffset] [bit] NOT NULL, [CalibrationRecord] [nvarchar](MAX) NOT NULL, [ISOCode] [nvarchar](20) NOT NULL, [SupportedExcitation] [nvarchar](255) NOT NULL, [InitialEU] [float] NULL, [Processed] [bit]) insert into @tSensorModel ([Model] ,[Manufacturer] ,[UserPartNumber] ,[Capacity] ,[OffsetToleranceLow] ,[OffsetToleranceHigh] ,[MeasurementUnit] ,[Bridge] ,[Shunt] ,[BridgeResistance] ,[FilterClass] ,[UniPolar] ,[IgnoreRange] ,[CouplingMode] ,[Version] ,[RangeLow] ,[RangeAve] ,[RangeHigh] ,[LastModified] ,[ModifiedBy] ,[LocalOnly] ,[NumberOfAxes] ,[CalInterval] ,[AxisNumber] ,[Polarity] ,[Invert] ,[CheckOffset] ,[CalibrationRecord] ,[ISOCode] ,[SupportedExcitation] ,[InitialEU] ,[Processed]) select t.x.value('Model[1]', 'nvarchar(50)') , t.x.value('Manufacturer[1]', 'nvarchar(50)') , t.x.value('UserPartNumber[1]', 'nvarchar(50)') , t.x.value('Capacity[1]', 'float') , t.x.value('OffsetToleranceLow[1]', 'float') , t.x.value('OffsetToleranceHigh[1]', 'float') , t.x.value('MeasurementUnit[1]', 'nvarchar(50)') , dbo.foo_BridgeTypeConverterToInt(t.x.value('Bridge[1]', 'varchar(50)')) , dbo.foo_ShuntConverterToInt(t.x.value('Shunt[1]', 'varchar(50)')) , t.x.value('BridgeResistance[1]', 'float') , t.x.value('FilterClass[1]', 'nvarchar(50)') , t.x.value('UniPolar[1]', 'bit') , t.x.value('IgnoreRange[1]', 'bit') , dbo.foo_CouplingModeConverterToInt(t.x.value('CouplingMode[1]', 'varchar(50)')) , t.x.value('Version[1]', 'int') , t.x.value('RangeLow[1]', 'float') , t.x.value('RangeAve[1]', 'float') , t.x.value('RangeHigh[1]', 'float') , t.x.value('LastModified[1]', 'datetime') , t.x.value('ModifiedBy[1]', 'nvarchar(50)') , t.x.value('LocalOnly[1]', 'bit') , t.x.value('NumberOfAxes[1]', 'smallint') , t.x.value('CalInterval[1]', 'int') , t.x.value('AxisNumber[1]', 'smallint') , t.x.value('Polarity[1]', 'nvarchar(10)') , t.x.value('Invert[1]', 'bit') , t.x.value('CheckOffset[1]', 'bit') , t.x.value('CalibrationRecord[1]', 'nvarchar(max)') , t.x.value('ISOCode[1]', 'nvarchar(20)') , t.x.value('SupportedExcitation[1]', 'nvarchar(255)') , 0 /* InitialEU */ , 0 /* Processed */ from @SensorModel.nodes('/SensorModels/SensorModel') t(x) while (Select Count(*) From @tSensorModel Where Processed = 0) > 0 Begin begin try begin transaction tSensorModel declare @recId int declare @SensorModelId int ,@Model nvarchar(50) ,@Manufacturer nvarchar(50) ,@UserPartNumber nvarchar(50) ,@Capacity float ,@OffsetToleranceLow float ,@OffsetToleranceHigh float ,@MeasurementUnit nvarchar(50) ,@Bridge smallint ,@Shunt smallint ,@BridgeResistance float ,@FilterClass nvarchar(50) ,@UniPolar bit ,@IgnoreRange bit ,@CouplingMode smallint ,@Version int ,@RangeLow float ,@RangeAve float ,@RangeHigh float ,@LastModified datetime ,@ModifiedBy nvarchar(50) ,@LocalOnly bit ,@NumberOfAxes smallint ,@CalInterval int ,@AxisNumber smallint ,@Polarity nvarchar(10) ,@Invert bit ,@CheckOffset bit ,@CalibrationRecord nvarchar(MAX) ,@ISOCode nvarchar(20) ,@SupportedExcitation nvarchar(255) ,@InitialEU float select top 1 @recId = recId , @Model = [Model] , @Manufacturer = [Manufacturer] , @UserPartNumber = [UserPartNumber] , @Capacity = [Capacity] , @OffsetToleranceLow = [OffsetToleranceLow] , @OffsetToleranceHigh = [OffsetToleranceHigh] , @MeasurementUnit = [MeasurementUnit] , @Bridge = [Bridge] , @Shunt = [Shunt] , @BridgeResistance = [BridgeResistance] , @FilterClass = [FilterClass] , @UniPolar = [UniPolar] , @IgnoreRange = [IgnoreRange] , @CouplingMode = [CouplingMode] , @Version = [Version] , @RangeLow = [RangeLow] , @RangeAve = [RangeAve] , @RangeHigh = [RangeHigh] , @LastModified = [LastModified] , @ModifiedBy = [ModifiedBy] , @LocalOnly = [LocalOnly] , @NumberOfAxes = [NumberOfAxes] , @CalInterval = [CalInterval] , @AxisNumber = [AxisNumber] , @Polarity = [Polarity] , @Invert = [Invert] , @CheckOffset = [CheckOffset] , @CalibrationRecord = [CalibrationRecord] , @ISOCode = [ISOCode] , @SupportedExcitation = [SupportedExcitation] , @InitialEU = [InitialEU] from @tSensorModel where [Processed] = 0 exec [dbo].[sp_SensorModelsUpdateInsert] @Model , @Manufacturer , @UserPartNumber , @Capacity , @OffsetToleranceLow , @OffsetToleranceHigh , @MeasurementUnit , @Bridge , @Shunt , @BridgeResistance , @FilterClass , @UniPolar , @IgnoreRange , @CouplingMode , @Version , @RangeLow , @RangeAve , @RangeHigh , @LastModified , @ModifiedBy , @LocalOnly , @NumberOfAxes , @CalInterval , @AxisNumber , @Polarity , @Invert , @CheckOffset , @CalibrationRecord , @ISOCode , @SupportedExcitation , @InitialEU , @SensorModelId output , @errorNumber output , @errorMessage output update dbo.SensorsAnalog set SensorModelId = sm.SensorModelId from dbo.SensorModels sm where dbo.SensorsAnalog.Model = sm.Model update @tSensorModel set Processed = 1 where recId = @recId commit transaction tSensorModel end try begin catch set @errorMessage = error_message() set @errorNumber = error_number() rollback transaction tSensorModel end catch; end END