USE [DataPRO] GO /****** Object: StoredProcedure [dbo].[sp_DBImportSensorsDigitalInput] Script Date: 6/19/2018 12:23:52 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[sp_DBImportSensorsDigitalInput] @DigitalInputSensors xml ,@errorNumber int output ,@errorMessage nvarchar(250) output AS BEGIN set @errorNumber = 0 set @errorMessage = '' declare @tSensorsDigitalIn table ([SerialNumber] [nvarchar](50), [SettingMode] [int], [ScaleMultiplier] [nvarchar](50), [LastModified] [datetime], [LastModifiedBy] [nvarchar](50), [eId] [nvarchar](50), [UserValue1] [nvarchar](255), [UserValue2] [nvarchar](255), [UserValue3] [nvarchar](255), /*[UserTags] [varbinary](max) NULL,*/ [UserTagsText] [nvarchar](4000) NULL, [MeasurementUnit] [nvarchar](50), [FilterClass] [nvarchar](50), [Processed] [bit]) insert into @tSensorsDigitalIn (SerialNumber , SettingMode , ScaleMultiplier , LastModified , LastModifiedBy , eId , UserValue1 , UserValue2 , UserValue3 /*, UserTags*/ , UserTagsText , MeasurementUnit , FilterClass ,Processed) select t.x.value('DigitalInputSettingName[1]', 'nvarchar(50)') as SerialNumber , dbo.foo_SettingModeConverterToInt(t.x.value('DigitalInputSettingMode[1]', 'nvarchar(50)')) as SettingMode , t.x.value('DigitalInputScaleMultiplier[1]', 'nvarchar(50)') as ScaleMultiplier , getdate() as LastModified /* t.x.value('@LastModified', 'datetime') */ , 'DBImport' as ModifiedBy /* t.x.value('@ModifiedBy', 'varchar(50)') */ , CASE WHEN t.x.value('eId[1]', 'varchar(50)') IS NULL THEN '' ELSE t.x.value('eId[1]', 'varchar(50)') end as eId , t.x.value('UserValue1[1]', 'nvarchar(255)') as UserValue1 , t.x.value('UserValue2[1]', 'nvarchar(255)') as UserValue2 , t.x.value('UserValue3[1]', 'nvarchar(255)') as UserValue3 /*, 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 , 'V' /* Change this if MeasurementUnit is ever exported */ , '1650 (CFC1000)' /* Change this if FilterClass is ever exported */ , 0 from @DigitalInputSensors.nodes('/Sensors/SensorData') t(x) OPTION (OPTIMIZE FOR ( @DigitalInputSensors = NULL )) while (Select Count(*) From @tSensorsDigitalIn Where Processed = 0) > 0 Begin begin try begin transaction tSensorsDigitalIn declare @SerialNumber nvarchar(50) ,@SettingMode int ,@ScaleMultiplier nvarchar(50) ,@LastModified datetime ,@LastModifiedBy nvarchar(50) ,@eId nvarchar(50) ,@UserValue1 nvarchar(255) ,@UserValue2 nvarchar(255) ,@UserValue3 nvarchar(255) ,@UserTags varbinary(max) ,@UserTagsText nvarchar(4000) ,@MeasurementUnit nvarchar(50) ,@FilterClass nvarchar(50) ,@new_id int select top 1 @SerialNumber = SerialNumber ,@SettingMode = SettingMode ,@ScaleMultiplier = ScaleMultiplier ,@LastModified = LastModified ,@LastModifiedBy = LastModifiedBy ,@eId = eId ,@UserValue1 = UserValue1 ,@UserValue2 = UserValue2 ,@UserValue3 = UserValue3 /*,@UserTags = UserTags*/ ,@UserTagsText = UserTagsText ,@MeasurementUnit = MeasurementUnit ,@FilterClass = FilterClass from @tSensorsDigitalIn 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 @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 exec dbo.sp_SensorsDigitalInUpdateInsert @SerialNumber ,@SettingMode ,@ScaleMultiplier ,@LastModified ,@LastModifiedBy ,@eId ,@UserValue1 ,@UserValue2 ,@UserValue3 ,@UserTags ,@MeasurementUnit ,@FilterClass ,@new_id ,@errorNumber output ,@errorMessage output update @tSensorsDigitalIn set Processed = 1 where SerialNumber = @SerialNumber commit transaction tSensorsDigitalIn end try begin catch set @errorNumber = error_number() set @errorMessage = error_message() rollback transaction tSensorsDigitalIn end catch; end END